Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -941,8 +956,19 @@ 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;
_this.code = code;
_this.message = message;
return _this;
}
return ErrorWithCode;
}(Error));
function getExecutable(inputs) {
if (!inputs.shell) {
return OS === 'win32' ? 'powershell' : 'bash';
Expand Down Expand Up @@ -1006,7 +1032,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:
Expand Down Expand Up @@ -1056,21 +1082,21 @@ 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*/];
}
});
});
}
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)];
Expand All @@ -1092,8 +1118,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
Expand Down Expand Up @@ -1131,8 +1158,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);
}
Expand Down
21 changes: 14 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ 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();
this.code = code;
this.message = message;
}
}

function getExecutable(inputs: Inputs): string {
if (!inputs.shell) {
return OS === 'win32' ? 'powershell' : 'bash';
Expand Down Expand Up @@ -71,7 +78,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;

Expand Down Expand Up @@ -117,10 +124,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;
}
Expand All @@ -138,8 +145,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;
Expand Down Expand Up @@ -168,8 +176,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);
Expand Down