Skip to content

Commit f2683a9

Browse files
committed
Add GRPC_CODE_FROM_HTTP map
1 parent ca84be4 commit f2683a9

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

lib/utils/grpc-codes-map.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { status as Status } from "@grpc/grpc-js";
2+
import { HttpStatus } from "@nestjs/common";
3+
4+
// https://github.com/nestjs/nest/blob/master/packages/common/enums/http-status.enum.ts
5+
export const GRPC_CODE_FROM_HTTP: Record<number, number> = {
6+
[HttpStatus.OK]: Status.OK,
7+
[HttpStatus.BAD_GATEWAY]: Status.UNKNOWN,
8+
[HttpStatus.UNPROCESSABLE_ENTITY]: Status.INVALID_ARGUMENT,
9+
[HttpStatus.REQUEST_TIMEOUT]: Status.DEADLINE_EXCEEDED,
10+
[HttpStatus.NOT_FOUND]: Status.NOT_FOUND,
11+
[HttpStatus.CONFLICT]: Status.ALREADY_EXISTS,
12+
[HttpStatus.FORBIDDEN]: Status.PERMISSION_DENIED,
13+
[HttpStatus.TOO_MANY_REQUESTS]: Status.RESOURCE_EXHAUSTED,
14+
[HttpStatus.PRECONDITION_REQUIRED]: Status.FAILED_PRECONDITION,
15+
[HttpStatus.METHOD_NOT_ALLOWED]: Status.ABORTED,
16+
[HttpStatus.PAYLOAD_TOO_LARGE]: Status.OUT_OF_RANGE,
17+
[HttpStatus.NOT_IMPLEMENTED]: Status.UNIMPLEMENTED,
18+
[HttpStatus.INTERNAL_SERVER_ERROR]: Status.INTERNAL,
19+
[HttpStatus.UNAUTHORIZED]: Status.UNAUTHENTICATED,
20+
};

lib/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./error-object";
22
export * from "./http-codes-map";
33
export * from "./grpc-exceptions-map";
4+
export * from "./grpc-codes-map";

test/interceptors/http-to-grpc-interceptor.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Observable, throwError } from "rxjs";
33
import { HttpToGrpcInterceptor } from "../../lib";
44
import { RpcException } from "@nestjs/microservices";
55
import { status as GrpcStatusCode } from "@grpc/grpc-js";
6+
import { GRPC_CODE_FROM_HTTP } from "../../lib/utils";
67

78
const throwMockException = (
89
Exception: new (...args: any[]) => any,
@@ -12,7 +13,7 @@ const throwMockException = (
1213
() =>
1314
new RpcException({
1415
message: exception.message,
15-
code: GrpcStatusCode.NOT_FOUND,
16+
code: GRPC_CODE_FROM_HTTP[exception.status],
1617
}),
1718
);
1819
};

0 commit comments

Comments
 (0)