From 07cff0ab9aa445cc7836efc62c725871bc35411c Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Fri, 12 Jun 2020 15:29:18 +0200 Subject: [PATCH] Fix error during slave authentication failure Treat sending messages over a link that is not connected as a SessionLost error and handle AuthenticationFailed error in the startup logic of slave. --- lib/link/connectors.js | 6 ++++-- slave.js | 11 ++++++++--- test/integration/link.js | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/link/connectors.js b/lib/link/connectors.js index d48b1a58..271fa326 100644 --- a/lib/link/connectors.js +++ b/lib/link/connectors.js @@ -97,7 +97,9 @@ class WebSocketBaseConnector extends events.EventEmitter { * @returns the sequence number of the message sent */ send(type, data) { - this._check(["handshake", "connected", "closing"]); + if (!["handshake", "connected", "closing"].includes(this._state)) { + throw new errors.SessionLost("No session"); + } let message = { seq: this._seq, type, data }; this._sendBuffer.push(message); if (["connected", "closing"].includes(this._state)) { @@ -271,7 +273,7 @@ class WebSocketClientConnector extends WebSocketBaseConnector { console.log(`SOCKET | Close (code: ${code}, reason: ${reason})`); // Authentication failed if (code === 4003) { - this.emit("error", new errors.AuthenticationFailed("Authentication failed")); + this.emit("error", new errors.AuthenticationFailed(reason)); this._state = "closing"; } diff --git a/slave.js b/slave.js index 8b05fa4f..bbca54f8 100644 --- a/slave.js +++ b/slave.js @@ -1275,14 +1275,19 @@ I version of clusterio. Expect things to break. I ` ); startSlave().catch(err => { - console.error(` + if (err instanceof errors.AuthenticationFailed) { + console.error(err.message); + + } else { + console.error(` +--------------------------------------------------------------+ | Unexpected error occured while starting slave, please report | | it to https://github.com/clusterio/factorioClusterio/issues | +--------------------------------------------------------------+` - ); + ); + console.error(err); + } - console.error(err); process.exit(1); }); } diff --git a/test/integration/link.js b/test/integration/link.js index ad9f2056..c160fc30 100644 --- a/test/integration/link.js +++ b/test/integration/link.js @@ -13,7 +13,7 @@ describe("Integration of lib/link", function() { control = new TestControl(controlConnector); await assert.rejects( controlConnector.connect(), - new Error("Authentication failed") + new Error("Authentication failed: jwt malformed") ); });