mirror of
https://github.com/immich-app/immich.git
synced 2026-06-06 14:55:17 -04:00
refactor(server)!: remove redundant error and statusCode fields from error responses (#28140)
* refactor(server)!: remove redundant error and statusCode fields from error responses * use enum * enhance response management * chore: clean up header * fix: chaining * refactor: handle error * fix e2e tests --------- Co-authored-by: Jason Rasmussen <jason@rasm.me>
This commit is contained in:
@@ -2,6 +2,7 @@ import { ArgumentsHost, Catch, ExceptionFilter, HttpException } from '@nestjs/co
|
||||
import { Response } from 'express';
|
||||
import { ClsService } from 'nestjs-cls';
|
||||
import { ZodSerializationException, ZodValidationException } from 'nestjs-zod';
|
||||
import { ImmichHeader } from 'src/enum';
|
||||
import { LoggingRepository } from 'src/repositories/logging.repository';
|
||||
import { logGlobalError } from 'src/utils/logger';
|
||||
import { ZodError } from 'zod';
|
||||
@@ -16,20 +17,13 @@ export class GlobalExceptionFilter implements ExceptionFilter<Error> {
|
||||
}
|
||||
|
||||
catch(error: Error, host: ArgumentsHost) {
|
||||
const ctx = host.switchToHttp();
|
||||
const response = ctx.getResponse<Response>();
|
||||
const { status, body } = this.fromError(error);
|
||||
if (!response.headersSent) {
|
||||
response.header('X-Correlation-ID', this.cls.getId());
|
||||
response.status(status).json({ ...body, statusCode: status });
|
||||
}
|
||||
this.handleError(host.switchToHttp().getResponse<Response>(), error);
|
||||
}
|
||||
|
||||
handleError(res: Response, error: Error) {
|
||||
const { status, body } = this.fromError(error);
|
||||
if (!res.headersSent) {
|
||||
res.header('X-Correlation-ID', this.cls.getId());
|
||||
res.status(status).json({ ...body, statusCode: status });
|
||||
res.header(ImmichHeader.CorrelationId, this.cls.getId()).status(status).json(body);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,26 +32,24 @@ export class GlobalExceptionFilter implements ExceptionFilter<Error> {
|
||||
|
||||
if (error instanceof HttpException) {
|
||||
const status = error.getStatus();
|
||||
let body = error.getResponse();
|
||||
|
||||
// unclear what circumstances would return a string
|
||||
if (typeof body === 'string') {
|
||||
body = { message: body };
|
||||
}
|
||||
const response = error.getResponse();
|
||||
const body: Record<string, unknown> =
|
||||
typeof response === 'string' ? { message: response } : { ...(response as object) };
|
||||
|
||||
// handle both request and response validation errors
|
||||
if (error instanceof ZodValidationException || error instanceof ZodSerializationException) {
|
||||
const zodError = error.getZodError();
|
||||
if (zodError instanceof ZodError && zodError.issues.length > 0) {
|
||||
body = {
|
||||
message: zodError.issues.map((issue) =>
|
||||
issue.path.length > 0 ? `[${issue.path.join('.')}] ${issue.message}` : issue.message,
|
||||
),
|
||||
error: 'Bad Request',
|
||||
};
|
||||
body['message'] = zodError.issues.map((issue) =>
|
||||
issue.path.length > 0 ? `[${issue.path.join('.')}] ${issue.message}` : issue.message,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// remove fields that duplicate the HTTP response line or will be reformatted in a later step
|
||||
delete body['error'];
|
||||
delete body['statusCode'];
|
||||
delete body['errors'];
|
||||
return { status, body };
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user