diff --git a/dist/index.js b/dist/index.js index c5029cb..badae57 100644 --- a/dist/index.js +++ b/dist/index.js @@ -891,6 +891,21 @@ exports.retryWait = retryWait; "use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { @@ -941,8 +956,17 @@ var OS = process.platform; var OUTPUT_TOTAL_ATTEMPTS_KEY = 'total_attempts'; var OUTPUT_EXIT_CODE_KEY = 'exit_code'; var OUTPUT_EXIT_ERROR_KEY = 'exit_error'; -var exit; var done; +var ErrorWithCode = /** @class */ (function (_super) { + __extends(ErrorWithCode, _super); + function ErrorWithCode(message, code) { + var _this = _super.call(this) || this; + _this.message = message; + _this.code = code; + return _this; + } + return ErrorWithCode; +}(Error)); function getExecutable(inputs) { if (!inputs.shell) { return OS === 'win32' ? 'powershell' : 'bash'; @@ -1005,7 +1029,7 @@ function runRetryCmd(inputs) { function runCmd(attempt, inputs) { var _a, _b; return __awaiter(this, void 0, void 0, function () { - var end_time, executable, timeout, child; + var end_time, executable, exit, timeout, child; return __generator(this, function (_c) { switch (_c.label) { case 0: @@ -1055,13 +1079,13 @@ function runCmd(attempt, inputs) { return [4 /*yield*/, (0, util_1.retryWait)(milliseconds_1.default.seconds(inputs.retry_wait_seconds))]; case 5: _c.sent(); - throw new Error("Timeout of ".concat((0, inputs_1.getTimeout)(inputs), "ms hit")); + throw new ErrorWithCode("Timeout of ".concat((0, inputs_1.getTimeout)(inputs), "ms hit"), exit); case 6: if (!(exit > 0)) return [3 /*break*/, 8]; return [4 /*yield*/, (0, util_1.retryWait)(milliseconds_1.default.seconds(inputs.retry_wait_seconds))]; case 7: _c.sent(); - throw new Error("Child_process exited with error code ".concat(exit)); + throw new ErrorWithCode("Child_process exited with error code ".concat(exit), exit); case 8: return [2 /*return*/]; } }); @@ -1069,7 +1093,7 @@ function runCmd(attempt, inputs) { } function runAction(inputs) { return __awaiter(this, void 0, void 0, function () { - var attempt, error_2; + var attempt, error_2, exit; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, (0, inputs_1.validateInputs)(inputs)]; @@ -1091,8 +1115,9 @@ function runAction(inputs) { return [3 /*break*/, 13]; case 5: error_2 = _a.sent(); + exit = error_2 instanceof ErrorWithCode ? error_2.code : 1; if (!(attempt === inputs.max_attempts)) return [3 /*break*/, 6]; - throw new Error("Final attempt failed. ".concat(error_2.message)); + throw new ErrorWithCode("Final attempt failed. ".concat(error_2.message), exit); case 6: if (!(!done && inputs.retry_on === 'error')) return [3 /*break*/, 7]; // error: timeout @@ -1130,8 +1155,7 @@ runAction(inputs) process.exit(0); // success }) .catch(function (err) { - // exact error code if available, otherwise just 1 - var exitCode = exit > 0 ? exit : 1; + var exitCode = err instanceof ErrorWithCode ? err.code : 1; if (inputs.continue_on_error) { (0, core_1.warning)(err.message); } diff --git a/src/index.ts b/src/index.ts index f443451..e477ad2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,9 +11,14 @@ const OUTPUT_TOTAL_ATTEMPTS_KEY = 'total_attempts'; const OUTPUT_EXIT_CODE_KEY = 'exit_code'; const OUTPUT_EXIT_ERROR_KEY = 'exit_error'; -let exit: number; let done: boolean; +class ErrorWithCode extends Error { + constructor(readonly message: string, readonly code: number) { + super(); + } +} + function getExecutable(inputs: Inputs): string { if (!inputs.shell) { return OS === 'win32' ? 'powershell' : 'bash'; @@ -69,7 +74,7 @@ async function runCmd(attempt: number, inputs: Inputs) { const end_time = Date.now() + getTimeout(inputs); const executable = getExecutable(inputs); - exit = 0; + let exit = 0; done = false; let timeout = false; @@ -115,10 +120,10 @@ async function runCmd(attempt: number, inputs: Inputs) { timeout = true; kill(child.pid); await retryWait(ms.seconds(inputs.retry_wait_seconds)); - throw new Error(`Timeout of ${getTimeout(inputs)}ms hit`); + throw new ErrorWithCode(`Timeout of ${getTimeout(inputs)}ms hit`, exit); } else if (exit > 0) { await retryWait(ms.seconds(inputs.retry_wait_seconds)); - throw new Error(`Child_process exited with error code ${exit}`); + throw new ErrorWithCode(`Child_process exited with error code ${exit}`, exit); } else { return; } @@ -136,8 +141,9 @@ async function runAction(inputs: Inputs) { break; // eslint-disable-next-line } catch (error: any) { + const exit = error instanceof ErrorWithCode ? error.code : 1; if (attempt === inputs.max_attempts) { - throw new Error(`Final attempt failed. ${error.message}`); + throw new ErrorWithCode(`Final attempt failed. ${error.message}`, exit); } else if (!done && inputs.retry_on === 'error') { // error: timeout throw error; @@ -166,8 +172,7 @@ runAction(inputs) process.exit(0); // success }) .catch((err) => { - // exact error code if available, otherwise just 1 - const exitCode = exit > 0 ? exit : 1; + const exitCode = err instanceof ErrorWithCode ? err.code : 1; if (inputs.continue_on_error) { warning(err.message);