Skip to content

Commit

Permalink
feature(grpc): add grpc to http exception mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
tychota committed Apr 8, 2019
1 parent 81239d3 commit e1cd93d
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 20 deletions.
1 change: 1 addition & 0 deletions packages/common/enums/http-status.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export enum HttpStatus {
I_AM_A_TEAPOT = 418,
UNPROCESSABLE_ENTITY = 422,
TOO_MANY_REQUESTS = 429,
CLIENT_CLOSED_REQUEST = 499,
INTERNAL_SERVER_ERROR = 500,
NOT_IMPLEMENTED = 501,
BAD_GATEWAY = 502,
Expand Down
15 changes: 15 additions & 0 deletions packages/common/exceptions/canceled.exception.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { HttpException } from './http.exception';
import { HttpStatus } from '../enums/http-status.enum';
import { createHttpExceptionBody } from '../utils/http-exception-body.util';

export class CanceledException extends HttpException {
constructor(
message?: string | object | any,
error = 'Client Closed Request',
) {
super(
createHttpExceptionBody(message, error, HttpStatus.CLIENT_CLOSED_REQUEST),
HttpStatus.CLIENT_CLOSED_REQUEST,
);
}
}
2 changes: 2 additions & 0 deletions packages/common/exceptions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ export * from './bad-gateway.exception';
export * from './service-unavailable.exception';
export * from './gateway-timeout.exception';
export * from './im-a-teapot.exception';
export * from './canceled.exception';
export * from './too-many-requests.exception';
12 changes: 12 additions & 0 deletions packages/common/exceptions/too-many-requests.exception.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { HttpException } from './http.exception';
import { HttpStatus } from '../enums/http-status.enum';
import { createHttpExceptionBody } from '../utils/http-exception-body.util';

export class TooManyRequestsException extends HttpException {
constructor(message?: string | object | any, error = 'Too Many Request') {
super(
createHttpExceptionBody(message, error, HttpStatus.TOO_MANY_REQUESTS),
HttpStatus.TOO_MANY_REQUESTS,
);
}
}
20 changes: 20 additions & 0 deletions packages/microservices/enums/grpc-status.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// As per https://github.com/grpc/grpc-node/blob/master/packages/grpc-js/src/constants.ts
export enum GrpcStatus {
OK = 0,
CANCELLED,
UNKNOWN,
INVALID_ARGUMENT,
DEADLINE_EXCEEDED,
NOT_FOUND,
ALREADY_EXISTS,
PERMISSION_DENIED,
RESOURCE_EXHAUSTED,
FAILED_PRECONDITION,
ABORTED,
OUT_OF_RANGE,
UNIMPLEMENTED,
INTERNAL,
UNAVAILABLE,
DATA_LOSS,
UNAUTHENTICATED,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { GrpcStatus } from '../../enums/grpc-status.enum';
import {
InternalServerErrorException,
BadRequestException,
GatewayTimeoutException,
NotFoundException,
ConflictException,
UnauthorizedException,
ForbiddenException,
ServiceUnavailableException,
CanceledException,
TooManyRequestsException,
NotImplementedException,
} from '@nestjs/common';

interface IGrpcToHttpExceptionMapping {
[grpcStatus: number]: typeof InternalServerErrorException;
}

// Based on https://github.com/grpc/grpc/blob/master/doc/statuscodes.md
export const GrpcToHttpExceptionMapping: IGrpcToHttpExceptionMapping = {
[GrpcStatus.OK]: null,
[GrpcStatus.CANCELLED]: CanceledException,
[GrpcStatus.UNKNOWN]: InternalServerErrorException,
[GrpcStatus.INVALID_ARGUMENT]: BadRequestException,
[GrpcStatus.DEADLINE_EXCEEDED]: GatewayTimeoutException,
[GrpcStatus.NOT_FOUND]: NotFoundException,
[GrpcStatus.ALREADY_EXISTS]: ConflictException,
[GrpcStatus.PERMISSION_DENIED]: ForbiddenException,
[GrpcStatus.UNAUTHENTICATED]: UnauthorizedException,
[GrpcStatus.RESOURCE_EXHAUSTED]: TooManyRequestsException,
[GrpcStatus.FAILED_PRECONDITION]: BadRequestException,
[GrpcStatus.DEADLINE_EXCEEDED]: GatewayTimeoutException,
[GrpcStatus.ABORTED]: ConflictException,
[GrpcStatus.OUT_OF_RANGE]: BadRequestException,
[GrpcStatus.UNIMPLEMENTED]: NotImplementedException,
[GrpcStatus.INTERNAL]: InternalServerErrorException,
[GrpcStatus.UNAVAILABLE]: ServiceUnavailableException,
[GrpcStatus.DATA_LOSS]: InternalServerErrorException,
};
21 changes: 1 addition & 20 deletions packages/microservices/exceptions/grpc-exceptions.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
// As per https://github.com/grpc/grpc-node/blob/master/packages/grpc-js/src/constants.ts
export enum GrpcStatus {
OK = 0,
CANCELLED,
UNKNOWN,
INVALID_ARGUMENT,
DEADLINE_EXCEEDED,
NOT_FOUND,
ALREADY_EXISTS,
PERMISSION_DENIED,
RESOURCE_EXHAUSTED,
FAILED_PRECONDITION,
ABORTED,
OUT_OF_RANGE,
UNIMPLEMENTED,
INTERNAL,
UNAVAILABLE,
DATA_LOSS,
UNAUTHENTICATED,
}
import { GrpcStatus } from '../enums/grpc-status.enum';

export class GrpcException extends Error {
public readonly message: string;
Expand Down

0 comments on commit e1cd93d

Please sign in to comment.