diff --git a/sources/src/dependency-submission/main.ts b/sources/src/dependency-submission/main.ts index c9e99a99..0e560db1 100644 --- a/sources/src/dependency-submission/main.ts +++ b/sources/src/dependency-submission/main.ts @@ -1,5 +1,3 @@ -import * as core from '@actions/core' - import * as setupGradle from '../setup-gradle' import * as gradle from '../execution/gradle' import * as dependencyGraph from '../dependency-graph' @@ -14,6 +12,7 @@ import { setActionId } from '../configuration' import {saveDeprecationState} from '../deprecation-collector' +import {handleMainActionError} from '../errors' /** * The main entry point for the action, called by Github Actions for the step. @@ -56,10 +55,7 @@ export async function run(): Promise { saveDeprecationState() } catch (error) { - core.setFailed(String(error)) - if (error instanceof Error && error.stack) { - core.info(error.stack) - } + handleMainActionError(error) } // Explicit process.exit() to prevent waiting for hanging promises. diff --git a/sources/src/dependency-submission/post.ts b/sources/src/dependency-submission/post.ts index 50ab70e9..743478e5 100644 --- a/sources/src/dependency-submission/post.ts +++ b/sources/src/dependency-submission/post.ts @@ -1,13 +1,12 @@ -import * as core from '@actions/core' import * as setupGradle from '../setup-gradle' import {CacheConfig, SummaryConfig} from '../configuration' -import {PostActionJobFailure} from '../errors' +import {handlePostActionError} from '../errors' // Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in // @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to // throw an uncaught exception. Instead of failing this action, just warn. -process.on('uncaughtException', e => handleFailure(e)) +process.on('uncaughtException', e => handlePostActionError(e)) /** * The post-execution entry point for the action, called by Github Actions after completing all steps for the Job. @@ -16,22 +15,11 @@ export async function run(): Promise { try { await setupGradle.complete(new CacheConfig(), new SummaryConfig()) } catch (error) { - if (error instanceof PostActionJobFailure) { - core.setFailed(String(error)) - } else { - handleFailure(error) - } + handlePostActionError(error) } // Explicit process.exit() to prevent waiting for promises left hanging by `@actions/cache` on save. process.exit() } -function handleFailure(error: unknown): void { - core.warning(`Unhandled error in Gradle post-action - job will continue: ${error}`) - if (error instanceof Error && error.stack) { - core.info(error.stack) - } -} - run() diff --git a/sources/src/errors.ts b/sources/src/errors.ts index 5236ec4e..ad2e1e21 100644 --- a/sources/src/errors.ts +++ b/sources/src/errors.ts @@ -1,3 +1,5 @@ +import * as core from '@actions/core' + export class PostActionJobFailure extends Error { constructor(error: unknown) { if (error instanceof Error) { @@ -9,3 +11,31 @@ export class PostActionJobFailure extends Error { } } } + +export function handleMainActionError(error: unknown): void { + if (error instanceof AggregateError) { + core.setFailed(`Multiple errors returned`) + for (const err of error.errors) { + core.error(`Error ${error.errors.indexOf(err)}: ${err.message}`) + if (err.stack) { + core.info(err.stack) + } + } + } else { + core.setFailed(String(error)) + if (error instanceof Error && error.stack) { + core.info(error.stack) + } + } +} + +export function handlePostActionError(error: unknown): void { + if (error instanceof PostActionJobFailure) { + core.setFailed(String(error)) + } else { + core.warning(`Unhandled error in Gradle post-action - job will continue: ${error}`) + if (error instanceof Error && error.stack) { + core.info(error.stack) + } + } +} diff --git a/sources/src/setup-gradle/main.ts b/sources/src/setup-gradle/main.ts index 1cada07d..c66c47ec 100644 --- a/sources/src/setup-gradle/main.ts +++ b/sources/src/setup-gradle/main.ts @@ -1,5 +1,3 @@ -import * as core from '@actions/core' - import * as setupGradle from '../setup-gradle' import * as gradle from '../execution/gradle' import * as dependencyGraph from '../dependency-graph' @@ -12,6 +10,7 @@ import { setActionId } from '../configuration' import {recordDeprecation, saveDeprecationState} from '../deprecation-collector' +import {handleMainActionError} from '../errors' /** * The main entry point for the action, called by Github Actions for the step. @@ -41,10 +40,7 @@ export async function run(): Promise { saveDeprecationState() } catch (error) { - core.setFailed(String(error)) - if (error instanceof Error && error.stack) { - core.info(error.stack) - } + handleMainActionError(error) } // Explicit process.exit() to prevent waiting for hanging promises. diff --git a/sources/src/setup-gradle/post.ts b/sources/src/setup-gradle/post.ts index 4f7b9d4e..b39f77d1 100644 --- a/sources/src/setup-gradle/post.ts +++ b/sources/src/setup-gradle/post.ts @@ -1,15 +1,14 @@ -import * as core from '@actions/core' import * as setupGradle from '../setup-gradle' import * as dependencyGraph from '../dependency-graph' import {CacheConfig, DependencyGraphConfig, SummaryConfig} from '../configuration' -import {PostActionJobFailure} from '../errors' +import {handlePostActionError} from '../errors' import {emitDeprecationWarnings, restoreDeprecationState} from '../deprecation-collector' // Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in // @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to // throw an uncaught exception. Instead of failing this action, just warn. -process.on('uncaughtException', e => handleFailure(e)) +process.on('uncaughtException', e => handlePostActionError(e)) /** * The post-execution entry point for the action, called by Github Actions after completing all steps for the Job. @@ -24,22 +23,11 @@ export async function run(): Promise { await dependencyGraph.complete(new DependencyGraphConfig()) } } catch (error) { - if (error instanceof PostActionJobFailure) { - core.setFailed(String(error)) - } else { - handleFailure(error) - } + handlePostActionError(error) } // Explicit process.exit() to prevent waiting for promises left hanging by `@actions/cache` on save. process.exit() } -function handleFailure(error: unknown): void { - core.warning(`Unhandled error in Gradle post-action - job will continue: ${error}`) - if (error instanceof Error && error.stack) { - core.info(error.stack) - } -} - run() diff --git a/sources/src/wrapper-validation/main.ts b/sources/src/wrapper-validation/main.ts index 9fb30705..65236580 100644 --- a/sources/src/wrapper-validation/main.ts +++ b/sources/src/wrapper-validation/main.ts @@ -2,6 +2,7 @@ import * as path from 'path' import * as core from '@actions/core' import * as validate from './validate' +import {handleMainActionError} from '../errors' export async function run(): Promise { try { @@ -22,16 +23,7 @@ export async function run(): Promise { } } } catch (error) { - if (error instanceof AggregateError) { - core.setFailed(`Multiple errors returned`) - for (const err of error.errors) { - core.error(`Error ${error.errors.indexOf(err)}: ${err.message}`) - } - } else if (error instanceof Error) { - core.setFailed(error.message) - } else { - core.setFailed(`Unknown object was thrown: ${error}`) - } + handleMainActionError(error) } }