diff --git a/src/log-cause.exception-filter.ts b/src/log-cause.exception-filter.ts new file mode 100644 index 0000000..67e4bac --- /dev/null +++ b/src/log-cause.exception-filter.ts @@ -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 { + 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; + } +} diff --git a/src/modules/ad/core/application/queries/match/match.query.ts b/src/modules/ad/core/application/queries/match/match.query.ts index 44a25b7..e5fda6f 100644 --- a/src/modules/ad/core/application/queries/match/match.query.ts +++ b/src/modules/ad/core/application/queries/match/match.query.ts @@ -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; }; diff --git a/src/modules/ad/interface/grpc-controllers/match.grpc-controller.ts b/src/modules/ad/interface/grpc-controllers/match.grpc-controller.ts index 35dd9bf..542db94 100644 --- a/src/modules/ad/interface/grpc-controllers/match.grpc-controller.ts +++ b/src/modules/ad/interface/grpc-controllers/match.grpc-controller.ts @@ -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, diff --git a/tsconfig.json b/tsconfig.json index 58c4a31..bf94438 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,6 +7,7 @@ "experimentalDecorators": true, "allowSyntheticDefaultImports": true, "target": "es2017", + "lib": ["es2022"], "sourceMap": true, "outDir": "./dist", "baseUrl": "./",