Skip to content

Commit 3eb23d9

Browse files
authored
chore(sourcemaps): use common HTTP interceptor for sourcemap uploads (#139)
1 parent 6c72894 commit 3eb23d9

File tree

1 file changed

+9
-29
lines changed

1 file changed

+9
-29
lines changed

src/sourcemaps/index.ts

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ import { injectFile } from './injectFile';
3232
import { Logger } from '../utils/logger';
3333
import { Spinner } from '../utils/spinner';
3434
import { uploadFile } from '../utils/httpUtils';
35-
import { AxiosError } from 'axios';
35+
import axios from 'axios';
3636
import { formatUploadProgress } from '../utils/stringUtils';
3737
import { wasInjectAlreadyRun } from './wasInjectAlreadyRun';
38+
import { attachApiInterceptor } from '../utils/apiInterceptor';
3839

3940
export type SourceMapInjectOptions = {
4041
directory: string;
@@ -162,7 +163,6 @@ export async function runSourcemapUpload(options: SourceMapUploadOptions, ctx: S
162163
* Upload files to the server
163164
*/
164165
let success = 0;
165-
let failed = 0;
166166

167167
logger.info('Upload URL: %s', getSourceMapUploadUrl(realm, '{id}'));
168168
logger.info('Found %s source map(s) to upload', jsMapFilePaths.length);
@@ -197,7 +197,6 @@ export async function runSourcemapUpload(options: SourceMapUploadOptions, ctx: S
197197
logger.info('sourceMapId %s would be used to upload %s', sourceMapId, path);
198198
});
199199
};
200-
201200
const uploadFileFn = options.dryRun ? dryRunUploadFile : uploadFile;
202201

203202
// notify user if we cannot be certain the "sourcemaps inject" command was already run
@@ -210,6 +209,10 @@ export async function runSourcemapUpload(options: SourceMapUploadOptions, ctx: S
210209

211210
// upload a single file
212211
try {
212+
const axiosInstance = axios.create();
213+
attachApiInterceptor(axiosInstance, logger, url, {
214+
userFriendlyMessage: 'An error occurred during source map upload.'
215+
});
213216
await uploadFileFn({
214217
url,
215218
file,
@@ -218,32 +221,12 @@ export async function runSourcemapUpload(options: SourceMapUploadOptions, ctx: S
218221
const { totalFormatted } = formatUploadProgress(loaded, total);
219222
spinner.updateText(`Uploading ${path} | ${totalFormatted} | ${filesRemaining} file(s) remaining`);
220223
},
221-
parameters
222-
});
224+
parameters,
225+
}, axiosInstance);
223226
success++;
224227
} catch (e) {
225-
failed++;
226228
spinner.stop();
227-
228-
const ae = e as AxiosError;
229-
const unableToUploadMessage = `Unable to upload ${path}`;
230-
231-
if (ae.response && ae.response.status === 413) {
232-
logger.warn(ae.response.status, ae.response.statusText);
233-
logger.warn(unableToUploadMessage);
234-
} else if (ae.response) {
235-
logger.error(ae.response.status, ae.response.statusText);
236-
logger.error(ae.response.data);
237-
logger.error(unableToUploadMessage);
238-
} else if (ae.request) {
239-
logger.error(`Response from ${url} was not received`);
240-
logger.error(ae.cause);
241-
logger.error(unableToUploadMessage);
242-
} else {
243-
logger.error(`Request to ${url} could not be sent`);
244-
logger.error(e);
245-
logger.error(unableToUploadMessage);
246-
}
229+
throw e;
247230
}
248231
}
249232
spinner.stop();
@@ -253,9 +236,6 @@ export async function runSourcemapUpload(options: SourceMapUploadOptions, ctx: S
253236
*/
254237
if (!options.dryRun) {
255238
logger.info(`${success} source map(s) were uploaded successfully`);
256-
if (failed > 0) {
257-
logger.info(`${failed} source map(s) could not be uploaded`);
258-
}
259239
}
260240

261241
if (jsMapFilePaths.length === 0) {

0 commit comments

Comments
 (0)