Skip to content

Commit

Permalink
Return relevant error code in logger interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
PooyaRaki committed Jan 7, 2025
1 parent 79ebc89 commit f744597
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions src/routes/common/interceptors/route-logger.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import { ILoggingService, LoggingService } from '@/logging/logging.interface';
import { Inject } from '@nestjs/common/decorators';
import { Observable, tap } from 'rxjs';
import { formatRouteLogMessage } from '@/logging/utils';
import { DataSourceError } from '@/domain/errors/data-source.error';
import { Request, Response } from 'express';
import isNumber from 'lodash/isNumber';
import { ZodErrorWithCode } from '@/validation/pipes/validation.pipe';
import { ZodError } from 'zod';

/**
* The {@link RouteLoggerInterceptor} is an interceptor that logs the requests
Expand Down Expand Up @@ -64,17 +63,13 @@ export class RouteLoggerInterceptor implements NestInterceptor {
* @private
*/
private onError(request: Request, error: Error, startTimeMs: number): void {
let statusCode;
if (error instanceof HttpException) {
statusCode = error.getStatus();
} else if (error instanceof DataSourceError) {
statusCode = error.code ?? HttpStatus.INTERNAL_SERVER_ERROR;
} else if (error instanceof ZodErrorWithCode) {
statusCode = error.code;
} else if ('code' in error && isNumber(error.code)) {
let statusCode: number = HttpStatus.INTERNAL_SERVER_ERROR;
if ('code' in error && isNumber(error.code)) {
statusCode = error.code;
} else {
statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
} else if (error instanceof HttpException) {
statusCode = error.getStatus();
} else if (error instanceof ZodError) {
statusCode = HttpStatus.UNPROCESSABLE_ENTITY;
}

const message = formatRouteLogMessage(
Expand Down

0 comments on commit f744597

Please sign in to comment.