Skip to content

Commit

Permalink
feat: improve status code checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Yehonal committed Sep 25, 2024
1 parent 9a7efbd commit d6d4558
Show file tree
Hide file tree
Showing 15 changed files with 251 additions and 185 deletions.
8 changes: 1 addition & 7 deletions app/src/__tests__/app.helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,19 @@ import {
describe,
it,
beforeEach,
beforeAll,
afterAll,
afterEach,
} from '@jest/globals';

import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { DynamicModule, INestApplicationContext, Module } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import {
curriedExecuteStandaloneFunction,
executeStandaloneFunction,
} from '../app.helper.js';
import { executeStandaloneFunction } from '../app.helper.js';
import {
getBootstrappedApps,
getMainBootstrappedApp,
} from '../app-bootstrap-base.helper.js';
import { AppBootstrap } from '../app-bootstrap.helper.js';
import { yalcBaseAppModuleMetadataFactory } from '../base-app-module.helper.js';
import { EventModule } from '@nestjs-yalc/event-manager/event.module.js';

@Module(
yalcBaseAppModuleMetadataFactory(TestModule1, 'test1', {
Expand Down
20 changes: 19 additions & 1 deletion errors/src/__tests__/default.error.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ import {
isDefaultErrorMixin,
DefaultErrorBase,
errorToDefaultError,
isDefaultErrorMixinClass,
} from '../default.error.js';
import { EventEmitter2 } from '@nestjs/event-emitter';
import EventEmitter from 'events';
import { ForbiddenException, HttpException, HttpStatus } from '@nestjs/common';
import {
BadRequestException,
ForbiddenException,
HttpException,
HttpStatus,
} from '@nestjs/common';
import { getHttpStatusDescription } from '@nestjs-yalc/utils/http.helper.js';

describe('DefaultErrorMixin', () => {
Expand Down Expand Up @@ -249,6 +255,18 @@ describe('DefaultError', () => {
});
});

describe('isDefaultErrorMixinClass', () => {
it('should check if an error is not of DefaultMixin type', () => {
const check = isDefaultErrorMixinClass(BadRequestException);
expect(check).toBeFalsy();
});

it('should check if an error is of DefaultMixin type', () => {
const check = isDefaultErrorMixinClass(DefaultError);
expect(check).toBeTruthy();
});
});

describe('error cause', () => {
it('should return if no cause defined', () => {
const check = new DefaultError('test', { cause: undefined });
Expand Down
23 changes: 23 additions & 0 deletions errors/src/__tests__/error.helper.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, expect, it } from '@jest/globals';
import { getStatusCodeFromError } from '../error.helper.js';
import { BadRequestException, HttpStatus } from '@nestjs/common';
import { BadRequestError } from '../error.class.js';

describe('ErrorHelper', () => {
it('should return the correct status code', () => {
expect(getStatusCodeFromError(BadRequestError)).toBe(
HttpStatus.BAD_REQUEST,
);

expect(getStatusCodeFromError(new BadRequestError())).toBe(
HttpStatus.BAD_REQUEST,
);

expect(getStatusCodeFromError(new Error())).toBe(null);
expect(getStatusCodeFromError(Error)).toBe(null);

expect(getStatusCodeFromError(BadRequestException)).toBe(
HttpStatus.BAD_REQUEST,
);
});
});
4 changes: 2 additions & 2 deletions errors/src/__tests__/verification.error.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import {
HttpStatusCodes,
} from '@nestjs-yalc/utils/http.helper.js';
import { HttpException } from '@nestjs/common';
import { AdditionalVerificationNeededException } from '../index.js';
import { AdditionalVerificationNeededError } from '../index.js';

describe('Verification error', () => {
const error = new AdditionalVerificationNeededException();
const error = new AdditionalVerificationNeededError();

it('should be defined', () => {
expect(error).toBeDefined();
Expand Down
26 changes: 20 additions & 6 deletions errors/src/default.error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { EventEmitter2 } from '@nestjs/event-emitter';
import { getHttpStatusNameByCode } from './error.enum.js';
import { deepMergeWithoutArrayConcat } from '@nestjs-yalc/utils/object.helper.js';
import { isClass } from '@nestjs-yalc/utils/class.helper.js';

export const ON_DEFAULT_ERROR_EVENT = 'onDefaultError';

Expand Down Expand Up @@ -217,12 +218,6 @@ export const newDefaultError = <
return new (DefaultErrorMixin(base))(options, ...args);
};

export function isDefaultErrorMixin(
error: any,
): error is IAbstractDefaultError {
return (error as any).__DefaultErrorMixin !== undefined;
}

interface IFormattedCause {
message?: string;
stack?: string;
Expand Down Expand Up @@ -255,6 +250,8 @@ export const DefaultErrorMixin = <
extends BaseClass
implements IAbstractDefaultError
{
static defaultStatusCode = HttpStatus.INTERNAL_SERVER_ERROR;

data?: any;
description?: string;
internalMessage?: string;
Expand Down Expand Up @@ -499,6 +496,7 @@ export function DefaultErrorBase<
T extends ClassType<HttpException> = ClassType<HttpException>,
>(base?: T): IDefaultErrorBaseConstructor<T> {
return class extends DefaultErrorMixin(base ?? HttpException) {
static defaultStatusCode = HttpStatus.INTERNAL_SERVER_ERROR;
constructor(
internalMessage?: string,
/**
Expand All @@ -517,6 +515,7 @@ export function DefaultErrorBase<
* It extends the HttpException class and the IAbstractDefaultError interface.
*/
export class DefaultError extends DefaultErrorBase(HttpException) {
static defaultStatusCode = HttpStatus.INTERNAL_SERVER_ERROR;
constructor(
internalMessage?: string,
/**
Expand Down Expand Up @@ -571,3 +570,18 @@ export const errorToDefaultError = (
...options,
});
};

export function isDefaultErrorMixin(
error: any,
): error is IAbstractDefaultError {
return (error as any).__DefaultErrorMixin !== undefined;
}

export function isDefaultErrorMixinClass(
error: any,
): error is typeof DefaultError {
return (
isClass(error) &&
(error as typeof DefaultError).defaultStatusCode !== undefined
);
}
Loading

0 comments on commit d6d4558

Please sign in to comment.