Skip to content

feat(common): allow passing errorCode in HttpExceptionOptions #15525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions packages/common/exceptions/http.exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface HttpExceptionOptions {
/** original cause of the error */
cause?: unknown;
description?: string;
errorCode?: string;
}

export interface DescriptionAndOptions {
Expand All @@ -30,6 +31,7 @@ export class HttpException extends IntrinsicException {
* It is used when catching and re-throwing an error with a more-specific or useful error message in order to still have access to the original error.
*/
public cause: unknown;
public errorCode?: string;

/**
* Instantiate a plain HTTP Exception.
Expand Down Expand Up @@ -73,6 +75,7 @@ export class HttpException extends IntrinsicException {
this.initMessage();
this.initName();
this.initCause();
this.initErrorCode();
}

/**
Expand All @@ -88,6 +91,12 @@ export class HttpException extends IntrinsicException {
}
}

public initErrorCode(): void {
if (this.options?.errorCode) {
this.errorCode = this.options.errorCode;
}
}

public initMessage() {
if (isString(this.response)) {
this.message = this.response;
Expand Down Expand Up @@ -122,27 +131,42 @@ export class HttpException extends IntrinsicException {
error: string,
statusCode: number,
): HttpExceptionBody;
public static createBody(
message: HttpExceptionBodyMessage,
error: string,
statusCode: number,
errorCode?: string,
): HttpExceptionBody;
public static createBody<Body extends Record<string, unknown>>(
custom: Body,
): Body;
public static createBody<Body extends Record<string, unknown>>(
arg0: null | HttpExceptionBodyMessage | Body,
arg1?: HttpExceptionBodyMessage | string,
statusCode?: number,
errorCode?: string,
): HttpExceptionBody | Body {
if (!arg0) {
return {
const body: HttpExceptionBody = {
message: arg1!,
statusCode: statusCode!,
};
if (errorCode) {
body.errorCode = errorCode;
}
return body;
}

if (isString(arg0) || Array.isArray(arg0) || isNumber(arg0)) {
return {
const body: HttpExceptionBody = {
message: arg0,
error: arg1 as string,
statusCode: statusCode!,
};
if (errorCode) {
body.errorCode = errorCode;
}
return body;
}

return arg0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export type HttpExceptionBodyMessage = string | string[] | number;

export interface HttpExceptionBody {
statusCode: number;
message: HttpExceptionBodyMessage;
error?: string;
statusCode: number;
errorCode?: string;
}
63 changes: 63 additions & 0 deletions packages/common/test/exceptions/http.exception.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
UnprocessableEntityException,
UnsupportedMediaTypeException,
} from '../../exceptions';
import { HttpStatus } from '@nestjs/common';

describe('HttpException', () => {
describe('getResponse', () => {
Expand Down Expand Up @@ -268,4 +269,66 @@ describe('HttpException', () => {
});
});
});

describe('when exception is created with a string and a description', () => {
it('should return a response with a message, error and status code', () => {
const exception = new HttpException('Forbidden', HttpStatus.FORBIDDEN);
expect(exception.getResponse()).to.deep.equal('Forbidden');
});

it('should return a response with a message, error, status code and description', () => {
const exception = new HttpException('Forbidden', HttpStatus.FORBIDDEN, {
description: 'some description',
});
expect(exception.getResponse()).to.deep.equal('Forbidden');
});
});

describe('when exception is created with a string and a cause', () => {
it('should set a cause', () => {
const error = new Error('An internal error cause');
const exception = new HttpException(
'Bad request',
HttpStatus.BAD_REQUEST,
{ cause: error },
);
expect(exception.cause).to.equal(error);
});
});

describe('when exception is created with an errorCode', () => {
it('should set an errorCode', () => {
const exception = new HttpException(
'Bad request',
HttpStatus.BAD_REQUEST,
{
errorCode: 'BAD_REQUEST_CODE',
},
);
expect(exception.errorCode).to.equal('BAD_REQUEST_CODE');
});

it('should be included in the response body when createBody is called', () => {
const body = HttpException.createBody(
'Bad Request',
'Error',
400,
'BAD_REQUEST_CODE',
);
expect(body.errorCode).to.equal('BAD_REQUEST_CODE');
});
});

describe('when exception is thrown', () => {
it('should return a response with a status code and a message', () => {
const exception = new BadRequestException('error');
const response = exception.getResponse();
const message = {
statusCode: 400,
error: 'Bad Request',
message: 'error',
};
expect(message).to.deep.equal(response);
});
});
});