Skip to content

Commit

Permalink
Handle errors as unknown (not any)
Browse files Browse the repository at this point in the history
Typescript started handling `error: unknown` in v4.0. It hadn't been
enforced strictly until now.
  • Loading branch information
Landon Grindheim authored and GitHub committed Aug 30, 2022
1 parent 698d95d commit 47d7a37
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 73 deletions.
8 changes: 8 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@
"@typescript-eslint/no-shadow": "error",
"no-shadow": "off"
},
"overrides": [
{
"files": ["*.ts"],
"rules": {
"no-undef": "off"
}
}
],
"env": {
"node": true,
"es6": true,
Expand Down
16 changes: 11 additions & 5 deletions dist/cleanup/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/cleanup/index.js.map

Large diffs are not rendered by default.

100 changes: 71 additions & 29 deletions dist/main/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/main/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class ApiClient {
}

return res.data.data.attributes.credentials
} catch (error) {
} catch (error: unknown) {
if (axios.isAxiosError(error)) {
const err = error
throw new CredentialFetchingError(
Expand Down
14 changes: 7 additions & 7 deletions src/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ export async function run(cutoff = '24h'): Promise<void> {
await docker.pruneContainers({filters: untilFilter})
await cleanupOldImageVersions(docker, UPDATER_IMAGE_NAME)
await cleanupOldImageVersions(docker, PROXY_IMAGE_NAME)
} catch (error) {
core.error(`Error cleaning up: ${error.message}`)
} catch (error: unknown) {
if (error instanceof Error) {
core.error(`Error cleaning up: ${error.message}`)
}
}
}

Expand Down Expand Up @@ -62,11 +64,9 @@ export async function cleanupOldImageVersions(
core.info(`Removing image ${imageInfo.Id}`)
try {
await docker.getImage(imageInfo.Id).remove()
} catch (error) {
if (error.statusCode === 409) {
core.info(
`Unable to remove ${imageInfo.Id} as it is currently in use`
)
} catch (error: unknown) {
if (error instanceof Error) {
core.info(`Unable to remove ${imageInfo.Id} -- ${error.message}`)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/image-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export const ImageService = {
core.info(`Resolved ${imageName} to existing ${image.RepoDigests}`)
return
} // else fallthrough to pull
} catch (e) {
if (!e.message.includes('no such image')) {
} catch (e: unknown) {
if (e instanceof Error && !e.message.includes('no such image')) {
throw e
} // else fallthrough to pull
}
Expand Down
18 changes: 11 additions & 7 deletions src/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,19 @@ function directoryExistsSync(directoryPath: string): boolean {

try {
stats = fs.statSync(directoryPath)
} catch (error) {
if (error.code === 'ENOENT') {
return stats.isDirectory()
} catch (error: unknown) {
if (isNodeError(error) && error.code === 'ENOENT') {
return false
} else if (error instanceof Error) {
throw new Error(
`Encountered an error when checking whether path '${directoryPath}' exists: ${error.message}`
)
}

throw new Error(
`Encountered an error when checking whether path '${directoryPath}' exists: ${error.message}`
)
}
return false
}

return stats.isDirectory()
function isNodeError(error: any): error is NodeJS.ErrnoException {
return error instanceof Error
}
Loading

0 comments on commit 47d7a37

Please sign in to comment.