From b0d8b47eb78066c6d92836871c01e5654ae19a0e Mon Sep 17 00:00:00 2001 From: Brian DeHamer Date: Thu, 9 May 2024 12:34:01 -0700 Subject: [PATCH] include more detail in error logging (#58) Signed-off-by: Brian DeHamer --- __tests__/main.test.ts | 12 +++++++----- dist/index.js | 10 ++++++++-- src/main.ts | 13 +++++++++++-- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 3d6668c..653c43d 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -116,7 +116,9 @@ describe('action', () => { expect(runMock).toHaveReturned() expect(setFailedMock).toHaveBeenCalledWith( - expect.stringMatching(/missing "id-token" permission/) + new Error( + 'missing "id-token" permission. Please add "permissions: id-token: write" to your workflow.' + ) ) }) }) @@ -131,9 +133,7 @@ describe('action', () => { expect(runMock).toHaveReturned() expect(setFailedMock).toHaveBeenCalledWith( - expect.stringMatching( - /one of subject-path or subject-digest must be provided/i - ) + new Error('One of subject-path or subject-digest must be provided') ) }) }) @@ -330,7 +330,9 @@ describe('action', () => { expect(runMock).toHaveReturned() expect(setFailedMock).toHaveBeenCalledWith( - 'Too many subjects specified. The maximum number of subjects is 64.' + new Error( + 'Too many subjects specified. The maximum number of subjects is 64.' + ) ) }) }) diff --git a/dist/index.js b/dist/index.js index c500d4f..6138928 100644 --- a/dist/index.js +++ b/dist/index.js @@ -79849,6 +79849,7 @@ const endpoints_1 = __nccwpck_require__(69112); const predicate_1 = __nccwpck_require__(72103); const subject_1 = __nccwpck_require__(95206); const COLOR_CYAN = '\x1B[36m'; +const COLOR_GRAY = '\x1B[38;5;244m'; const COLOR_DEFAULT = '\x1B[39m'; const ATTESTATION_FILE_NAME = 'attestation.jsonl'; const MAX_SUBJECT_COUNT = 64; @@ -79901,11 +79902,12 @@ async function run() { } catch (err) { // Fail the workflow run if an error occurs - core.setFailed(err instanceof Error ? err.message : /* istanbul ignore next */ `${err}`); + core.setFailed(err instanceof Error ? err : /* istanbul ignore next */ `${err}`); + // Log the cause of the error if one is available /* istanbul ignore if */ if (err instanceof Error && 'cause' in err) { const innerErr = err.cause; - core.debug(innerErr instanceof Error ? innerErr.message : `${innerErr}}`); + core.info(mute(innerErr instanceof Error ? innerErr.toString() : `${innerErr}`)); } } } @@ -79951,7 +79953,11 @@ const createAttestation = async (subject, predicate, sigstoreInstance) => { } return attestation; }; +// Emphasis string using ANSI color codes const highlight = (str) => `${COLOR_CYAN}${str}${COLOR_DEFAULT}`; +// De-emphasize string using ANSI color codes +/* istanbul ignore next */ +const mute = (str) => `${COLOR_GRAY}${str}${COLOR_DEFAULT}`; const tempDir = () => { const basePath = process.env['RUNNER_TEMP']; /* istanbul ignore if */ diff --git a/src/main.ts b/src/main.ts index aea3f7c..158746a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -13,6 +13,7 @@ type SigstoreInstance = 'public-good' | 'github' type AttestedSubject = { subject: Subject; attestationID: string } const COLOR_CYAN = '\x1B[36m' +const COLOR_GRAY = '\x1B[38;5;244m' const COLOR_DEFAULT = '\x1B[39m' const ATTESTATION_FILE_NAME = 'attestation.jsonl' @@ -86,13 +87,16 @@ export async function run(): Promise { } catch (err) { // Fail the workflow run if an error occurs core.setFailed( - err instanceof Error ? err.message : /* istanbul ignore next */ `${err}` + err instanceof Error ? err : /* istanbul ignore next */ `${err}` ) + // Log the cause of the error if one is available /* istanbul ignore if */ if (err instanceof Error && 'cause' in err) { const innerErr = err.cause - core.debug(innerErr instanceof Error ? innerErr.message : `${innerErr}}`) + core.info( + mute(innerErr instanceof Error ? innerErr.toString() : `${innerErr}`) + ) } } } @@ -156,8 +160,13 @@ const createAttestation = async ( return attestation } +// Emphasis string using ANSI color codes const highlight = (str: string): string => `${COLOR_CYAN}${str}${COLOR_DEFAULT}` +// De-emphasize string using ANSI color codes +/* istanbul ignore next */ +const mute = (str: string): string => `${COLOR_GRAY}${str}${COLOR_DEFAULT}` + const tempDir = (): string => { const basePath = process.env['RUNNER_TEMP']