From 96d5b4182c99296e1c133fe4966576f2de1fe134 Mon Sep 17 00:00:00 2001 From: TESTELIN Geoffrey Date: Sat, 6 Nov 2021 13:13:26 +0100 Subject: [PATCH] feat(error): catch and stop the action in case of errors --- src/core/stale.service.spec.ts | 108 +++++++++++++++++++++++++++++++++ src/core/stale.service.ts | 21 ++++++- 2 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 src/core/stale.service.spec.ts diff --git a/src/core/stale.service.spec.ts b/src/core/stale.service.spec.ts new file mode 100644 index 000000000..aed037b59 --- /dev/null +++ b/src/core/stale.service.spec.ts @@ -0,0 +1,108 @@ +import { InputsService } from './inputs/inputs.service'; +import { StaleService } from './stale.service'; +import { LoggerService } from '../utils/logger/logger.service'; +import * as core from '@actions/core'; + +describe(`StaleService`, (): void => { + describe(`initialize()`, (): void => { + let inputsServiceInitializeSpy: jest.SpyInstance; + let coreSetFailedSpy: jest.SpyInstance; + let loggerServiceErrorSpy: jest.SpyInstance; + let loggerServiceDebugSpy: jest.SpyInstance; + + beforeEach((): void => { + inputsServiceInitializeSpy = jest.spyOn(InputsService, `initialize`).mockImplementation(); + coreSetFailedSpy = jest.spyOn(core, `setFailed`).mockImplementation(); + loggerServiceErrorSpy = jest.spyOn(LoggerService, `error`).mockImplementation(); + loggerServiceDebugSpy = jest.spyOn(LoggerService, `debug`).mockImplementation(); + }); + + it(`should read and parse the inputs from the job`, (): void => { + expect.assertions(2); + + StaleService.initialize(); + + expect(inputsServiceInitializeSpy).toHaveBeenCalledTimes(1); + expect(inputsServiceInitializeSpy).toHaveBeenCalledWith(); + }); + + describe(`when an error occur`, (): void => { + beforeEach((): void => { + inputsServiceInitializeSpy.mockImplementation((): void => { + throw new Error(`inputs error`); + }); + }); + + describe(`when the error is an Error object`, (): void => { + beforeEach((): void => { + inputsServiceInitializeSpy.mockImplementation((): void => { + throw new Error(`inputs error`); + }); + }); + + it(`should log the error`, (): void => { + expect.assertions(2); + + StaleService.initialize(); + + expect(loggerServiceErrorSpy).toHaveBeenCalledTimes(1); + expect(loggerServiceErrorSpy).toHaveBeenCalledWith(`[Error] inputs error`); + }); + + it(`should log the error trace`, (): void => { + expect.assertions(3); + + StaleService.initialize(); + + expect(loggerServiceDebugSpy).toHaveBeenCalledTimes(1); + expect(loggerServiceDebugSpy.mock.calls[0][0]).toContain(`Error: inputs error`); + expect(loggerServiceDebugSpy.mock.calls[0]).toHaveLength(1); + }); + + it(`should exit the action with the thrown error`, (): void => { + expect.assertions(2); + + StaleService.initialize(); + + expect(coreSetFailedSpy).toHaveBeenCalledTimes(1); + expect(coreSetFailedSpy).toHaveBeenCalledWith(`Stale action failed with error inputs error`); + }); + }); + + describe(`when the error is not an Error object`, (): void => { + beforeEach((): void => { + inputsServiceInitializeSpy.mockImplementation((): void => { + // eslint-disable-next-line no-throw-literal + throw `inputs error`; + }); + }); + + it(`should log the error`, (): void => { + expect.assertions(2); + + StaleService.initialize(); + + expect(loggerServiceErrorSpy).toHaveBeenCalledTimes(1); + expect(loggerServiceErrorSpy).toHaveBeenCalledWith(`Stale action failed with error inputs error`); + }); + + it(`should exit the action with the thrown error`, (): void => { + expect.assertions(2); + + StaleService.initialize(); + + expect(coreSetFailedSpy).toHaveBeenCalledTimes(1); + expect(coreSetFailedSpy).toHaveBeenCalledWith(`Stale action failed with error inputs error`); + }); + }); + }); + + it(`should return the service`, (): void => { + expect.assertions(1); + + const result = StaleService.initialize(); + + expect(result).toStrictEqual(StaleService); + }); + }); +}); diff --git a/src/core/stale.service.ts b/src/core/stale.service.ts index 1279fd143..3b31114fe 100644 --- a/src/core/stale.service.ts +++ b/src/core/stale.service.ts @@ -1,8 +1,27 @@ import { InputsService } from './inputs/inputs.service'; +import { LoggerService } from '../utils/logger/logger.service'; +import * as core from '@actions/core'; export class StaleService { public static initialize(): StaleService { - InputsService.initialize(); + try { + InputsService.initialize(); + } catch (error: unknown) { + if (error instanceof Error) { + LoggerService.error(`[${error.name}] ${error.message}`); + + if (error.stack) { + LoggerService.debug(error.stack); + } + + core.setFailed(`Stale action failed with error ${error.message}`); + } else { + const errorMessage = `Stale action failed with error ${error}`; + + LoggerService.error(errorMessage); + core.setFailed(errorMessage); + } + } return StaleService; }