Compare commits
No commits in common. "ee1e8640d57867228c0c1e72ea545d4cd1d9fba5" and "353213c6bdfc8b0b25e04acacd02c4ee697ccfdf" have entirely different histories.
ee1e8640d5
...
353213c6bd
@ -28,7 +28,6 @@
|
||||
"@nestjs/microservices": "^11.1.12",
|
||||
"@nestjs/platform-express": "^11.0.1",
|
||||
"@nestjs/swagger": "^11.2.5",
|
||||
"@teacinema/common": "^1.0.0",
|
||||
"@teacinema/contracts": "^1.0.0",
|
||||
"class-transformer": "^0.5.1",
|
||||
"class-validator": "^0.14.3",
|
||||
|
||||
@ -5,7 +5,6 @@ import { SwaggerModule } from '@nestjs/swagger'
|
||||
|
||||
import { AppModule } from './core/app.module'
|
||||
import { getCorsConfig, swaggerConfig, validationPipe } from './core/config'
|
||||
import { GrpcExceptionFilter } from './shared/filters'
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule)
|
||||
@ -14,7 +13,6 @@ async function bootstrap() {
|
||||
const config = app.get(ConfigService)
|
||||
|
||||
app.useGlobalPipes(validationPipe)
|
||||
app.useGlobalFilters(new GrpcExceptionFilter())
|
||||
|
||||
app.enableCors(getCorsConfig(config))
|
||||
|
||||
|
||||
@ -1,57 +0,0 @@
|
||||
import {
|
||||
ArgumentsHost,
|
||||
Catch,
|
||||
ExceptionFilter,
|
||||
HttpException,
|
||||
HttpStatus
|
||||
} from '@nestjs/common'
|
||||
import type { Response } from 'express'
|
||||
|
||||
import { grpcToHttpStatus } from '../utils'
|
||||
|
||||
type GrpcException = {
|
||||
code: number
|
||||
details: string
|
||||
}
|
||||
|
||||
@Catch()
|
||||
export class GrpcExceptionFilter implements ExceptionFilter {
|
||||
public catch(exception: unknown, host: ArgumentsHost) {
|
||||
console.log('filter host', host)
|
||||
console.log('filter exception', exception)
|
||||
const ctx = host.switchToHttp()
|
||||
const response = ctx.getResponse<Response>()
|
||||
|
||||
if (this.isGrpcException(exception)) {
|
||||
const status =
|
||||
grpcToHttpStatus[exception.code] || HttpStatus.INTERNAL_SERVER_ERROR
|
||||
|
||||
return response.status(status).json({
|
||||
statusCode: status,
|
||||
message: exception.details || 'gRPC Error'
|
||||
})
|
||||
}
|
||||
|
||||
if (exception instanceof HttpException) {
|
||||
const status = exception.getStatus()
|
||||
return response.status(status).json({
|
||||
statusCode: status,
|
||||
message: exception.message
|
||||
})
|
||||
}
|
||||
|
||||
return response.status(HttpStatus.INTERNAL_SERVER_ERROR).json({
|
||||
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
message: 'Internal Server Error'
|
||||
})
|
||||
}
|
||||
|
||||
private isGrpcException(exception: unknown): exception is GrpcException {
|
||||
return Boolean(
|
||||
exception &&
|
||||
typeof exception === 'object' &&
|
||||
'code' in exception &&
|
||||
'details' in exception
|
||||
)
|
||||
}
|
||||
}
|
||||
@ -1 +0,0 @@
|
||||
export * from './grpc-exception.filter'
|
||||
@ -1,22 +0,0 @@
|
||||
import { HttpStatus } from '@nestjs/common'
|
||||
import { RpcStatus } from '@teacinema/common'
|
||||
|
||||
export const grpcToHttpStatus: Record<number, number> = {
|
||||
[RpcStatus.OK]: HttpStatus.OK,
|
||||
[RpcStatus.CANCELED]: 499,
|
||||
[RpcStatus.UNKNOWN]: HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
[RpcStatus.INVALID_ARGUMENTS]: HttpStatus.BAD_REQUEST,
|
||||
[RpcStatus.DEADLINE_EXCEEDED]: HttpStatus.GATEWAY_TIMEOUT,
|
||||
[RpcStatus.NOT_FOUND]: HttpStatus.NOT_FOUND,
|
||||
[RpcStatus.ALREADY_EXISTS]: HttpStatus.CONFLICT,
|
||||
[RpcStatus.PERMISION_DENIED]: HttpStatus.FORBIDDEN,
|
||||
[RpcStatus.RESOURSE_EXHAUSED]: HttpStatus.TOO_MANY_REQUESTS,
|
||||
[RpcStatus.FAILED_PRECONDITION]: HttpStatus.BAD_REQUEST,
|
||||
[RpcStatus.ABORTED]: HttpStatus.CONFLICT,
|
||||
[RpcStatus.OUT_OF_RANGE]: HttpStatus.BAD_REQUEST,
|
||||
[RpcStatus.UNIMPLEMENTED]: HttpStatus.NOT_IMPLEMENTED,
|
||||
[RpcStatus.INTERNAL]: HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
[RpcStatus.UNAVAILABLE]: HttpStatus.SERVICE_UNAVAILABLE,
|
||||
[RpcStatus.DATA_LOSS]: HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
[RpcStatus.UNAUTHENTICATED]: HttpStatus.UNAUTHORIZED
|
||||
}
|
||||
@ -1 +0,0 @@
|
||||
export * from './grpc-to-http-status'
|
||||
@ -1192,11 +1192,6 @@
|
||||
dependencies:
|
||||
"@sinonjs/commons" "^3.0.1"
|
||||
|
||||
"@teacinema/common@^1.0.0":
|
||||
version "1.0.0"
|
||||
resolved "https://git.ksv741.keenetic.pro/api/packages/teacinema/npm/%40teacinema%2Fcommon/-/1.0.0/common-1.0.0.tgz#3b79ae6eb46352ed1544535be094fe0ead244c36"
|
||||
integrity sha512-L006mySvhRpUlRHf3ONbXA0RG42gbQ9QAnmQuZx4zejHEsWB9702xe18MSYcBilv+lmYkQpiX/g3p8P7suLfqQ==
|
||||
|
||||
"@teacinema/contracts@^1.0.0":
|
||||
version "1.0.1"
|
||||
resolved "https://git.ksv741.keenetic.pro/api/packages/teacinema/npm/%40teacinema%2Fcontracts/-/1.0.1/contracts-1.0.1.tgz#25021fecb1d33d0505fd11ca3d8201660cb1bcbe"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user