-
Notifications
You must be signed in to change notification settings - Fork 0
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 #1 from harrylowkey/feat/grpc-exception-filter
feat: grpc exception filter
- Loading branch information
Showing
9 changed files
with
2,821 additions
and
2,534 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { HttpStatus } from '@nestjs/common'; | ||
import { status } from '@grpc/grpc-js'; | ||
|
||
export const HTTP_STATUS_CODE: Record<number, number> = { | ||
// standard gRPC error mapping | ||
// https://cloud.google.com/apis/design/errors#handling_errors | ||
[HttpStatus.BAD_REQUEST]: status.INVALID_ARGUMENT, | ||
[HttpStatus.UNAUTHORIZED]: status.UNAUTHENTICATED, | ||
[HttpStatus.FORBIDDEN]: status.PERMISSION_DENIED, | ||
[HttpStatus.NOT_FOUND]: status.NOT_FOUND, | ||
[HttpStatus.CONFLICT]: status.ALREADY_EXISTS, | ||
[HttpStatus.GONE]: status.ABORTED, | ||
[HttpStatus.TOO_MANY_REQUESTS]: status.RESOURCE_EXHAUSTED, | ||
499: status.CANCELLED, | ||
[HttpStatus.INTERNAL_SERVER_ERROR]: status.INTERNAL, | ||
[HttpStatus.NOT_IMPLEMENTED]: status.UNIMPLEMENTED, | ||
[HttpStatus.BAD_GATEWAY]: status.UNKNOWN, | ||
[HttpStatus.SERVICE_UNAVAILABLE]: status.UNAVAILABLE, | ||
[HttpStatus.GATEWAY_TIMEOUT]: status.DEADLINE_EXCEEDED, | ||
|
||
// additional built-in http exceptions | ||
// https://docs.nestjs.com/exception-filters#built-in-http-exceptions | ||
[HttpStatus.HTTP_VERSION_NOT_SUPPORTED]: status.UNAVAILABLE, | ||
[HttpStatus.PAYLOAD_TOO_LARGE]: status.OUT_OF_RANGE, | ||
[HttpStatus.UNSUPPORTED_MEDIA_TYPE]: status.CANCELLED, | ||
[HttpStatus.UNPROCESSABLE_ENTITY]: status.CANCELLED, | ||
[HttpStatus.I_AM_A_TEAPOT]: status.UNKNOWN, | ||
[HttpStatus.METHOD_NOT_ALLOWED]: status.CANCELLED, | ||
[HttpStatus.PRECONDITION_FAILED]: status.FAILED_PRECONDITION | ||
}; |
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,19 @@ | ||
export enum GrpcStatusEnum { | ||
OK = 0, | ||
CANCELLED = 1, | ||
UNKNOWN = 2, | ||
INVALID_ARGUMENT = 3, | ||
DEADLINE_EXCEEDED = 4, | ||
NOT_FOUND = 5, | ||
ALREADY_EXISTS = 6, | ||
PERMISSION_DENIED = 7, | ||
RESOURCE_EXHAUSTED = 8, | ||
FAILED_PRECONDITION = 9, | ||
ABORTED = 10, | ||
OUT_OF_RANGE = 11, | ||
UNIMPLEMENTED = 12, | ||
INTERNAL = 13, | ||
UNAVAILABLE = 14, | ||
DATA_LOSS = 15, | ||
UNAUTHENTICATED = 16 | ||
} |
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,10 @@ | ||
import { Catch } from '@nestjs/common'; | ||
import { HttpExceptionFilter } from './http-exception.filter'; | ||
|
||
@Catch() | ||
export class GrpcExceptionFilter extends HttpExceptionFilter { | ||
constructor() { | ||
super(); | ||
this.isGrpc = true; | ||
} | ||
} |
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
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
Oops, something went wrong.