Include schema validation errors in InvalidMessage stack

Generic error handlers such as FactorioServer.handle() and the event
handler catch in Link only log err.stack, so the ajv errors attached to
an InvalidMessage were lost unless the catch site checked for the class
explicitly. Appending the errors to the stack in the constructor makes
every stack-logging handler show which field failed validation without
special casing each one.

Fixes #986

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
bbassieandClaude Fable 5.1 committed 2026-09-09 11:57:05 +00:00
1 parent abfc5ff8d5
commit dc129b2fad
5 files changed
+95 -2

No files matched your search

+24
View File
@@ -0,0 +1,24 @@
"use strict";
const assert = require("assert").strict;
const lib = require("@clusterio/lib");
describe("lib/errors", function() {
describe("class InvalidMessage", function() {
it("should include validation errors in the stack", function() {
let errors = [{ instancePath: "/value", message: "must be number" }];
let err = new lib.InvalidMessage("Request Test failed validation", errors);
assert.equal(err.message, "Request Test failed validation");
assert.equal(err.errors, errors);
assert(err.stack.startsWith("Error: Request Test failed validation\n"));
assert(err.stack.endsWith(`\n${JSON.stringify(errors, null, "\t")}`));
});
it("should leave the stack alone without errors", function() {
let err = new lib.InvalidMessage("Unrecognized request Test");
assert.equal(err.errors, undefined);
assert(!err.stack.includes("\n["));
let nullErr = new lib.InvalidMessage("Unrecognized request Test", null);
assert.equal(nullErr.errors, null);
});
});
});