diff --git a/src/app.controller.ts b/src/app.controller.ts index a757aedf..b6bf5d12 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -1,6 +1,5 @@ import { Body, Controller, Get, Post, Query, UseFilters } from '@nestjs/common'; import { AppService } from './app.service'; -import { GenericErrorFilter } from './genericErrorFilter'; import { GetPoolAndTokenVolumesReply, GetPoolAndTokenVolumesRequest, @@ -10,7 +9,6 @@ import { } from './interfaces/IController'; @Controller() -@UseFilters(new GenericErrorFilter()) export class AppController { constructor(private readonly appService: AppService) {} diff --git a/src/generic.interceptor.ts b/src/generic.interceptor.ts index 06ad4b81..42712c98 100644 --- a/src/generic.interceptor.ts +++ b/src/generic.interceptor.ts @@ -4,7 +4,7 @@ import { Injectable, NestInterceptor, } from '@nestjs/common'; -import { Observable } from 'rxjs'; +import { Observable, tap } from 'rxjs'; import { log } from './util/logger/logger'; @Injectable() @@ -15,13 +15,27 @@ export class GenericInterceptor implements NestInterceptor { ): Observable { const req = context.switchToHttp().getRequest(); const endpoint = context.getHandler(); - log.info({ - message: - endpoint.name == 'getPoolAndTokenVolumes' - ? '' - : JSON.stringify(req.query), - endpoint: endpoint.name, - }); - return next.handle().pipe(); + + return next.handle().pipe( + tap({ + next: (value) => { + log.info({ + message: + endpoint.name == 'getPoolAndTokenVolumes' + ? '' + : JSON.stringify(req.query), + endpoint: endpoint.name, + }); + }, + error: (err: Error) => { + log.error({ + message: err?.message, + stack: err?.stack || '', + detail: err?.name, + endpoint: endpoint.name, + }); + }, + }), + ); } } diff --git a/src/genericErrorFilter.ts b/src/genericErrorFilter.ts deleted file mode 100644 index 0bd01d06..00000000 --- a/src/genericErrorFilter.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { log } from './util/logger/logger'; -import { Observable, throwError } from 'rxjs'; -import { - ArgumentsHost, - Catch, - ExceptionFilter, - HttpStatus, -} from '@nestjs/common'; -@Catch() -export class GenericErrorFilter implements ExceptionFilter { - catch(exception: any, host: ArgumentsHost): Observable { - const ctx = host.switchToHttp(); - const errorResponse: any = { - statusCode: HttpStatus.INTERNAL_SERVER_ERROR, - timestamp: new Date().toISOString(), - errorName: exception?.name, - message: exception?.message, - requestData: JSON.stringify(ctx.getRequest().query), - }; - - log.error({ - message: errorResponse?.message, - stack: exception?.stack || '', - detail: errorResponse?.errorResponsestatusCode, - endpoint: ctx.getRequest().url, - }); - return throwError(() => errorResponse); - } -}