Skip to content

Commit 7ec60ca

Browse files
Merge pull request #10460 from thiagomini/feature/10392-http-exceptions-cause-with-custom-message
feat(common): add error options object
2 parents e04e414 + ab881e3 commit 7ec60ca

23 files changed

+455
-151
lines changed

packages/common/exceptions/bad-gateway.exception.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { HttpStatus } from '../enums/http-status.enum';
2-
import { HttpException } from './http.exception';
2+
import { isString } from '../utils/shared.utils';
3+
import { HttpException, HttpExceptionOptions } from './http.exception';
34

45
/**
56
* Defines an HTTP exception for *Bad Gateway* type errors.
@@ -18,7 +19,7 @@ export class BadGatewayException extends HttpException {
1819
* @usageNotes
1920
* The HTTP response status code will be 502.
2021
* - The `objectOrError` argument defines the JSON response body or the message string.
21-
* - The `description` argument contains a short description of the HTTP error.
22+
* - The `descriptionOrOptions` argument contains either a short description of the HTTP error or an options object used to provide an underlying error cause.
2223
*
2324
* By default, the JSON response body contains two properties:
2425
* - `statusCode`: this will be the value 502.
@@ -31,19 +32,23 @@ export class BadGatewayException extends HttpException {
3132
* and return it as the JSON response body.
3233
*
3334
* @param objectOrError string or object describing the error condition.
34-
* @param description a short description of the HTTP error.
35+
* @param descriptionOrOptions either a short description of the HTTP error or an options object used to provide an underlying error cause
3536
*/
3637
constructor(
3738
objectOrError?: string | object | any,
38-
description = 'Bad Gateway',
39+
descriptionOrOptions: string | HttpExceptionOptions = 'Bad Gateway',
3940
) {
41+
const { description, httpExceptionOptions } =
42+
HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions);
43+
4044
super(
4145
HttpException.createBody(
4246
objectOrError,
4347
description,
4448
HttpStatus.BAD_GATEWAY,
4549
),
4650
HttpStatus.BAD_GATEWAY,
51+
httpExceptionOptions,
4752
);
4853
}
4954
}

packages/common/exceptions/bad-request.exception.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { isString } from 'class-validator';
12
import { HttpStatus } from '../enums/http-status.enum';
2-
import { HttpException } from './http.exception';
3+
import { HttpException, HttpExceptionOptions } from './http.exception';
34

45
/**
56
* Defines an HTTP exception for *Bad Request* type errors.
@@ -18,7 +19,7 @@ export class BadRequestException extends HttpException {
1819
* @usageNotes
1920
* The HTTP response status code will be 400.
2021
* - The `objectOrError` argument defines the JSON response body or the message string.
21-
* - The `description` argument contains a short description of the HTTP error.
22+
* - The `descriptionOrOptions` argument contains either a short description of the HTTP error or an options object used to provide an underlying error cause.
2223
*
2324
* By default, the JSON response body contains two properties:
2425
* - `statusCode`: this will be the value 400.
@@ -31,19 +32,23 @@ export class BadRequestException extends HttpException {
3132
* and return it as the JSON response body.
3233
*
3334
* @param objectOrError string or object describing the error condition.
34-
* @param description a short description of the HTTP error.
35+
* @param descriptionOrOptions either a short description of the HTTP error or an options object used to provide an underlying error cause
3536
*/
3637
constructor(
3738
objectOrError?: string | object | any,
38-
description = 'Bad Request',
39+
descriptionOrOptions: string | HttpExceptionOptions = 'Bad Request',
3940
) {
41+
const { description, httpExceptionOptions } =
42+
HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions);
43+
4044
super(
4145
HttpException.createBody(
4246
objectOrError,
4347
description,
4448
HttpStatus.BAD_REQUEST,
4549
),
4650
HttpStatus.BAD_REQUEST,
51+
httpExceptionOptions,
4752
);
4853
}
4954
}

packages/common/exceptions/conflict.exception.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { HttpStatus } from '../enums/http-status.enum';
2-
import { HttpException } from './http.exception';
2+
import { HttpException, HttpExceptionOptions } from './http.exception';
33

44
/**
55
* Defines an HTTP exception for *Conflict* type errors.
@@ -18,7 +18,7 @@ export class ConflictException extends HttpException {
1818
* @usageNotes
1919
* The HTTP response status code will be 409.
2020
* - The `objectOrError` argument defines the JSON response body or the message string.
21-
* - The `description` argument contains a short description of the HTTP error.
21+
* - The `descriptionOrOptions` argument contains either a short description of the HTTP error or an options object used to provide an underlying error cause.
2222
*
2323
* By default, the JSON response body contains two properties:
2424
* - `statusCode`: this will be the value 409.
@@ -31,12 +31,19 @@ export class ConflictException extends HttpException {
3131
* and return it as the JSON response body.
3232
*
3333
* @param objectOrError string or object describing the error condition.
34-
* @param description a short description of the HTTP error.
34+
* @param descriptionOrOptions either a short description of the HTTP error or an options object used to provide an underlying error cause
3535
*/
36-
constructor(objectOrError?: string | object | any, description = 'Conflict') {
36+
constructor(
37+
objectOrError?: string | object | any,
38+
descriptionOrOptions: string | HttpExceptionOptions = 'Conflict',
39+
) {
40+
const { description, httpExceptionOptions } =
41+
HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions);
42+
3743
super(
3844
HttpException.createBody(objectOrError, description, HttpStatus.CONFLICT),
3945
HttpStatus.CONFLICT,
46+
httpExceptionOptions,
4047
);
4148
}
4249
}

packages/common/exceptions/forbidden.exception.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { HttpStatus } from '../enums/http-status.enum';
2-
import { HttpException } from './http.exception';
2+
import { HttpException, HttpExceptionOptions } from './http.exception';
33

44
/**
55
* Defines an HTTP exception for *Forbidden* type errors.
@@ -18,7 +18,7 @@ export class ForbiddenException extends HttpException {
1818
* @usageNotes
1919
* The HTTP response status code will be 403.
2020
* - The `objectOrError` argument defines the JSON response body or the message string.
21-
* - The `description` argument contains a short description of the HTTP error.
21+
* - The `descriptionOrOptions` argument contains either a short description of the HTTP error or an options object used to provide an underlying error cause.
2222
*
2323
* By default, the JSON response body contains two properties:
2424
* - `statusCode`: this will be the value 403.
@@ -31,19 +31,23 @@ export class ForbiddenException extends HttpException {
3131
* and return it as the JSON response body.
3232
*
3333
* @param objectOrError string or object describing the error condition.
34-
* @param description a short description of the HTTP error.
34+
* @param descriptionOrOptions either a short description of the HTTP error or an options object used to provide an underlying error cause
3535
*/
3636
constructor(
3737
objectOrError?: string | object | any,
38-
description = 'Forbidden',
38+
descriptionOrOptions: string | HttpExceptionOptions = 'Forbidden',
3939
) {
40+
const { description, httpExceptionOptions } =
41+
HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions);
42+
4043
super(
4144
HttpException.createBody(
4245
objectOrError,
4346
description,
4447
HttpStatus.FORBIDDEN,
4548
),
4649
HttpStatus.FORBIDDEN,
50+
httpExceptionOptions,
4751
);
4852
}
4953
}

packages/common/exceptions/gateway-timeout.exception.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { HttpStatus } from '../enums/http-status.enum';
2-
import { HttpException } from './http.exception';
2+
import { HttpException, HttpExceptionOptions } from './http.exception';
33

44
/**
55
* Defines an HTTP exception for *Gateway Timeout* type errors.
@@ -18,7 +18,7 @@ export class GatewayTimeoutException extends HttpException {
1818
* @usageNotes
1919
* The HTTP response status code will be 504.
2020
* - The `objectOrError` argument defines the JSON response body or the message string.
21-
* - The `description` argument contains a short description of the HTTP error.
21+
* - The `descriptionOrOptions` argument contains either a short description of the HTTP error or an options object used to provide an underlying error cause.
2222
*
2323
* By default, the JSON response body contains two properties:
2424
* - `statusCode`: this will be the value 504.
@@ -31,19 +31,23 @@ export class GatewayTimeoutException extends HttpException {
3131
* and return it as the JSON response body.
3232
*
3333
* @param objectOrError string or object describing the error condition.
34-
* @param description a short description of the HTTP error.
34+
* @param descriptionOrOptions either a short description of the HTTP error or an options object used to provide an underlying error cause
3535
*/
3636
constructor(
3737
objectOrError?: string | object | any,
38-
description = 'Gateway Timeout',
38+
descriptionOrOptions: string | HttpExceptionOptions = 'Gateway Timeout',
3939
) {
40+
const { description, httpExceptionOptions } =
41+
HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions);
42+
4043
super(
4144
HttpException.createBody(
4245
objectOrError,
4346
description,
4447
HttpStatus.GATEWAY_TIMEOUT,
4548
),
4649
HttpStatus.GATEWAY_TIMEOUT,
50+
httpExceptionOptions,
4751
);
4852
}
4953
}

packages/common/exceptions/gone.exception.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { HttpStatus } from '../enums/http-status.enum';
2-
import { HttpException } from './http.exception';
2+
import { HttpException, HttpExceptionOptions } from './http.exception';
33

44
/**
55
* Defines an HTTP exception for *Gone* type errors.
@@ -18,7 +18,7 @@ export class GoneException extends HttpException {
1818
* @usageNotes
1919
* The HTTP response status code will be 410.
2020
* - The `objectOrError` argument defines the JSON response body or the message string.
21-
* - The `description` argument contains a short description of the HTTP error.
21+
* - The `descriptionOrOptions` argument contains either a short description of the HTTP error or an options object used to provide an underlying error cause.
2222
*
2323
* By default, the JSON response body contains two properties:
2424
* - `statusCode`: this will be the value 410.
@@ -31,12 +31,19 @@ export class GoneException extends HttpException {
3131
* and return it as the JSON response body.
3232
*
3333
* @param objectOrError string or object describing the error condition.
34-
* @param description a short description of the HTTP error.
34+
* @param descriptionOrOptions either a short description of the HTTP error or an options object used to provide an underlying error cause
3535
*/
36-
constructor(objectOrError?: string | object | any, description = 'Gone') {
36+
constructor(
37+
objectOrError?: string | object | any,
38+
descriptionOrOptions: string | HttpExceptionOptions = 'Gone',
39+
) {
40+
const { description, httpExceptionOptions } =
41+
HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions);
42+
3743
super(
3844
HttpException.createBody(objectOrError, description, HttpStatus.GONE),
3945
HttpStatus.GONE,
46+
httpExceptionOptions,
4047
);
4148
}
4249
}

packages/common/exceptions/http-version-not-supported.exception.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { HttpStatus } from '../enums/http-status.enum';
2-
import { HttpException } from './http.exception';
2+
import { HttpException, HttpExceptionOptions } from './http.exception';
33

44
/**
55
* Defines an HTTP exception for *Http Version Not Supported* type errors.
@@ -18,7 +18,7 @@ export class HttpVersionNotSupportedException extends HttpException {
1818
* @usageNotes
1919
* The HTTP response status code will be 505.
2020
* - The `objectOrError` argument defines the JSON response body or the message string.
21-
* - The `description` argument contains a short description of the HTTP error.
21+
* - The `descriptionOrOptions` argument contains either a short description of the HTTP error or an options object used to provide an underlying error cause.
2222
*
2323
* By default, the JSON response body contains two properties:
2424
* - `statusCode`: this will be the value 505.
@@ -31,19 +31,25 @@ export class HttpVersionNotSupportedException extends HttpException {
3131
* and return it as the JSON response body.
3232
*
3333
* @param objectOrError string or object describing the error condition.
34-
* @param description a short description of the HTTP error.
34+
* @param descriptionOrOptions either a short description of the HTTP error or an options object used to provide an underlying error cause
3535
*/
3636
constructor(
3737
objectOrError?: string | object | any,
38-
description = 'HTTP Version Not Supported',
38+
descriptionOrOptions:
39+
| string
40+
| HttpExceptionOptions = 'HTTP Version Not Supported',
3941
) {
42+
const { description, httpExceptionOptions } =
43+
HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions);
44+
4045
super(
4146
HttpException.createBody(
4247
objectOrError,
4348
description,
4449
HttpStatus.HTTP_VERSION_NOT_SUPPORTED,
4550
),
4651
HttpStatus.HTTP_VERSION_NOT_SUPPORTED,
52+
httpExceptionOptions,
4753
);
4854
}
4955
}

0 commit comments

Comments
 (0)