Yggdrasil/errors/ValidationError.js
azures04 3d0f5c54af Add session join schema and enhance error serialization
Introduces a Zod schema for the Minecraft session join endpoint, validating accessToken, selectedProfile, and serverId. Updates ValidationError to support serialization via YggdrasilError and SessionError for improved error formatting.
2025-12-24 04:54:53 +01:00

65 lines
1.9 KiB
JavaScript

const DefaultError = require("./DefaultError")
const YggdrasilError = require("./YggdrasilError")
const SessionError = require("./SessionError")
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") {
const err = new YggdrasilError(
this.code,
this.config.errorName || "IllegalArgumentException",
this.message,
JSON.stringify(this.formattedErrors)
)
return err.serialize()
}
if (this.config.errorFormat === "SessionError") {
const err = new SessionError(
this.code,
this.config.errorName || "Forbidden",
this.message,
this.context.path || ""
)
return err.serialize()
}
return {
code: this.code,
message: this.message,
errors: this.formattedErrors
}
}
}
module.exports = ValidationError