diff --git a/dist/index.js b/dist/index.js index 0900b2d..9bb1efa 100644 --- a/dist/index.js +++ b/dist/index.js @@ -436,7 +436,7 @@ function run() { } } catch (error) { - core.setFailed(error.message); + core.setFailed(utils.getErrorMessage(error)); } }); } @@ -738,7 +738,7 @@ var __importStar = (this && this.__importStar) || function (mod) { return result; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.getInputAsArray = exports.fileExistsSync = void 0; +exports.getErrorMessage = exports.getInputAsArray = exports.fileExistsSync = void 0; const core = __importStar(__nccwpck_require__(2186)); const fs = __importStar(__nccwpck_require__(5747)); function fileExistsSync(path) { @@ -750,10 +750,10 @@ function fileExistsSync(path) { stats = fs.statSync(path); } catch (error) { - if (error.code === 'ENOENT') { + if (hasErrorCode(error) && error.code === 'ENOENT') { return false; } - throw new Error(`Encountered an error when checking whether path '${path}' exists: ${error.message}`); + throw new Error(`Encountered an error when checking whether path '${path}' exists: ${getErrorMessage(error)}`); } if (!stats.isDirectory()) { return true; @@ -771,6 +771,16 @@ function getStringAsArray(str) { .map(s => s.trim()) .filter(x => x !== ''); } +/* eslint-disable @typescript-eslint/no-explicit-any */ +function hasErrorCode(error) { + return typeof (error && error.code) === 'string'; +} +function getErrorMessage(error) { + if (error instanceof Error) + return error.message; + return String(error); +} +exports.getErrorMessage = getErrorMessage; /***/ }), diff --git a/src/main.ts b/src/main.ts index bf63708..a4e2bb1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -67,8 +67,8 @@ async function run(): Promise { } else { core.info('No pull requests found.') } - } catch (error: any) { - core.setFailed(error.message) + } catch (error) { + core.setFailed(utils.getErrorMessage(error)) } } diff --git a/src/utils.ts b/src/utils.ts index 2509712..52b8f48 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -9,13 +9,15 @@ export function fileExistsSync(path: string): boolean { let stats: fs.Stats try { stats = fs.statSync(path) - } catch (error: any) { - if (error.code === 'ENOENT') { + } catch (error) { + if (hasErrorCode(error) && error.code === 'ENOENT') { return false } throw new Error( - `Encountered an error when checking whether path '${path}' exists: ${error.message}` + `Encountered an error when checking whether path '${path}' exists: ${getErrorMessage( + error + )}` ) } @@ -39,3 +41,13 @@ function getStringAsArray(str: string): string[] { .map(s => s.trim()) .filter(x => x !== '') } + +/* eslint-disable @typescript-eslint/no-explicit-any */ +function hasErrorCode(error: any): error is {code: string} { + return typeof (error && error.code) === 'string' +} + +export function getErrorMessage(error: unknown): string { + if (error instanceof Error) return error.message + return String(error) +}