Yggdrasil/errors/ValidationError.js
azures04 2519d8078a Refactor logger usage and add userRepository module
Replaces custom logger instantiation with a shared logger import across modules and routes. Moves player property and privilege management from authRepository to a new userRepository, expanding userRepository with additional user management functions (ban, unban, preferences, privileges, bans). Updates service and route files to use userRepository where appropriate. Adds new session join route and schema, and utility for UUID formatting.
2025-12-24 04:22:43 +01:00

52 lines
1.5 KiB
JavaScript

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"}) ` +
`<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