diff --git a/packages/common/exceptions/bad-gateway.exception.ts b/packages/common/exceptions/bad-gateway.exception.ts index 5bb9cd156a3..f138adf96fc 100644 --- a/packages/common/exceptions/bad-gateway.exception.ts +++ b/packages/common/exceptions/bad-gateway.exception.ts @@ -37,13 +37,13 @@ export class BadGatewayException extends HttpException { objectOrError?: any, descriptionOrOptions: string | HttpExceptionOptions = 'Bad Gateway', ) { - const { description, httpExceptionOptions } = + const { description = 'Bad Gateway', httpExceptionOptions } = HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions); super( HttpException.createBody( objectOrError, - description!, + description, HttpStatus.BAD_GATEWAY, ), HttpStatus.BAD_GATEWAY, diff --git a/packages/common/exceptions/bad-request.exception.ts b/packages/common/exceptions/bad-request.exception.ts index 33ad6652b65..126fbdd59e9 100644 --- a/packages/common/exceptions/bad-request.exception.ts +++ b/packages/common/exceptions/bad-request.exception.ts @@ -37,13 +37,13 @@ export class BadRequestException extends HttpException { objectOrError?: any, descriptionOrOptions: string | HttpExceptionOptions = 'Bad Request', ) { - const { description, httpExceptionOptions } = + const { description = 'Bad Request', httpExceptionOptions } = HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions); super( HttpException.createBody( objectOrError, - description!, + description, HttpStatus.BAD_REQUEST, ), HttpStatus.BAD_REQUEST, diff --git a/packages/common/exceptions/conflict.exception.ts b/packages/common/exceptions/conflict.exception.ts index 27bdda8aa07..c892186a1c0 100644 --- a/packages/common/exceptions/conflict.exception.ts +++ b/packages/common/exceptions/conflict.exception.ts @@ -37,13 +37,13 @@ export class ConflictException extends HttpException { objectOrError?: any, descriptionOrOptions: string | HttpExceptionOptions = 'Conflict', ) { - const { description, httpExceptionOptions } = + const { description = 'Conflict', httpExceptionOptions } = HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions); super( HttpException.createBody( objectOrError, - description!, + description, HttpStatus.CONFLICT, ), HttpStatus.CONFLICT, diff --git a/packages/common/exceptions/forbidden.exception.ts b/packages/common/exceptions/forbidden.exception.ts index f04b86bcaa0..a8e35e2cd11 100644 --- a/packages/common/exceptions/forbidden.exception.ts +++ b/packages/common/exceptions/forbidden.exception.ts @@ -37,13 +37,13 @@ export class ForbiddenException extends HttpException { objectOrError?: any, descriptionOrOptions: string | HttpExceptionOptions = 'Forbidden', ) { - const { description, httpExceptionOptions } = + const { description = 'Forbidden', httpExceptionOptions } = HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions); super( HttpException.createBody( objectOrError, - description!, + description, HttpStatus.FORBIDDEN, ), HttpStatus.FORBIDDEN, diff --git a/packages/common/exceptions/gateway-timeout.exception.ts b/packages/common/exceptions/gateway-timeout.exception.ts index 49dee49381f..d3b368466ef 100644 --- a/packages/common/exceptions/gateway-timeout.exception.ts +++ b/packages/common/exceptions/gateway-timeout.exception.ts @@ -37,13 +37,13 @@ export class GatewayTimeoutException extends HttpException { objectOrError?: any, descriptionOrOptions: string | HttpExceptionOptions = 'Gateway Timeout', ) { - const { description, httpExceptionOptions } = + const { description = 'Gateway Timeout', httpExceptionOptions } = HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions); super( HttpException.createBody( objectOrError, - description!, + description, HttpStatus.GATEWAY_TIMEOUT, ), HttpStatus.GATEWAY_TIMEOUT, diff --git a/packages/common/exceptions/gone.exception.ts b/packages/common/exceptions/gone.exception.ts index b1fc0f65b52..ac1d874daec 100644 --- a/packages/common/exceptions/gone.exception.ts +++ b/packages/common/exceptions/gone.exception.ts @@ -37,11 +37,11 @@ export class GoneException extends HttpException { objectOrError?: any, descriptionOrOptions: string | HttpExceptionOptions = 'Gone', ) { - const { description, httpExceptionOptions } = + const { description = 'Gone', httpExceptionOptions } = HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions); super( - HttpException.createBody(objectOrError, description!, HttpStatus.GONE), + HttpException.createBody(objectOrError, description, HttpStatus.GONE), HttpStatus.GONE, httpExceptionOptions, ); diff --git a/packages/common/exceptions/http-version-not-supported.exception.ts b/packages/common/exceptions/http-version-not-supported.exception.ts index 23283d343e2..9924036dba4 100644 --- a/packages/common/exceptions/http-version-not-supported.exception.ts +++ b/packages/common/exceptions/http-version-not-supported.exception.ts @@ -39,13 +39,13 @@ export class HttpVersionNotSupportedException extends HttpException { | string | HttpExceptionOptions = 'HTTP Version Not Supported', ) { - const { description, httpExceptionOptions } = + const { description = 'HTTP Version Not Supported', httpExceptionOptions } = HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions); super( HttpException.createBody( objectOrError, - description!, + description, HttpStatus.HTTP_VERSION_NOT_SUPPORTED, ), HttpStatus.HTTP_VERSION_NOT_SUPPORTED, diff --git a/packages/common/exceptions/im-a-teapot.exception.ts b/packages/common/exceptions/im-a-teapot.exception.ts index 209e32c4380..0045d83fe44 100644 --- a/packages/common/exceptions/im-a-teapot.exception.ts +++ b/packages/common/exceptions/im-a-teapot.exception.ts @@ -40,13 +40,13 @@ export class ImATeapotException extends HttpException { objectOrError?: any, descriptionOrOptions: string | HttpExceptionOptions = `I'm a teapot`, ) { - const { description, httpExceptionOptions } = + const { description = `I'm a teapot`, httpExceptionOptions } = HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions); super( HttpException.createBody( objectOrError, - description!, + description, HttpStatus.I_AM_A_TEAPOT, ), HttpStatus.I_AM_A_TEAPOT, diff --git a/packages/common/exceptions/internal-server-error.exception.ts b/packages/common/exceptions/internal-server-error.exception.ts index f8955b86afb..8891375532e 100644 --- a/packages/common/exceptions/internal-server-error.exception.ts +++ b/packages/common/exceptions/internal-server-error.exception.ts @@ -39,13 +39,13 @@ export class InternalServerErrorException extends HttpException { | string | HttpExceptionOptions = 'Internal Server Error', ) { - const { description, httpExceptionOptions } = + const { description = 'Internal Server Error', httpExceptionOptions } = HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions); super( HttpException.createBody( objectOrError, - description!, + description, HttpStatus.INTERNAL_SERVER_ERROR, ), HttpStatus.INTERNAL_SERVER_ERROR, diff --git a/packages/common/exceptions/method-not-allowed.exception.ts b/packages/common/exceptions/method-not-allowed.exception.ts index e6db6198cf5..f67e2cc2fbf 100644 --- a/packages/common/exceptions/method-not-allowed.exception.ts +++ b/packages/common/exceptions/method-not-allowed.exception.ts @@ -37,13 +37,13 @@ export class MethodNotAllowedException extends HttpException { objectOrError?: any, descriptionOrOptions: string | HttpExceptionOptions = 'Method Not Allowed', ) { - const { description, httpExceptionOptions } = + const { description = 'Method Not Allowed', httpExceptionOptions } = HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions); super( HttpException.createBody( objectOrError, - description!, + description, HttpStatus.METHOD_NOT_ALLOWED, ), HttpStatus.METHOD_NOT_ALLOWED, diff --git a/packages/common/exceptions/misdirected.exception.ts b/packages/common/exceptions/misdirected.exception.ts index 4ff86459f62..358fb10908e 100644 --- a/packages/common/exceptions/misdirected.exception.ts +++ b/packages/common/exceptions/misdirected.exception.ts @@ -37,13 +37,13 @@ export class MisdirectedException extends HttpException { objectOrError?: any, descriptionOrOptions: string | HttpExceptionOptions = 'Misdirected', ) { - const { description, httpExceptionOptions } = + const { description = 'Misdirected', httpExceptionOptions } = HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions); super( HttpException.createBody( objectOrError, - description!, + description, HttpStatus.MISDIRECTED, ), HttpStatus.MISDIRECTED, diff --git a/packages/common/exceptions/not-acceptable.exception.ts b/packages/common/exceptions/not-acceptable.exception.ts index 8b3cf66a028..98fba80d4ce 100644 --- a/packages/common/exceptions/not-acceptable.exception.ts +++ b/packages/common/exceptions/not-acceptable.exception.ts @@ -37,13 +37,13 @@ export class NotAcceptableException extends HttpException { objectOrError?: any, descriptionOrOptions: string | HttpExceptionOptions = 'Not Acceptable', ) { - const { description, httpExceptionOptions } = + const { description = 'Not Acceptable', httpExceptionOptions } = HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions); super( HttpException.createBody( objectOrError, - description!, + description, HttpStatus.NOT_ACCEPTABLE, ), HttpStatus.NOT_ACCEPTABLE, diff --git a/packages/common/exceptions/not-found.exception.ts b/packages/common/exceptions/not-found.exception.ts index 1213e8dca8b..d5cc3de6b5b 100644 --- a/packages/common/exceptions/not-found.exception.ts +++ b/packages/common/exceptions/not-found.exception.ts @@ -37,13 +37,13 @@ export class NotFoundException extends HttpException { objectOrError?: any, descriptionOrOptions: string | HttpExceptionOptions = 'Not Found', ) { - const { description, httpExceptionOptions } = + const { description = 'Not Found', httpExceptionOptions } = HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions); super( HttpException.createBody( objectOrError, - description!, + description, HttpStatus.NOT_FOUND, ), HttpStatus.NOT_FOUND, diff --git a/packages/common/exceptions/not-implemented.exception.ts b/packages/common/exceptions/not-implemented.exception.ts index 206a87333a7..f10962bab49 100644 --- a/packages/common/exceptions/not-implemented.exception.ts +++ b/packages/common/exceptions/not-implemented.exception.ts @@ -37,13 +37,13 @@ export class NotImplementedException extends HttpException { objectOrError?: any, descriptionOrOptions: string | HttpExceptionOptions = 'Not Implemented', ) { - const { description, httpExceptionOptions } = + const { description = 'Not Implemented', httpExceptionOptions } = HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions); super( HttpException.createBody( objectOrError, - description!, + description, HttpStatus.NOT_IMPLEMENTED, ), HttpStatus.NOT_IMPLEMENTED, diff --git a/packages/common/exceptions/payload-too-large.exception.ts b/packages/common/exceptions/payload-too-large.exception.ts index 4e841a35911..6c3b75c697d 100644 --- a/packages/common/exceptions/payload-too-large.exception.ts +++ b/packages/common/exceptions/payload-too-large.exception.ts @@ -37,13 +37,13 @@ export class PayloadTooLargeException extends HttpException { objectOrError?: any, descriptionOrOptions: string | HttpExceptionOptions = 'Payload Too Large', ) { - const { description, httpExceptionOptions } = + const { description = 'Payload Too Large', httpExceptionOptions } = HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions); super( HttpException.createBody( objectOrError, - description!, + description, HttpStatus.PAYLOAD_TOO_LARGE, ), HttpStatus.PAYLOAD_TOO_LARGE, diff --git a/packages/common/exceptions/precondition-failed.exception.ts b/packages/common/exceptions/precondition-failed.exception.ts index 68c3433c7f7..5f7987b34d0 100644 --- a/packages/common/exceptions/precondition-failed.exception.ts +++ b/packages/common/exceptions/precondition-failed.exception.ts @@ -37,13 +37,13 @@ export class PreconditionFailedException extends HttpException { objectOrError?: any, descriptionOrOptions: string | HttpExceptionOptions = 'Precondition Failed', ) { - const { description, httpExceptionOptions } = + const { description = 'Precondition Failed', httpExceptionOptions } = HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions); super( HttpException.createBody( objectOrError, - description!, + description, HttpStatus.PRECONDITION_FAILED, ), HttpStatus.PRECONDITION_FAILED, diff --git a/packages/common/exceptions/request-timeout.exception.ts b/packages/common/exceptions/request-timeout.exception.ts index f4f6d795566..0c8a5b87c43 100644 --- a/packages/common/exceptions/request-timeout.exception.ts +++ b/packages/common/exceptions/request-timeout.exception.ts @@ -37,13 +37,13 @@ export class RequestTimeoutException extends HttpException { objectOrError?: any, descriptionOrOptions: string | HttpExceptionOptions = 'Request Timeout', ) { - const { description, httpExceptionOptions } = + const { description = 'Request Timeout', httpExceptionOptions } = HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions); super( HttpException.createBody( objectOrError, - description!, + description, HttpStatus.REQUEST_TIMEOUT, ), HttpStatus.REQUEST_TIMEOUT, diff --git a/packages/common/exceptions/service-unavailable.exception.ts b/packages/common/exceptions/service-unavailable.exception.ts index 0afca4bdd5f..ec23beba811 100644 --- a/packages/common/exceptions/service-unavailable.exception.ts +++ b/packages/common/exceptions/service-unavailable.exception.ts @@ -37,13 +37,13 @@ export class ServiceUnavailableException extends HttpException { objectOrError?: any, descriptionOrOptions: string | HttpExceptionOptions = 'Service Unavailable', ) { - const { description, httpExceptionOptions } = + const { description = 'Service Unavailable', httpExceptionOptions } = HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions); super( HttpException.createBody( objectOrError, - description!, + description, HttpStatus.SERVICE_UNAVAILABLE, ), HttpStatus.SERVICE_UNAVAILABLE, diff --git a/packages/common/exceptions/unauthorized.exception.ts b/packages/common/exceptions/unauthorized.exception.ts index c5be32253ff..c59da6c1b11 100644 --- a/packages/common/exceptions/unauthorized.exception.ts +++ b/packages/common/exceptions/unauthorized.exception.ts @@ -37,13 +37,13 @@ export class UnauthorizedException extends HttpException { objectOrError?: any, descriptionOrOptions: string | HttpExceptionOptions = 'Unauthorized', ) { - const { description, httpExceptionOptions } = + const { description = 'Unauthorized', httpExceptionOptions } = HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions); super( HttpException.createBody( objectOrError, - description!, + description, HttpStatus.UNAUTHORIZED, ), HttpStatus.UNAUTHORIZED, diff --git a/packages/common/exceptions/unprocessable-entity.exception.ts b/packages/common/exceptions/unprocessable-entity.exception.ts index d6f99aa253f..648304d5000 100644 --- a/packages/common/exceptions/unprocessable-entity.exception.ts +++ b/packages/common/exceptions/unprocessable-entity.exception.ts @@ -39,13 +39,13 @@ export class UnprocessableEntityException extends HttpException { | string | HttpExceptionOptions = 'Unprocessable Entity', ) { - const { description, httpExceptionOptions } = + const { description = 'Unprocessable Entity', httpExceptionOptions } = HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions); super( HttpException.createBody( objectOrError, - description!, + description, HttpStatus.UNPROCESSABLE_ENTITY, ), HttpStatus.UNPROCESSABLE_ENTITY, diff --git a/packages/common/exceptions/unsupported-media-type.exception.ts b/packages/common/exceptions/unsupported-media-type.exception.ts index 9d8af94af26..5d8e042d08b 100644 --- a/packages/common/exceptions/unsupported-media-type.exception.ts +++ b/packages/common/exceptions/unsupported-media-type.exception.ts @@ -39,13 +39,13 @@ export class UnsupportedMediaTypeException extends HttpException { | string | HttpExceptionOptions = 'Unsupported Media Type', ) { - const { description, httpExceptionOptions } = + const { description = 'Unsupported Media Type', httpExceptionOptions } = HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions); super( HttpException.createBody( objectOrError, - description!, + description, HttpStatus.UNSUPPORTED_MEDIA_TYPE, ), HttpStatus.UNSUPPORTED_MEDIA_TYPE, diff --git a/packages/common/test/exceptions/http.exception.spec.ts b/packages/common/test/exceptions/http.exception.spec.ts index 3bcc6d2aecb..ea428cc257a 100644 --- a/packages/common/test/exceptions/http.exception.spec.ts +++ b/packages/common/test/exceptions/http.exception.spec.ts @@ -267,5 +267,49 @@ describe('HttpException', () => { expect(cause).to.be.eql(errorCause); }); }); + + it('should preserve default description when using options with cause', () => { + const builtInExceptionsWithDefaults: [Type, string][] = [ + [BadGatewayException, 'Bad Gateway'], + [BadRequestException, 'Bad Request'], + [ConflictException, 'Conflict'], + [ForbiddenException, 'Forbidden'], + [GatewayTimeoutException, 'Gateway Timeout'], + [GoneException, 'Gone'], + [HttpVersionNotSupportedException, 'HTTP Version Not Supported'], + [ImATeapotException, `I'm a teapot`], + [InternalServerErrorException, 'Internal Server Error'], + [MethodNotAllowedException, 'Method Not Allowed'], + [MisdirectedException, 'Misdirected'], + [NotAcceptableException, 'Not Acceptable'], + [NotFoundException, 'Not Found'], + [NotImplementedException, 'Not Implemented'], + [PayloadTooLargeException, 'Payload Too Large'], + [PreconditionFailedException, 'Precondition Failed'], + [RequestTimeoutException, 'Request Timeout'], + [ServiceUnavailableException, 'Service Unavailable'], + [UnauthorizedException, 'Unauthorized'], + [UnprocessableEntityException, 'Unprocessable Entity'], + [UnsupportedMediaTypeException, 'Unsupported Media Type'], + ]; + + builtInExceptionsWithDefaults.forEach( + ([ExceptionClass, expectedDescription]) => { + const error = new ExceptionClass('Custom message', { + cause: errorCause, + }); + + const response = error.getResponse() as { + message: string; + error: string; + }; + + expect(response.error).to.be.eql( + expectedDescription, + `${ExceptionClass.name} should have error "${expectedDescription}"`, + ); + }, + ); + }); }); });