feat(common): add error cause option

add error cause option to ConflictException.
This commit is contained in:
Thiago Martins
2022-10-27 16:16:03 -03:00
parent 472c711127
commit 88ac6aa463
2 changed files with 17 additions and 5 deletions

View File

@@ -1,5 +1,5 @@
import { HttpStatus } from '../enums/http-status.enum';
import { HttpException } from './http.exception';
import { HttpException, HttpExceptionOptions } from './http.exception';
/**
* Defines an HTTP exception for *Conflict* type errors.
@@ -18,7 +18,7 @@ export class ConflictException extends HttpException {
* @usageNotes
* The HTTP response status code will be 409.
* - The `objectOrError` argument defines the JSON response body or the message string.
* - The `description` argument contains a short description of the HTTP error.
* - The `descriptionOrOptions` argument contains either a short description of the HTTP error or an options object used to provide an underlying error cause.
*
* By default, the JSON response body contains two properties:
* - `statusCode`: this will be the value 409.
@@ -31,12 +31,19 @@ export class ConflictException extends HttpException {
* and return it as the JSON response body.
*
* @param objectOrError string or object describing the error condition.
* @param description a short description of the HTTP error.
* @param descriptionOrOptions either a short description of the HTTP error or an options object used to provide an underlying error cause
*/
constructor(objectOrError?: string | object | any, description = 'Conflict') {
constructor(
objectOrError?: string | object | any,
descriptionOrOptions: string | HttpExceptionOptions = 'Conflict',
) {
const { description, httpExceptionOptions } =
HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions);
super(
HttpException.createBody(objectOrError, description, HttpStatus.CONFLICT),
HttpStatus.CONFLICT,
httpExceptionOptions,
);
}
}

View File

@@ -2,6 +2,7 @@ import { expect } from 'chai';
import {
BadGatewayException,
BadRequestException,
ConflictException,
HttpException,
NotFoundException,
} from '../../exceptions';
@@ -176,7 +177,11 @@ describe('HttpException', () => {
});
it('configures a cause when using a bult-in exception with options', () => {
const builInErrorClasses = [BadGatewayException, BadRequestException];
const builInErrorClasses = [
BadGatewayException,
BadRequestException,
ConflictException,
];
builInErrorClasses.forEach(ExceptionClass => {
const error = new ExceptionClass(customDescription, {