Skip to content

Commit

Permalink
Merge pull request #10460 from thiagomini/feature/10392-http-exceptio…
Browse files Browse the repository at this point in the history
…ns-cause-with-custom-message

feat(common): add error options object
  • Loading branch information
kamilmysliwiec committed Nov 7, 2022
2 parents e04e414 + ab881e3 commit 7ec60ca
Show file tree
Hide file tree
Showing 23 changed files with 455 additions and 151 deletions.
13 changes: 9 additions & 4 deletions packages/common/exceptions/bad-gateway.exception.ts
@@ -1,5 +1,6 @@
import { HttpStatus } from '../enums/http-status.enum';
import { HttpException } from './http.exception';
import { isString } from '../utils/shared.utils';
import { HttpException, HttpExceptionOptions } from './http.exception';

/**
* Defines an HTTP exception for *Bad Gateway* type errors.
Expand All @@ -18,7 +19,7 @@ export class BadGatewayException extends HttpException {
* @usageNotes
* The HTTP response status code will be 502.
* - 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 502.
Expand All @@ -31,19 +32,23 @@ export class BadGatewayException 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 = 'Bad Gateway',
descriptionOrOptions: string | HttpExceptionOptions = 'Bad Gateway',
) {
const { description, httpExceptionOptions } =
HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions);

super(
HttpException.createBody(
objectOrError,
description,
HttpStatus.BAD_GATEWAY,
),
HttpStatus.BAD_GATEWAY,
httpExceptionOptions,
);
}
}
13 changes: 9 additions & 4 deletions packages/common/exceptions/bad-request.exception.ts
@@ -1,5 +1,6 @@
import { isString } from 'class-validator';
import { HttpStatus } from '../enums/http-status.enum';
import { HttpException } from './http.exception';
import { HttpException, HttpExceptionOptions } from './http.exception';

/**
* Defines an HTTP exception for *Bad Request* type errors.
Expand All @@ -18,7 +19,7 @@ export class BadRequestException extends HttpException {
* @usageNotes
* The HTTP response status code will be 400.
* - 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 400.
Expand All @@ -31,19 +32,23 @@ export class BadRequestException 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 = 'Bad Request',
descriptionOrOptions: string | HttpExceptionOptions = 'Bad Request',
) {
const { description, httpExceptionOptions } =
HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions);

super(
HttpException.createBody(
objectOrError,
description,
HttpStatus.BAD_REQUEST,
),
HttpStatus.BAD_REQUEST,
httpExceptionOptions,
);
}
}
15 changes: 11 additions & 4 deletions packages/common/exceptions/conflict.exception.ts
@@ -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.
Expand All @@ -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.
Expand All @@ -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,
);
}
}
12 changes: 8 additions & 4 deletions packages/common/exceptions/forbidden.exception.ts
@@ -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 *Forbidden* type errors.
Expand All @@ -18,7 +18,7 @@ export class ForbiddenException extends HttpException {
* @usageNotes
* The HTTP response status code will be 403.
* - 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 403.
Expand All @@ -31,19 +31,23 @@ export class ForbiddenException 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 = 'Forbidden',
descriptionOrOptions: string | HttpExceptionOptions = 'Forbidden',
) {
const { description, httpExceptionOptions } =
HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions);

super(
HttpException.createBody(
objectOrError,
description,
HttpStatus.FORBIDDEN,
),
HttpStatus.FORBIDDEN,
httpExceptionOptions,
);
}
}
12 changes: 8 additions & 4 deletions packages/common/exceptions/gateway-timeout.exception.ts
@@ -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 *Gateway Timeout* type errors.
Expand All @@ -18,7 +18,7 @@ export class GatewayTimeoutException extends HttpException {
* @usageNotes
* The HTTP response status code will be 504.
* - 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 504.
Expand All @@ -31,19 +31,23 @@ export class GatewayTimeoutException 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 = 'Gateway Timeout',
descriptionOrOptions: string | HttpExceptionOptions = 'Gateway Timeout',
) {
const { description, httpExceptionOptions } =
HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions);

super(
HttpException.createBody(
objectOrError,
description,
HttpStatus.GATEWAY_TIMEOUT,
),
HttpStatus.GATEWAY_TIMEOUT,
httpExceptionOptions,
);
}
}
15 changes: 11 additions & 4 deletions packages/common/exceptions/gone.exception.ts
@@ -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 *Gone* type errors.
Expand All @@ -18,7 +18,7 @@ export class GoneException extends HttpException {
* @usageNotes
* The HTTP response status code will be 410.
* - 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 410.
Expand All @@ -31,12 +31,19 @@ export class GoneException 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 = 'Gone') {
constructor(
objectOrError?: string | object | any,
descriptionOrOptions: string | HttpExceptionOptions = 'Gone',
) {
const { description, httpExceptionOptions } =
HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions);

super(
HttpException.createBody(objectOrError, description, HttpStatus.GONE),
HttpStatus.GONE,
httpExceptionOptions,
);
}
}
14 changes: 10 additions & 4 deletions packages/common/exceptions/http-version-not-supported.exception.ts
@@ -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 *Http Version Not Supported* type errors.
Expand All @@ -18,7 +18,7 @@ export class HttpVersionNotSupportedException extends HttpException {
* @usageNotes
* The HTTP response status code will be 505.
* - 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 505.
Expand All @@ -31,19 +31,25 @@ export class HttpVersionNotSupportedException 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 = 'HTTP Version Not Supported',
descriptionOrOptions:
| string
| HttpExceptionOptions = 'HTTP Version Not Supported',
) {
const { description, httpExceptionOptions } =
HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions);

super(
HttpException.createBody(
objectOrError,
description,
HttpStatus.HTTP_VERSION_NOT_SUPPORTED,
),
HttpStatus.HTTP_VERSION_NOT_SUPPORTED,
httpExceptionOptions,
);
}
}

0 comments on commit 7ec60ca

Please sign in to comment.