diff --git a/event-manager/src/__tests__/event.helper.spec.ts b/event-manager/src/__tests__/event.helper.spec.ts index 05fab0e..87ffa9d 100644 --- a/event-manager/src/__tests__/event.helper.spec.ts +++ b/event-manager/src/__tests__/event.helper.spec.ts @@ -1,6 +1,7 @@ -import { describe, expect, it, jest } from "@jest/globals"; -import { getLogLevelByStatus } from "../event.helper.js"; -import { HttpStatus } from "@nestjs/common"; +import { describe, expect, it, jest } from '@jest/globals'; +import { getLogLevelByStatus, isErrorEvent } from '../event.helper.js'; +import { HttpException, HttpStatus } from '@nestjs/common'; +import { DefaultError } from '@nestjs-yalc/errors/default.error.js'; describe('EventHelper', () => { it('should return the correct log level', () => { @@ -9,4 +10,16 @@ describe('EventHelper', () => { expect(getLogLevelByStatus(HttpStatus.BAD_REQUEST)).toBe('log'); expect(getLogLevelByStatus(200)).toBe('log'); }); -}) + + it('should return true if the event is an error event', () => { + expect(isErrorEvent({})).toBeFalsy(); + expect(isErrorEvent({ errorClass: true })).toBeTruthy(); + expect(isErrorEvent({ errorClass: DefaultError })).toBeTruthy(); + expect(isErrorEvent({ errorClass: HttpException })).toBeFalsy(); + expect( + isErrorEvent({ + errorClass: new HttpException('test', HttpStatus.BAD_REQUEST), + }), + ).toBeFalsy(); + }); +}); diff --git a/event-manager/src/event.helper.ts b/event-manager/src/event.helper.ts index 64c6b47..57661eb 100644 --- a/event-manager/src/event.helper.ts +++ b/event-manager/src/event.helper.ts @@ -1,6 +1,9 @@ +import { DefaultError } from '@nestjs-yalc/errors/default.error.js'; import { LogLevelEnum } from '@nestjs-yalc/logger/logger.enum.js'; +import { isClass } from '@nestjs-yalc/utils/class.helper.js'; import { HttpStatus } from '@nestjs/common'; import { LogLevel } from 'typeorm'; +import { IErrorEventOptions } from './event.js'; export function getLogLevelByStatus(statusCode: number) { let loggerLevel: LogLevel; @@ -19,3 +22,18 @@ export function getLogLevelByStatus(statusCode: number) { return loggerLevel; } + +export function isErrorEvent(options: IErrorEventOptions) { + if (!options.errorClass) { + return false; + } + + return ( + options.errorClass === true || + isClass(options.errorClass, DefaultError.name) || + (!isClass(options.errorClass) && + options.errorClass.getStatus && + getLogLevelByStatus(options.errorClass.getStatus()) === + LogLevelEnum.ERROR) + ); +} diff --git a/utils/src/__tests__/class.helper.spec.ts b/utils/src/__tests__/class.helper.spec.ts index 86da79c..ee58d32 100644 --- a/utils/src/__tests__/class.helper.spec.ts +++ b/utils/src/__tests__/class.helper.spec.ts @@ -7,6 +7,7 @@ import { beforeAll, beforeEach, } from '@jest/globals'; +import { DefaultError } from '@nestjs-yalc/errors/default.error.js'; describe('Test Class Helpers', () => { it('should test a class correctly', () => { @@ -24,4 +25,11 @@ describe('Test Class Helpers', () => { it('expect an instance of a class to not be a class', () => { expect(isClass(new (class {})())).toBeFalsy(); }); + + it('should test a class against a name', () => { + const defaultError = DefaultError; + expect(isClass(class Test {}, 'Test')).toBeTruthy(); + expect(isClass(class Test {}, 'Test2')).toBeFalsy(); + expect(isClass(defaultError, DefaultError.name)).toBeTruthy(); + }); }); diff --git a/utils/src/class.helper.ts b/utils/src/class.helper.ts index 084bef0..ae2b069 100644 --- a/utils/src/class.helper.ts +++ b/utils/src/class.helper.ts @@ -1,7 +1,9 @@ import { ClassType } from '@nestjs-yalc/types/globals.d.js'; -export function isClass(func: any): func is ClassType { +export function isClass(func: any, className?: string): func is ClassType { return ( - typeof func === 'function' && /^class\s/.test(func.toString.call(func)) + typeof func === 'function' && + /^class\s/.test(func.toString.call(func)) && + (className ? func.name === className : true) ); }