mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-13 23:14:53 +09:00
Pass validation errors with InvalidMessage error
Pass the array describing the validation errors occuring with the InvalidMessage error throw and print it to the console.
This commit is contained in:
1 parent
0a9fff7578
commit
43ce6aa58c
3 files changed
+14
-7
No files matched your search
+6
-1
@@ -7,7 +7,12 @@ class CommandError extends Error { }
|
||||
class RequestError extends Error { }
|
||||
|
||||
// Signal for messages that fail validation
|
||||
class InvalidMessage extends Error { }
|
||||
class InvalidMessage extends Error {
|
||||
constructor(msg, errors) {
|
||||
super(msg);
|
||||
this.errors = errors || null;
|
||||
}
|
||||
}
|
||||
|
||||
// Errror class for known errors occuring during startup
|
||||
class StartupError extends Error { }
|
||||
|
||||
+6
-4
@@ -33,6 +33,10 @@ class Link {
|
||||
} catch (err) {
|
||||
if (err instanceof errors.InvalidMessage) {
|
||||
this.connector.close(`Invalid message: ${err.message}`);
|
||||
console.error(`Invalid message on ${this.source}-${this.target} link: ${err.message}`)
|
||||
if (err.errors) {
|
||||
console.error(err.errors);
|
||||
}
|
||||
} else {
|
||||
this.connector.emit('error', err);
|
||||
}
|
||||
@@ -57,7 +61,7 @@ class Link {
|
||||
*/
|
||||
processMessage(message) {
|
||||
if (!schema.message(message)) {
|
||||
throw new errors.InvalidMessage("Malformed");
|
||||
throw new errors.InvalidMessage("Malformed", schema.message.errors);
|
||||
}
|
||||
|
||||
let validator = this._validators.get(message.type);
|
||||
@@ -66,9 +70,7 @@ class Link {
|
||||
}
|
||||
|
||||
if (!validator(message)) {
|
||||
// TODO better logging of errors encountered validating
|
||||
console.error(validator.errors);
|
||||
throw new errors.InvalidMessage(`Validation failed for ${message.type}`);
|
||||
throw new errors.InvalidMessage(`Validation failed for ${message.type}`, validator.errors);
|
||||
}
|
||||
|
||||
let hadHandlers = this._processHandler(message);
|
||||
|
||||
@@ -90,7 +90,7 @@ describe("lib/link/link", function() {
|
||||
it("should throw on invalid message", function() {
|
||||
assert.throws(
|
||||
() => testLink.processMessage({ data: "invalid message" }),
|
||||
new errors.InvalidMessage("Malformed")
|
||||
{ message: "Malformed" }
|
||||
);
|
||||
});
|
||||
it("should throw on message without validator", function() {
|
||||
@@ -102,7 +102,7 @@ describe("lib/link/link", function() {
|
||||
it("should throw on message failing validation", function() {
|
||||
assert.throws(
|
||||
() => testLink.processMessage({ seq: 1, type: 'simple', data: { string: 1 }}),
|
||||
new errors.InvalidMessage("Validation failed for simple")
|
||||
{ message: "Validation failed for simple" }
|
||||
);
|
||||
});
|
||||
it("should throw on unhandled message", function() {
|
||||
|
||||
Reference in new issue
Block a user