Skip to content

Commit 59cf0d3

Browse files
committed
feat(common): add payment required exception for http 402
402 is the only status code in the 400-422 range without a dedicated HttpException subclass. The HttpStatus.PAYMENT_REQUIRED enum entry already exists, but throwing it requires the verbose form 'new HttpException(msg, HttpStatus.PAYMENT_REQUIRED)'. Add PaymentRequiredException mirroring ImATeapotException for symmetry with the other 21 sibling exceptions, export it from the barrel, and extend the parametric tests in http.exception.spec.ts.
1 parent aeec17d commit 59cf0d3

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

packages/common/exceptions/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export * from './not-acceptable.exception';
1515
export * from './not-found.exception';
1616
export * from './not-implemented.exception';
1717
export * from './payload-too-large.exception';
18+
export * from './payment-required.exception';
1819
export * from './precondition-failed.exception';
1920
export * from './request-timeout.exception';
2021
export * from './service-unavailable.exception';
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { HttpStatus } from '../enums/http-status.enum';
2+
import { HttpException, HttpExceptionOptions } from './http.exception';
3+
4+
/**
5+
* Defines an HTTP exception for *Payment Required* type errors.
6+
*
7+
* @see [Built-in HTTP exceptions](https://docs.nestjs.com/exception-filters#built-in-http-exceptions)
8+
*
9+
* @publicApi
10+
*/
11+
export class PaymentRequiredException extends HttpException {
12+
/**
13+
* Instantiate a `PaymentRequiredException` Exception.
14+
*
15+
* @example
16+
* `throw new PaymentRequiredException()`
17+
*
18+
* @usageNotes
19+
* The HTTP response status code will be 402.
20+
* - The `objectOrError` argument defines the JSON response body or the message string.
21+
* - The `descriptionOrOptions` argument contains either a short description of the HTTP error or an options object used to provide an underlying error cause.
22+
*
23+
* By default, the JSON response body contains two properties:
24+
* - `statusCode`: this will be the value 402.
25+
* - `message`: the string `'Payment Required'` by default; override this by supplying
26+
* a string in the `objectOrError` parameter.
27+
*
28+
* If the parameter `objectOrError` is a string, the response body will contain an
29+
* additional property, `error`, with a short description of the HTTP error. To override the
30+
* entire JSON response body, pass an object instead. Nest will serialize the object
31+
* and return it as the JSON response body.
32+
*
33+
* @param objectOrError string or object describing the error condition.
34+
* @param descriptionOrOptions either a short description of the HTTP error or an options object used to provide an underlying error cause
35+
*/
36+
constructor(
37+
objectOrError?: any,
38+
descriptionOrOptions: string | HttpExceptionOptions = 'Payment Required',
39+
) {
40+
const { description, httpExceptionOptions } =
41+
HttpException.extractDescriptionAndOptionsFrom(descriptionOrOptions);
42+
43+
super(
44+
HttpException.createBody(
45+
objectOrError,
46+
description!,
47+
HttpStatus.PAYMENT_REQUIRED,
48+
),
49+
HttpStatus.PAYMENT_REQUIRED,
50+
httpExceptionOptions,
51+
);
52+
}
53+
}

packages/common/test/exceptions/http.exception.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
NotFoundException,
1818
NotImplementedException,
1919
PayloadTooLargeException,
20+
PaymentRequiredException,
2021
PreconditionFailedException,
2122
RequestTimeoutException,
2223
ServiceUnavailableException,
@@ -66,6 +67,7 @@ describe('HttpException', () => {
6667
const testCases: [Type<HttpException>, number][] = [
6768
[BadRequestException, 400],
6869
[UnauthorizedException, 401],
70+
[PaymentRequiredException, 402],
6971
[ForbiddenException, 403],
7072
[NotFoundException, 404],
7173
[MethodNotAllowedException, 405],
@@ -98,6 +100,7 @@ describe('HttpException', () => {
98100
const testCases: [Type<HttpException>, number, string][] = [
99101
[BadRequestException, 400, 'Bad Request'],
100102
[UnauthorizedException, 401, 'Unauthorized'],
103+
[PaymentRequiredException, 402, 'Payment Required'],
101104
[ForbiddenException, 403, 'Forbidden'],
102105
[NotFoundException, 404, 'Not Found'],
103106
[MethodNotAllowedException, 405, 'Method Not Allowed'],
@@ -248,6 +251,7 @@ describe('HttpException', () => {
248251
NotFoundException,
249252
NotImplementedException,
250253
PayloadTooLargeException,
254+
PaymentRequiredException,
251255
PreconditionFailedException,
252256
RequestTimeoutException,
253257
ServiceUnavailableException,

0 commit comments

Comments
 (0)