Skip to content

Commit 0e4a1d3

Browse files
committed
fix: build artifacts shouldn't throw error when 404, graceful handling
1 parent a608608 commit 0e4a1d3

File tree

3 files changed

+28
-41
lines changed

3 files changed

+28
-41
lines changed

bin/helpers/buildArtifacts.js

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,15 @@ const downloadAndUnzip = async (filePath, fileName, url) => {
110110
logger.debug(`Downloading build artifact for: ${filePath}`)
111111
return new Promise(async (resolve, reject) => {
112112
try {
113-
const response = await axios.get(url, {responseType: 'stream'});
113+
const response = await axios.get(url, {
114+
responseType: 'stream',
115+
validateStatus: status => (status >= 200 && status < 300) || status === 404
116+
});
114117
if(response.status != 200) {
115-
if (response.statusCode === 404) {
118+
if (response.status === 404) {
116119
reject(Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_NOT_FOUND);
117120
}
118-
const errorMsg = `Non 200 status code, got status code: ${response.statusCode}`;
121+
const errorMsg = `Non 200 status code, got status code: ${response.status}`;
119122
reject(errorMsg);
120123
} else {
121124
//ensure that the user can call `then()` only when the file has
@@ -140,7 +143,7 @@ const downloadAndUnzip = async (filePath, fileName, url) => {
140143
});
141144
}
142145
} catch (error) {
143-
reject();
146+
reject(error);
144147
}
145148
});
146149
}
@@ -259,45 +262,29 @@ exports.downloadBuildArtifacts = async (bsConfig, buildId, args, rawArgs, buildR
259262
try {
260263
response = await axios.get(options.url, options.config);
261264
buildDetails = response.data;
262-
if(response.status != 200) {
263-
logger.error('Downloading the build artifacts failed.');
264-
logger.error(`Error: Request failed with status code ${response.status}`)
265-
logger.error(utils.formatRequest(response.statusText, response, response.data));
266-
utils.sendUsageReport(bsConfig, args, JSON.stringify(buildDetails), Constants.messageTypes.ERROR, 'api_failed_build_artifacts', buildReportData, rawArgs);
265+
await createDirectories(buildId, buildDetails);
266+
await parseAndDownloadArtifacts(buildId, buildDetails, bsConfig, args, rawArgs, buildReportData);
267+
if (BUILD_ARTIFACTS_FAIL_COUNT > 0) {
268+
messageType = Constants.messageTypes.ERROR;
269+
message = Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_FAILED.replace('<build-id>', buildId).replace('<machine-count>', BUILD_ARTIFACTS_FAIL_COUNT);
270+
logger.error(message);
267271
process.exitCode = Constants.ERROR_EXIT_CODE;
268272
} else {
269-
await createDirectories(buildId, buildDetails);
270-
await parseAndDownloadArtifacts(buildId, buildDetails);
271-
if (BUILD_ARTIFACTS_FAIL_COUNT > 0) {
272-
messageType = Constants.messageTypes.ERROR;
273-
message = Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_FAILED.replace('<build-id>', buildId).replace('<machine-count>', BUILD_ARTIFACTS_FAIL_COUNT);
274-
logger.error(message);
275-
process.exitCode = Constants.ERROR_EXIT_CODE;
276-
} else {
277-
messageType = Constants.messageTypes.SUCCESS;
278-
message = Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_SUCCESS.replace('<build-id>', buildId).replace('<user-path>', process.cwd());
279-
logger.info(message);
280-
}
281-
await sendUpdatesToBstack(bsConfig, buildId, args, options, rawArgs, buildReportData)
282-
utils.sendUsageReport(bsConfig, args, message, messageType, null, buildReportData, rawArgs);
273+
messageType = Constants.messageTypes.SUCCESS;
274+
message = Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_SUCCESS.replace('<build-id>', buildId).replace('<user-path>', process.cwd());
275+
logger.info(message);
283276
}
277+
await sendUpdatesToBstack(bsConfig, buildId, args, options, rawArgs, buildReportData)
278+
utils.sendUsageReport(bsConfig, args, message, messageType, null, buildReportData, rawArgs);
284279
} catch (err) {
280+
messageType = Constants.messageTypes.ERROR;
281+
errorCode = 'api_failed_build_artifacts';
285282
if(err.response && err.response.status !== 200) {
286-
messageType = Constants.messageTypes.ERROR;
287-
errorCode = 'api_failed_build_artifacts';
288-
if (BUILD_ARTIFACTS_FAIL_COUNT > 0) {
289-
messageType = Constants.messageTypes.ERROR;
290-
message = Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_FAILED.replace('<build-id>', buildId).replace('<machine-count>', BUILD_ARTIFACTS_FAIL_COUNT);
291-
logger.error(message);
292-
} else {
293-
logger.error('Downloading the build artifacts failed.');
294-
}
295-
utils.sendUsageReport(bsConfig, args, err.response.data, messageType, errorCode, buildReportData, rawArgs);
296-
logger.error(`Error: Request failed with status code ${err.status}`)
297-
logger.error(utils.formatRequest(err.status, err.response, err.response.data));
283+
logger.error('Downloading the build artifacts failed.');
284+
logger.error(`Error: Request failed with status code ${err.response.status}`)
285+
logger.error(utils.formatRequest(err.response.statusText, err.response, err.response.data));
286+
utils.sendUsageReport(bsConfig, args, JSON.stringify(buildDetails), Constants.messageTypes.ERROR, 'api_failed_build_artifacts', buildReportData, rawArgs);
298287
} else {
299-
messageType = Constants.messageTypes.ERROR;
300-
errorCode = 'api_failed_build_artifacts';
301288
if (BUILD_ARTIFACTS_FAIL_COUNT > 0) {
302289
messageType = Constants.messageTypes.ERROR;
303290
message = Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_FAILED.replace('<build-id>', buildId).replace('<machine-count>', BUILD_ARTIFACTS_FAIL_COUNT);
@@ -306,8 +293,8 @@ exports.downloadBuildArtifacts = async (bsConfig, buildId, args, rawArgs, buildR
306293
logger.error('Downloading the build artifacts failed.');
307294
}
308295
utils.sendUsageReport(bsConfig, args, err, messageType, errorCode, buildReportData, rawArgs);
309-
logger.error(`Error: Request failed with status code ${response.status}`)
310-
logger.error(utils.formatRequest(err, response, response.data));
296+
logger.error(`Error: Request failed with status code ${resp.status}`)
297+
logger.error(utils.formatRequest(err, resp, body));
311298
}
312299
process.exitCode = Constants.ERROR_EXIT_CODE;
313300
}

bin/helpers/downloadBuildStacktrace.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const downloadBuildStacktrace = async (url) => {
1111
process.stdout.on('error', (err) => {
1212
error = err;
1313
process.stdout.close();
14-
reject(response.data.status);
14+
reject(response.status);
1515
});
1616
process.stdout.on('close', async () => {
1717
if (!error) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"dependencies": {
1515
"archiver": "5.3.0",
1616
"async": "3.2.3",
17-
"axios": "^1.4.0",
17+
"axios": "^1.7.7",
1818
"axios-retry": "^3.5.0",
1919
"browserstack-local": "1.5.4",
2020
"chalk": "4.1.2",

0 commit comments

Comments
 (0)