Skip to content

Commit

Permalink
Replace use of any type
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-evans committed Oct 17, 2022
1 parent 3398cd8 commit 0c74c07
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
18 changes: 14 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ function run() {
}
}
catch (error) {
core.setFailed(error.message);
core.setFailed(utils.getErrorMessage(error));
}
});
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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;


/***/ }),
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ async function run(): Promise<void> {
} else {
core.info('No pull requests found.')
}
} catch (error: any) {
core.setFailed(error.message)
} catch (error) {
core.setFailed(utils.getErrorMessage(error))
}
}

Expand Down
18 changes: 15 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)}`
)
}

Expand All @@ -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)
}

0 comments on commit 0c74c07

Please sign in to comment.