nestjs-grpc-exceptions
Advanced tools
Comparing version 0.2.1 to 0.2.2
# nestjs-grpc-exceptions | ||
## 0.2.2 | ||
### Patch Changes | ||
- fbea997: refactor the internal handler | ||
## 0.2.1 | ||
@@ -4,0 +10,0 @@ |
import { | ||
CallHandler, | ||
ExecutionContext, | ||
HttpStatus, | ||
Injectable, | ||
@@ -10,26 +9,6 @@ NestInterceptor, | ||
import { catchError } from "rxjs/operators"; | ||
import { | ||
GrpcAbortedException, | ||
GrpcAlreadyExistsException, | ||
GrpcInternalException, | ||
GrpcInvalidArgumentException, | ||
GrpcNotFoundException, | ||
GrpcPermissionDeniedException, | ||
GrpcResourceExhaustedException, | ||
GrpcUnauthenticatedException, | ||
GrpcUnknownException, | ||
} from "../exceptions"; | ||
import { GRPC_CODE_FROM_HTTP } from "../utils"; | ||
import { status as Status } from "@grpc/grpc-js"; | ||
import { RpcException } from "@nestjs/microservices"; | ||
const GRPC_EXCEPTION_FROM_HTTP: Record<number, any> = { | ||
[HttpStatus.NOT_FOUND]: GrpcNotFoundException, | ||
[HttpStatus.FORBIDDEN]: GrpcPermissionDeniedException, | ||
[HttpStatus.METHOD_NOT_ALLOWED]: GrpcAbortedException, | ||
[HttpStatus.INTERNAL_SERVER_ERROR]: GrpcInternalException, | ||
[HttpStatus.TOO_MANY_REQUESTS]: GrpcResourceExhaustedException, | ||
[HttpStatus.BAD_GATEWAY]: GrpcUnknownException, | ||
[HttpStatus.CONFLICT]: GrpcAlreadyExistsException, | ||
[HttpStatus.UNPROCESSABLE_ENTITY]: GrpcInvalidArgumentException, | ||
[HttpStatus.UNAUTHORIZED]: GrpcUnauthenticatedException, | ||
}; | ||
@Injectable() | ||
@@ -59,7 +38,12 @@ export class HttpToGrpcInterceptor implements NestInterceptor { | ||
if (!(exception.statusCode in GRPC_EXCEPTION_FROM_HTTP)) | ||
return throwError(() => err); | ||
const statusCode = | ||
GRPC_CODE_FROM_HTTP[exception.statusCode] || Status.INTERNAL; | ||
const Exception = GRPC_EXCEPTION_FROM_HTTP[exception.statusCode]; | ||
return throwError(() => new Exception(exception.message)); | ||
return throwError( | ||
() => | ||
new RpcException({ | ||
message: exception.message, | ||
code: statusCode, | ||
}), | ||
); | ||
}), | ||
@@ -66,0 +50,0 @@ ); |
export * from "./error-object"; | ||
export * from "./http-codes-map"; | ||
export * from "./grpc-codes-map"; |
@@ -14,3 +14,3 @@ { | ||
}, | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"main": "dist/index.js", | ||
@@ -17,0 +17,0 @@ "module": "dist/index.mjs", |
@@ -1,2 +0,2 @@ | ||
import { HttpException } from "@nestjs/common"; | ||
import { HttpException, HttpStatus } from "@nestjs/common"; | ||
import { Observable, throwError } from "rxjs"; | ||
@@ -51,3 +51,3 @@ import { GrpcNotFoundException, GrpcToHttpInterceptor } from "../../lib"; | ||
expect(err).toBeInstanceOf(HttpException); | ||
expect(err.status).toEqual(404); | ||
expect(err.status).toEqual(HttpStatus.NOT_FOUND); | ||
done(); | ||
@@ -54,0 +54,0 @@ }, |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
845165
39
20920