mirror of
https://github.com/dadosfera/maestro.git
synced 2026-11-29 10:40:04 +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 { NestFactory } from '@nestjs/core';
|
||||||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
||||||
import { writeFileSync } from 'fs';
|
|
||||||
import helmet from 'helmet';
|
import helmet from 'helmet';
|
||||||
import { DadosferaLogger } from 'dadosfera-logs';
|
import { DadosferaLogger } from 'dadosfera-logs';
|
||||||
|
|
||||||
import documentEmpty from '../swagger_empty.json';
|
import documentEmpty from '../swagger_empty.json';
|
||||||
|
|
||||||
import { AppModule } from './app.module';
|
import { AppModule } from './app.module';
|
||||||
|
|
||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
DadosferaLogger.setupLogger({
|
DadosferaLogger.setupLogger({
|
||||||
serviceName: 'maestro',
|
serviceName: 'maestro',
|
||||||
serviceEnvironment: process.env.ENV,
|
serviceEnvironment: process.env.ENV,
|
||||||
});
|
});
|
||||||
|
|
||||||
const logger = new DadosferaLogger().logger;
|
const logger = new DadosferaLogger().logger;
|
||||||
|
|
||||||
const orginalWinstonLog = logger.log.bind(logger);
|
const orginalWinstonLog = logger.log.bind(logger);
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
HttpCode,
|
HttpCode,
|
||||||
HttpStatus,
|
HttpStatus,
|
||||||
Inject,
|
Inject,
|
||||||
|
UseFilters,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ApiTags } from '@nestjs/swagger';
|
import { ApiTags } from '@nestjs/swagger';
|
||||||
import {
|
import {
|
||||||
@@ -22,8 +23,10 @@ import {
|
|||||||
|
|
||||||
import { AuthClientService } from './auth.service';
|
import { AuthClientService } from './auth.service';
|
||||||
import { DadosferaLogger } from 'dadosfera-logs';
|
import { DadosferaLogger } from 'dadosfera-logs';
|
||||||
|
import { GrpcToHttpExceptionFilter } from '../../error/grpc-to-http-exception.filter';
|
||||||
|
|
||||||
@ApiTags('Auth')
|
@ApiTags('Auth')
|
||||||
|
@UseFilters(new GrpcToHttpExceptionFilter())
|
||||||
@Controller('auth')
|
@Controller('auth')
|
||||||
export class AuthController {
|
export class AuthController {
|
||||||
logger: any;
|
logger: any;
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { RpcException } from '@nestjs/microservices';
|
|||||||
|
|
||||||
import ErrorCodes from './errorCodes';
|
import ErrorCodes from './errorCodes';
|
||||||
|
|
||||||
function enrichErrorCode(code: string) {
|
export function EnrichErrorCode(code: string) {
|
||||||
switch (code) {
|
switch (code) {
|
||||||
case ErrorCodes.AUTH.WRONG_CREDENTIALS:
|
case ErrorCodes.AUTH.WRONG_CREDENTIALS:
|
||||||
return {
|
return {
|
||||||
@@ -138,7 +138,7 @@ export default class ErrorBuilder extends HttpException {
|
|||||||
logger.log(code);
|
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);
|
super({ statusCode, message, error, code: rCode }, statusCode);
|
||||||
this.code = code;
|
this.code = code;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user