Skip to content

Commit ccc9476

Browse files
committed
feature(grpc): add grpc to http exception mapping
1 parent 001d58d commit ccc9476

File tree

7 files changed

+91
-20
lines changed

7 files changed

+91
-20
lines changed

packages/common/enums/http-status.enum.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export enum HttpStatus {
3737
I_AM_A_TEAPOT = 418,
3838
UNPROCESSABLE_ENTITY = 422,
3939
TOO_MANY_REQUESTS = 429,
40+
CLIENT_CLOSED_REQUEST = 499,
4041
INTERNAL_SERVER_ERROR = 500,
4142
NOT_IMPLEMENTED = 501,
4243
BAD_GATEWAY = 502,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { HttpException } from './http.exception';
2+
import { HttpStatus } from '../enums/http-status.enum';
3+
import { createHttpExceptionBody } from '../utils/http-exception-body.util';
4+
5+
export class CanceledException extends HttpException {
6+
constructor(
7+
message?: string | object | any,
8+
error = 'Client Closed Request',
9+
) {
10+
super(
11+
createHttpExceptionBody(message, error, HttpStatus.CLIENT_CLOSED_REQUEST),
12+
HttpStatus.CLIENT_CLOSED_REQUEST,
13+
);
14+
}
15+
}

packages/common/exceptions/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ export * from './bad-gateway.exception';
1717
export * from './service-unavailable.exception';
1818
export * from './gateway-timeout.exception';
1919
export * from './im-a-teapot.exception';
20+
export * from './canceled.exception';
21+
export * from './too-many-requests.exception';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { HttpException } from './http.exception';
2+
import { HttpStatus } from '../enums/http-status.enum';
3+
import { createHttpExceptionBody } from '../utils/http-exception-body.util';
4+
5+
export class TooManyRequestsException extends HttpException {
6+
constructor(message?: string | object | any, error = 'Too Many Request') {
7+
super(
8+
createHttpExceptionBody(message, error, HttpStatus.TOO_MANY_REQUESTS),
9+
HttpStatus.TOO_MANY_REQUESTS,
10+
);
11+
}
12+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// As per https://github.com/grpc/grpc-node/blob/master/packages/grpc-js/src/constants.ts
2+
export enum GrpcStatus {
3+
OK = 0,
4+
CANCELLED,
5+
UNKNOWN,
6+
INVALID_ARGUMENT,
7+
DEADLINE_EXCEEDED,
8+
NOT_FOUND,
9+
ALREADY_EXISTS,
10+
PERMISSION_DENIED,
11+
RESOURCE_EXHAUSTED,
12+
FAILED_PRECONDITION,
13+
ABORTED,
14+
OUT_OF_RANGE,
15+
UNIMPLEMENTED,
16+
INTERNAL,
17+
UNAVAILABLE,
18+
DATA_LOSS,
19+
UNAUTHENTICATED,
20+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { GrpcStatus } from '../../enums/grpc-status.enum';
2+
import {
3+
InternalServerErrorException,
4+
BadRequestException,
5+
GatewayTimeoutException,
6+
NotFoundException,
7+
ConflictException,
8+
UnauthorizedException,
9+
ForbiddenException,
10+
ServiceUnavailableException,
11+
CanceledException,
12+
TooManyRequestsException,
13+
NotImplementedException,
14+
} from '@nestjs/common';
15+
16+
interface IGrpcToHttpExceptionMapping {
17+
[grpcStatus: number]: typeof InternalServerErrorException;
18+
}
19+
20+
// Based on https://github.com/grpc/grpc/blob/master/doc/statuscodes.md
21+
export const GrpcToHttpExceptionMapping: IGrpcToHttpExceptionMapping = {
22+
[GrpcStatus.OK]: null,
23+
[GrpcStatus.CANCELLED]: CanceledException,
24+
[GrpcStatus.UNKNOWN]: InternalServerErrorException,
25+
[GrpcStatus.INVALID_ARGUMENT]: BadRequestException,
26+
[GrpcStatus.DEADLINE_EXCEEDED]: GatewayTimeoutException,
27+
[GrpcStatus.NOT_FOUND]: NotFoundException,
28+
[GrpcStatus.ALREADY_EXISTS]: ConflictException,
29+
[GrpcStatus.PERMISSION_DENIED]: ForbiddenException,
30+
[GrpcStatus.UNAUTHENTICATED]: UnauthorizedException,
31+
[GrpcStatus.RESOURCE_EXHAUSTED]: TooManyRequestsException,
32+
[GrpcStatus.FAILED_PRECONDITION]: BadRequestException,
33+
[GrpcStatus.DEADLINE_EXCEEDED]: GatewayTimeoutException,
34+
[GrpcStatus.ABORTED]: ConflictException,
35+
[GrpcStatus.OUT_OF_RANGE]: BadRequestException,
36+
[GrpcStatus.UNIMPLEMENTED]: NotImplementedException,
37+
[GrpcStatus.INTERNAL]: InternalServerErrorException,
38+
[GrpcStatus.UNAVAILABLE]: ServiceUnavailableException,
39+
[GrpcStatus.DATA_LOSS]: InternalServerErrorException,
40+
};

packages/microservices/exceptions/grpc-exceptions.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,4 @@
1-
// As per https://github.com/grpc/grpc-node/blob/master/packages/grpc-js/src/constants.ts
2-
export enum GrpcStatus {
3-
OK = 0,
4-
CANCELLED,
5-
UNKNOWN,
6-
INVALID_ARGUMENT,
7-
DEADLINE_EXCEEDED,
8-
NOT_FOUND,
9-
ALREADY_EXISTS,
10-
PERMISSION_DENIED,
11-
RESOURCE_EXHAUSTED,
12-
FAILED_PRECONDITION,
13-
ABORTED,
14-
OUT_OF_RANGE,
15-
UNIMPLEMENTED,
16-
INTERNAL,
17-
UNAVAILABLE,
18-
DATA_LOSS,
19-
UNAUTHENTICATED,
20-
}
1+
import { GrpcStatus } from '../enums/grpc-status.enum';
212

223
export class GrpcException extends Error {
234
public readonly message: string;

0 commit comments

Comments
 (0)