mirror of
https://github.com/dadosfera/maestro.git
synced 2026-09-05 06:04:49 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cc3b34d87e | ||
|
|
88bc7fe625 | ||
|
|
137f129e13 | ||
|
|
f49c5ac1c1 | ||
|
|
d288a4f357 | ||
|
|
9f009309cb |
@@ -0,0 +1,22 @@
|
||||
import { ExceptionFilter, Catch, ArgumentsHost } from '@nestjs/common';
|
||||
import { Response } from 'express';
|
||||
|
||||
import ErrorBuilder from '../utils/ErrorBuilder';
|
||||
|
||||
@Catch(Error)
|
||||
export class GrpcToHttpExceptionFilter implements ExceptionFilter {
|
||||
catch(exception: any, host: ArgumentsHost) {
|
||||
const ctx = host.switchToHttp();
|
||||
const response = ctx.getResponse<Response>();
|
||||
|
||||
// return this exception directly if is already of type ErrorBuilder,
|
||||
// else transform it using the ErrorBuilder
|
||||
const err =
|
||||
exception.constructor.name === ErrorBuilder.name
|
||||
? exception
|
||||
: new ErrorBuilder(exception.details);
|
||||
|
||||
const { statusCode } = err.response;
|
||||
return response.status(statusCode).json(err.response);
|
||||
}
|
||||
}
|
||||
+1
-4
@@ -1,20 +1,17 @@
|
||||
import 'elastic-apm-node/start';
|
||||
if (process.env.ENV !== 'local') require('elastic-apm-node/start');
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
||||
import { writeFileSync } from 'fs';
|
||||
import helmet from 'helmet';
|
||||
import { DadosferaLogger } from 'dadosfera-logs';
|
||||
|
||||
import documentEmpty from '../swagger_empty.json';
|
||||
|
||||
import { AppModule } from './app.module';
|
||||
|
||||
async function bootstrap() {
|
||||
DadosferaLogger.setupLogger({
|
||||
serviceName: 'maestro',
|
||||
serviceEnvironment: process.env.ENV,
|
||||
});
|
||||
|
||||
const logger = new DadosferaLogger().logger;
|
||||
|
||||
const orginalWinstonLog = logger.log.bind(logger);
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
Inject,
|
||||
UseFilters,
|
||||
} from '@nestjs/common';
|
||||
import { ApiTags } from '@nestjs/swagger';
|
||||
import {
|
||||
@@ -22,8 +23,10 @@ import {
|
||||
|
||||
import { AuthClientService } from './auth.service';
|
||||
import { DadosferaLogger } from 'dadosfera-logs';
|
||||
import { GrpcToHttpExceptionFilter } from '../../error/grpc-to-http-exception.filter';
|
||||
|
||||
@ApiTags('Auth')
|
||||
@UseFilters(new GrpcToHttpExceptionFilter())
|
||||
@Controller('auth')
|
||||
export class AuthController {
|
||||
logger: any;
|
||||
|
||||
@@ -3,7 +3,7 @@ import { RpcException } from '@nestjs/microservices';
|
||||
|
||||
import ErrorCodes from './errorCodes';
|
||||
|
||||
function enrichErrorCode(code: string) {
|
||||
export function EnrichErrorCode(code: string) {
|
||||
switch (code) {
|
||||
case ErrorCodes.AUTH.WRONG_CREDENTIALS:
|
||||
return {
|
||||
@@ -138,7 +138,7 @@ export default class ErrorBuilder extends HttpException {
|
||||
logger.log(code);
|
||||
}
|
||||
|
||||
const { statusCode, message, error, code: rCode } = enrichErrorCode(code);
|
||||
const { statusCode, message, error, code: rCode } = EnrichErrorCode(code);
|
||||
super({ statusCode, message, error, code: rCode }, statusCode);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user