From 221dc855f496b6e2f3f9678a028629522c3207ce Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Tue, 31 Dec 2019 04:28:14 +0100 Subject: [PATCH] Add option to throw on non-empty RCON responses Add option to FactorioServer.sendRcon to throw on non-empty responses from Factorio. This is useful for detecting errors returned by commands that should not have a response. --- lib/factorio/server.js | 11 +++++++++-- plugins/global_chat/instance.js | 2 +- test/integration/server.js | 7 +++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/factorio/server.js b/lib/factorio/server.js index 831f1173..39effbab 100644 --- a/lib/factorio/server.js +++ b/lib/factorio/server.js @@ -625,15 +625,22 @@ class FactorioServer extends events.EventEmitter { * wait until it is establied and then send the message. * * @param {string} message - message to send to server over RCON. + * @param {boolean} expectEmpty - + * if true throw if the response is not empty. Useful for detecting + * errors that might have been sent in response. * @returns {string} response from server. */ - async sendRcon(message) { + async sendRcon(message, expectEmpty) { this._check(["running"]) if (!this._rconReady) { await events.once(this, 'rcon-ready'); } - return await this._rconClient.send(message); + let response = await this._rconClient.send(message); + if (expectEmpty && response !== '') { + throw new Error(`Expected empty response but got "${response}"`); + } + return response; } /** diff --git a/plugins/global_chat/instance.js b/plugins/global_chat/instance.js index 8d85d1c1..40661eac 100644 --- a/plugins/global_chat/instance.js +++ b/plugins/global_chat/instance.js @@ -34,7 +34,7 @@ class InstancePlugin extends plugin.BaseInstancePlugin { async chatEventHandler(message) { // TODO check if cross server chat is enabled let content = `[${message.data.instance_name}] ${removeTags(message.data.content)}`; - await this.instance.server.sendRcon(`/sc game.print('${escapeLuaString(content)}')`); + await this.instance.server.sendRcon(`/sc game.print('${escapeLuaString(content)}')`, true); } async onOutput(output) { diff --git a/test/integration/server.js b/test/integration/server.js index 327ed2d0..488b7a02 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -129,6 +129,13 @@ describe("Integration of lib/factorio/server", function() { let result = await server.sendRcon("/sc rcon.print('success')"); assert.equal(result, 'success\n'); }); + it("throws on non-empty response when enabled", async function() { + slowTest(this); + await assert.rejects( + server.sendRcon("/sc rcon.print('fail')", true), + new Error('Expected empty response but got "fail\n"') + ); + }); }); describe(".stop()", function() {