mirror of
https://github.com/immich-app/immich.git
synced 2026-06-05 22:35:16 -04:00
refactor!: migrate class-validator to zod (#26597)
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import { ArgumentsHost, Catch, ExceptionFilter, HttpException } from '@nestjs/common';
|
||||
import { Response } from 'express';
|
||||
import { ClsService } from 'nestjs-cls';
|
||||
import { ZodSerializationException, ZodValidationException } from 'nestjs-zod';
|
||||
import { LoggingRepository } from 'src/repositories/logging.repository';
|
||||
import { logGlobalError } from 'src/utils/logger';
|
||||
import { ZodError } from 'zod';
|
||||
|
||||
@Catch()
|
||||
export class GlobalExceptionFilter implements ExceptionFilter<Error> {
|
||||
@@ -41,6 +43,19 @@ export class GlobalExceptionFilter implements ExceptionFilter<Error> {
|
||||
body = { message: body };
|
||||
}
|
||||
|
||||
// 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',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return { status, body };
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user