Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return relevant error code in logger interceptor #2245

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
PooyaRaki marked this conversation as resolved.
Show resolved Hide resolved
}

const message = formatRouteLogMessage(
Expand Down
Loading