Add more context to missing handler error

Amend the error message shown when a message handler is missing from a
plugin with the expected name of the handler and the name of the plugin
it's missing from.
This commit is contained in:
Hornwitser committed 2020-11-12 04:17:52 +01:00
1 parent 5b74432ee3
commit 65a25e2510
3 files changed
+47 -10

No files matched your search

+30 -3
View File
@@ -3,6 +3,25 @@
const libSchema = require("@clusterio/lib/schema");
const libErrors = require("@clusterio/lib/errors");
class MissingLinkHandlerError extends Error {
constructor(type, source, target) {
super();
this.code = "MISSING_LINK_HANDLER";
this.type = type;
this.source = source;
this.target = target;
this.handler = null;
this.plugin = null;
}
get message() {
let handler = this.handler ? `${this.handler}` : "handler";
let plugin = this.plugin ? ` on plugin ${this.plugin}` : "";
return `Missing ${handler}${plugin} for ${this.type} on ${this.source}-${this.target} link`;
}
}
/**
* Represents a message that can be sent over the link
*
@@ -168,7 +187,7 @@ class Request extends Message {
}
if (!handler) {
throw new Error(`Missing handler for ${this.requestType} on ${link.source}-${link.target} link`);
throw new MissingLinkHandlerError(this.requestType, link.source, link.target);
}
// Check permission if this is a handler for a control connection on the master
@@ -766,7 +785,7 @@ class Event extends Message {
}
if (!handler) {
throw new Error(`Missing handler for ${this.eventType} on ${link.source}-${link.target} link`);
throw new MissingLinkHandlerError(this.eventType, link.source, link.target);
}
link.setHandler(this.eventType, message => {
@@ -959,7 +978,15 @@ messages.playerEvent = new Event({
*/
function attachAllMessages(link) {
for (let [name, message] of Object.entries(messages)) {
message.attach(link, link[name + message.handlerSuffix]);
let handler = `${name}${message.handlerSuffix}`;
try {
message.attach(link, link[handler]);
} catch (err) {
if (err.code === "MISSING_LINK_HANDLER") {
err.handler = handler;
}
throw err;
}
}
}
+16 -6
View File
@@ -402,13 +402,23 @@ function attachPluginMessages(link, pluginInfo, plugin) {
}
let handler = name + messageFormat.handlerSuffix;
if (plugin === null || !plugin[handler]) {
messageFormat.attach(link);
try {
if (plugin === null || !plugin[handler]) {
messageFormat.attach(link);
} else {
messageFormat.attach(link, async function(message, format) {
return await plugin[handler](message, format, this);
});
} else {
messageFormat.attach(link, async function(message, format) {
return await plugin[handler](message, format, this);
});
}
} catch (err) {
if (err.code === "MISSING_LINK_HANDLER") {
err.plugin = pluginInfo.name;
err.handler = handler;
}
throw err;
}
}
}
+1 -1
View File
@@ -55,7 +55,7 @@ describe("lib/plugin", function() {
it("should throw if missing handler for the given message", function() {
assert.throws(
() => libPlugin.attachPluginMessages(mockLink, { name: "test", messages: { mockEvent }}, {}),
new Error("Missing handler for test:test_event on source-target link")
new Error("Missing mockEventEventHandler on plugin test for test:test_event on source-target link")
);
});
it("should throw if message starts with the wrong prefix", function() {