From 65a25e25100d90944349806518757cf147c2ebd7 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Thu, 12 Nov 2020 04:17:52 +0100 Subject: [PATCH] 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. --- packages/lib/link/messages.js | 33 ++++++++++++++++++++++++++++++--- packages/lib/plugin.js | 22 ++++++++++++++++------ test/lib/plugin.js | 2 +- 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/packages/lib/link/messages.js b/packages/lib/link/messages.js index f486f15e..dab4fefd 100644 --- a/packages/lib/link/messages.js +++ b/packages/lib/link/messages.js @@ -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; + } } } diff --git a/packages/lib/plugin.js b/packages/lib/plugin.js index 5444ba23..f5447f3a 100644 --- a/packages/lib/plugin.js +++ b/packages/lib/plugin.js @@ -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; } } } diff --git a/test/lib/plugin.js b/test/lib/plugin.js index 2ca64415..bfe7fa93 100644 --- a/test/lib/plugin.js +++ b/test/lib/plugin.js @@ -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() {