Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace use of any type #123

Merged
merged 1 commit into from Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 14 additions & 4 deletions dist/index.js
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
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
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)
}