Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Handle errors as unknown (not any)
Typescript started handling `error: unknown` in v4.0. It hadn't been
enforced strictly until now.
Landon Grindheim authored and GitHub committed Aug 30, 2022

Unverified

No user is associated with the committer email.
1 parent 698d95d commit 47d7a37
Showing 10 changed files with 137 additions and 73 deletions.
8 changes: 8 additions & 0 deletions .eslintrc.json
@@ -46,6 +46,14 @@
"@typescript-eslint/no-shadow": "error",
"no-shadow": "off"
},
"overrides": [
{
"files": ["*.ts"],
"rules": {
"no-undef": "off"
}
}
],
"env": {
"node": true,
"es6": true,
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
@@ -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(
14 changes: 7 additions & 7 deletions src/cleanup.ts
@@ -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}`)
}
}
}

@@ -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}`)
}
}
}
4 changes: 2 additions & 2 deletions src/image-service.ts
@@ -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
}
18 changes: 11 additions & 7 deletions src/inputs.ts
@@ -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
}
44 changes: 24 additions & 20 deletions src/main.ts
@@ -59,22 +59,24 @@ export async function run(context: Context): Promise<void> {
try {
await ImageService.pull(UPDATER_IMAGE_NAME)
await ImageService.pull(PROXY_IMAGE_NAME)
} catch (error: any) {
await failJob(
apiClient,
'Error fetching updater images',
error,
DependabotErrorType.Image
)
return
} catch (error: unknown) {
if (error instanceof Error) {
await failJob(
apiClient,
'Error fetching updater images',
error,
DependabotErrorType.Image
)
return
}
}
core.endGroup()

try {
core.info('Starting update process')

await updater.runUpdater()
} catch (error: any) {
} catch (error: unknown) {
// If we have encountered a UpdaterFetchError, the Updater will already have
// reported the error and marked the job as processed, so we only need to
// set an exit status.
@@ -85,7 +87,7 @@ export async function run(context: Context): Promise<void> {
)
botSay('finished: unable to fetch files')
return
} else {
} else if (error instanceof Error) {
await failJob(
apiClient,
'Dependabot encountered an error performing the update',
@@ -96,15 +98,15 @@ export async function run(context: Context): Promise<void> {
}
}
botSay('finished')
} catch (error: any) {
} catch (error: unknown) {
if (error instanceof CredentialFetchingError) {
await failJob(
apiClient,
'Dependabot was unable to retrieve job credentials',
error,
DependabotErrorType.UpdateRun
)
} else {
} else if (error instanceof Error) {
await failJob(
apiClient,
'Dependabot was unable to start the update',
@@ -114,14 +116,16 @@ export async function run(context: Context): Promise<void> {

return
}
} catch (error) {
// If we've reached this point, we do not have a viable
// API client to report back to Dependabot API.
//
// We output the raw error in the Action logs and defer
// to workflow_run monitoring to detect the job failure.
setFailed('Dependabot encountered an unexpected problem', error)
botSay('finished: unexpected error')
} catch (error: unknown) {
if (error instanceof Error) {
// If we've reached this point, we do not have a viable
// API client to report back to Dependabot API.
//
// We output the raw error in the Action logs and defer
// to workflow_run monitoring to detect the job failure.
setFailed('Dependabot encountered an unexpected problem', error)
botSay('finished: unexpected error')
}
}
}

0 comments on commit 47d7a37

Please sign in to comment.