Define an exception filter to log the cause of exceptions

This commit is contained in:
Romain Thouvenin 2024-03-29 15:58:06 +01:00
parent 7f7a51d19b
commit 4581af5e9f
4 changed files with 42 additions and 7 deletions

View File

@ -0,0 +1,24 @@
import { ArgumentsHost, Catch, Logger } from '@nestjs/common';
import { BaseRpcExceptionFilter } from '@nestjs/microservices';
import { Observable } from 'rxjs';
@Catch()
export class LogCauseExceptionFilter extends BaseRpcExceptionFilter {
private static readonly causeLogger = new Logger('RpcExceptionsHandler');
catch(exception: any, host: ArgumentsHost): Observable<any> {
const response = super.catch(exception, host);
const cause = exception.cause;
if (cause) {
if (this.isError(cause)) {
LogCauseExceptionFilter.causeLogger.error(
'Caused by: ' + cause.message,
cause.stack,
);
} else {
LogCauseExceptionFilter.causeLogger.error('Caused by: ' + cause);
}
}
return response;
}
}

View File

@ -1,9 +1,5 @@
import { QueryBase } from '@mobicoop/ddd-library';
import { AlgorithmType } from '../../types/algorithm.types';
import { Waypoint } from '../../types/waypoint.type';
import { Frequency, Role } from '@modules/ad/core/domain/ad.types';
import { MatchRequestDto } from '@modules/ad/interface/grpc-controllers/dtos/match.request.dto';
import { DateTimeTransformerPort } from '../../ports/datetime-transformer.port';
import {
Path,
PathCreator,
@ -11,8 +7,12 @@ import {
TypedRoute,
} from '@modules/ad/core/domain/path-creator.service';
import { Point } from '@modules/ad/core/domain/value-objects/point.value-object';
import { Route } from '../../types/route.type';
import { MatchRequestDto } from '@modules/ad/interface/grpc-controllers/dtos/match.request.dto';
import { DateTimeTransformerPort } from '../../ports/datetime-transformer.port';
import { GeorouterPort } from '../../ports/georouter.port';
import { AlgorithmType } from '../../types/algorithm.types';
import { Route } from '../../types/route.type';
import { Waypoint } from '../../types/waypoint.type';
export class MatchQuery extends QueryBase {
id?: string;
@ -227,7 +227,9 @@ export class MatchQuery extends QueryBase {
}
});
} catch (e: any) {
throw new Error('Unable to find a route for given waypoints');
throw new Error('Unable to find a route for given waypoints', {
cause: e,
});
}
return this;
};

View File

@ -6,12 +6,20 @@ import { MatchingResult } from '@modules/ad/core/application/queries/match/match
import { MatchEntity } from '@modules/ad/core/domain/match.entity';
import { MatchMapper } from '@modules/ad/match.mapper';
import { CacheInterceptor, CacheKey } from '@nestjs/cache-manager';
import { Controller, Inject, UseInterceptors, UsePipes } from '@nestjs/common';
import {
Controller,
Inject,
UseFilters,
UseInterceptors,
UsePipes,
} from '@nestjs/common';
import { QueryBus } from '@nestjs/cqrs';
import { GrpcMethod } from '@nestjs/microservices';
import { LogCauseExceptionFilter } from '@src/log-cause.exception-filter';
import { MatchingPaginatedResponseDto } from '../dtos/matching.paginated.response.dto';
import { MatchRequestDto } from './dtos/match.request.dto';
@UseFilters(LogCauseExceptionFilter)
@UsePipes(
new RpcValidationPipe({
whitelist: false,

View File

@ -7,6 +7,7 @@
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"lib": ["es2022"],
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",