const path = require("node:path") const DefaultError = require("./DefaultError") const logger = require("../modules/logger") class ValidationError extends DefaultError { constructor(zodResult, config = {}, context = {}) { const formattedErrors = zodResult.error.issues.map(e => ({ field: e.path.join("."), message: e.message })) const message = config.message || "Validation failed" const statusCode = config.code || 400 super(statusCode, message, { errors: formattedErrors }) this.config = config this.formattedErrors = formattedErrors this.context = context this.logError() } logError() { const { method, path, ip } = this.context if (method && path) { logger.warn( `Validation failed for ${method} ${path} (${this.config.errorFormat || "Standard"}) ` + ``, ["WEB", "yellow"] ) } } serialize() { if (this.config.errorFormat === "YggdrasilError") { return { error: this.config.errorName || "IllegalArgumentException", errorMessage: this.message, cause: JSON.stringify(this.formattedErrors) } } return { code: this.code, message: this.message, errors: this.formattedErrors } } } module.exports = ValidationError