2023-11-03 21:33:15 -04:00
|
|
|
import {
|
|
|
|
|
CallHandler,
|
|
|
|
|
ExecutionContext,
|
|
|
|
|
HttpException,
|
2024-04-15 19:39:06 -04:00
|
|
|
Inject,
|
2023-11-03 21:33:15 -04:00
|
|
|
Injectable,
|
|
|
|
|
InternalServerErrorException,
|
|
|
|
|
NestInterceptor,
|
|
|
|
|
} from '@nestjs/common';
|
|
|
|
|
import { Observable, catchError, throwError } from 'rxjs';
|
2024-04-15 19:39:06 -04:00
|
|
|
import { ILoggerRepository } from 'src/interfaces/logger.interface';
|
2024-09-04 13:32:43 -04:00
|
|
|
import { logGlobalError } from 'src/utils/logger';
|
2024-06-06 10:09:42 -04:00
|
|
|
import { routeToErrorMessage } from 'src/utils/misc';
|
2023-11-03 21:33:15 -04:00
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class ErrorInterceptor implements NestInterceptor {
|
2024-04-15 19:39:06 -04:00
|
|
|
constructor(@Inject(ILoggerRepository) private logger: ILoggerRepository) {
|
|
|
|
|
this.logger.setContext(ErrorInterceptor.name);
|
|
|
|
|
}
|
2023-11-03 21:33:15 -04:00
|
|
|
|
2024-03-05 23:23:06 +01:00
|
|
|
intercept(context: ExecutionContext, next: CallHandler<any>): Observable<any> {
|
2023-11-03 21:33:15 -04:00
|
|
|
return next.handle().pipe(
|
|
|
|
|
catchError((error) =>
|
|
|
|
|
throwError(() => {
|
2024-06-06 10:09:42 -04:00
|
|
|
if (error instanceof HttpException) {
|
2023-11-03 21:33:15 -04:00
|
|
|
return error;
|
|
|
|
|
}
|
2024-06-06 10:09:42 -04:00
|
|
|
|
2024-09-04 13:32:43 -04:00
|
|
|
logGlobalError(this.logger, error);
|
|
|
|
|
|
|
|
|
|
const message = routeToErrorMessage(context.getHandler().name);
|
|
|
|
|
return new InternalServerErrorException(message);
|
2023-11-03 21:33:15 -04:00
|
|
|
}),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|