File

src/shared/common/filters/validation-error.filter.ts

Description

Global exception filter for handling ValidationError from @openid4vc/utils. Catches all ValidationError instances, formats them consistently, and returns proper HTTP responses.

Implements

ExceptionFilter

Index

Properties
Methods

Properties

Private Readonly logger
Type : unknown
Default value : new Logger(ValidationErrorFilter.name)

Methods

catch
catch(exception: ValidationError, host: ArgumentsHost)
Parameters :
Name Type Optional
exception ValidationError No
host ArgumentsHost No
Returns : void
import {
    ArgumentsHost,
    Catch,
    ExceptionFilter,
    HttpStatus,
    Logger,
} from "@nestjs/common";
import { ValidationError } from "@openid4vc/utils";
import { Response } from "express";

/**
 * Global exception filter for handling ValidationError from @openid4vc/utils.
 * Catches all ValidationError instances, formats them consistently, and returns proper HTTP responses.
 */
@Catch(ValidationError)
export class ValidationErrorFilter implements ExceptionFilter {
    private readonly logger = new Logger(ValidationErrorFilter.name);

    catch(exception: ValidationError, host: ArgumentsHost) {
        const ctx = host.switchToHttp();
        const response = ctx.getResponse<Response>();

        // Format Zod validation errors for better readability
        const issues = exception.zodError?.issues;

        // Log with full details (no [Array] truncation)
        this.logger.error(
            `Validation error: ${JSON.stringify(issues, null, 2)}`,
        );

        // Return formatted error response
        response.status(HttpStatus.BAD_REQUEST).json({
            statusCode: HttpStatus.BAD_REQUEST,
            message: "Validation failed",
            errors: issues,
            timestamp: new Date().toISOString(),
        });
    }
}

results matching ""

    No results matching ""