-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from youngkiu/feature/refactor-HttpToGrpcInter…
…ceptor refactor: Generalize HttpToGrpcInterceptor with status code map.
- Loading branch information
Showing
5 changed files
with
110 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { status as Status } from "@grpc/grpc-js"; | ||
import { HttpStatus } from "@nestjs/common"; | ||
|
||
// https://github.com/nestjs/nest/blob/master/packages/common/enums/http-status.enum.ts | ||
export const GRPC_CODE_FROM_HTTP: Record<number, number> = { | ||
[HttpStatus.OK]: Status.OK, | ||
[HttpStatus.BAD_GATEWAY]: Status.UNKNOWN, | ||
[HttpStatus.UNPROCESSABLE_ENTITY]: Status.INVALID_ARGUMENT, | ||
[HttpStatus.REQUEST_TIMEOUT]: Status.DEADLINE_EXCEEDED, | ||
[HttpStatus.NOT_FOUND]: Status.NOT_FOUND, | ||
[HttpStatus.CONFLICT]: Status.ALREADY_EXISTS, | ||
[HttpStatus.FORBIDDEN]: Status.PERMISSION_DENIED, | ||
[HttpStatus.TOO_MANY_REQUESTS]: Status.RESOURCE_EXHAUSTED, | ||
[HttpStatus.PRECONDITION_REQUIRED]: Status.FAILED_PRECONDITION, | ||
[HttpStatus.METHOD_NOT_ALLOWED]: Status.ABORTED, | ||
[HttpStatus.PAYLOAD_TOO_LARGE]: Status.OUT_OF_RANGE, | ||
[HttpStatus.NOT_IMPLEMENTED]: Status.UNIMPLEMENTED, | ||
[HttpStatus.INTERNAL_SERVER_ERROR]: Status.INTERNAL, | ||
[HttpStatus.UNAUTHORIZED]: Status.UNAUTHENTICATED, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./error-object"; | ||
export * from "./http-codes-map"; | ||
export * from "./grpc-codes-map"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { NotFoundException } from "@nestjs/common"; | ||
import { Observable, throwError } from "rxjs"; | ||
import { HttpToGrpcInterceptor } from "../../lib"; | ||
import { RpcException } from "@nestjs/microservices"; | ||
import { status as GrpcStatusCode } from "@grpc/grpc-js"; | ||
import { GRPC_CODE_FROM_HTTP } from "../../lib/utils"; | ||
|
||
const throwMockException = ( | ||
Exception: new (...args: any[]) => any, | ||
): Observable<any> => { | ||
const exception = new Exception(Exception.name); | ||
return throwError( | ||
() => | ||
new RpcException({ | ||
message: exception.message, | ||
code: GRPC_CODE_FROM_HTTP[exception.status], | ||
}), | ||
); | ||
}; | ||
|
||
describe("HttpToGrpcInterceptor", () => { | ||
let interceptor: HttpToGrpcInterceptor; | ||
|
||
beforeAll(() => { | ||
interceptor = new HttpToGrpcInterceptor(); | ||
}); | ||
|
||
it("Should be defined", () => { | ||
expect(interceptor).toBeDefined(); | ||
}); | ||
|
||
it("Should convert HTTP exceptions to gRPC exceptions", (done) => { | ||
const intercept$ = interceptor.intercept({} as any, { | ||
handle: () => throwMockException(NotFoundException), | ||
}) as Observable<any>; | ||
|
||
intercept$.subscribe({ | ||
error: (err) => { | ||
expect(err).toBeInstanceOf(RpcException); | ||
done(); | ||
}, | ||
complete: () => done(), | ||
}); | ||
}); | ||
|
||
it("Should convert HTTP exceptions to gRPC exceptions", (done) => { | ||
const intercept$ = interceptor.intercept({} as any, { | ||
handle: () => throwMockException(NotFoundException), | ||
}) as Observable<any>; | ||
|
||
intercept$.subscribe({ | ||
error: (err) => { | ||
expect(err).toBeInstanceOf(RpcException); | ||
expect(err.error.code).toEqual(GrpcStatusCode.NOT_FOUND); | ||
done(); | ||
}, | ||
complete: () => done(), | ||
}); | ||
}); | ||
|
||
it("Should contain the HTTP exception error message", (done) => { | ||
const intercept$ = interceptor.intercept({} as any, { | ||
handle: () => throwMockException(NotFoundException), | ||
}) as Observable<any>; | ||
|
||
intercept$.subscribe({ | ||
error: (err) => { | ||
expect(err).toBeInstanceOf(RpcException); | ||
expect(err.message).toEqual(NotFoundException.name); | ||
done(); | ||
}, | ||
complete: () => done(), | ||
}); | ||
}); | ||
}); |