Skip to content

Commit

Permalink
feat(error): catch and stop the action in case of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
C0ZEN committed Nov 6, 2021
1 parent 161786e commit 96d5b41
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 1 deletion.
108 changes: 108 additions & 0 deletions src/core/stale.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
21 changes: 20 additions & 1 deletion src/core/stale.service.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down

0 comments on commit 96d5b41

Please sign in to comment.