Yggdrasil/errors/ValidationError.js
azures04 0b1662d8ca Initial project structure and core modules
Add environment example, update .gitignore, and switch license to AGPL v3. Introduce error handling classes, ESLint config, and main modules for database, logging, certificate management, and utility functions. Add authentication routes, schemas, and service layer for a modular REST API. Update README and set up repository structure for further development.
2025-12-23 15:59:43 +01:00

53 lines
1.5 KiB
JavaScript

const path = require("node:path")
const DefaultError = require("./DefaultError")
const Logger = require("../modules/logger")
const logger = Logger.createLogger(path.join(__dirname, ".."))
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"}) ` +
`<IP:${ip || "Unknown"}>`,
["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