You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.3 KiB
46 lines
1.3 KiB
import {
|
|
ExecutionContext,
|
|
HttpException,
|
|
Injectable,
|
|
NestInterceptor,
|
|
} from '@nestjs/common';
|
|
import { Observable } from 'rxjs';
|
|
import { catchError } from 'rxjs/operators';
|
|
import { LoggerService } from './logger.service';
|
|
|
|
@Injectable()
|
|
export class LoggerExceptionInterceptor implements NestInterceptor {
|
|
constructor(private loggerService: LoggerService) {}
|
|
|
|
intercept(
|
|
context: ExecutionContext,
|
|
call$: Observable<any>,
|
|
): Observable<any> {
|
|
const request = context.switchToHttp().getRequest();
|
|
|
|
return call$.pipe(
|
|
catchError(exception => {
|
|
if (exception instanceof HttpException) {
|
|
// If 500, log as error
|
|
if (500 <= exception.getStatus())
|
|
this.loggerService.error(
|
|
'HttpException ' + exception.getStatus(),
|
|
request.path,
|
|
exception.getResponse(),
|
|
);
|
|
// Else log as debug (we don't want 4xx errors in production)
|
|
else {
|
|
this.loggerService.debug(
|
|
'HttpException ' + exception.getStatus(),
|
|
request.path,
|
|
exception.getResponse(),
|
|
);
|
|
}
|
|
} else {
|
|
this.loggerService.error('Unexpected error', request.path, exception);
|
|
}
|
|
throw exception;
|
|
}),
|
|
);
|
|
}
|
|
}
|