From f7567cd2f9e3f8db404eaa875e26f6494eb2ccaa Mon Sep 17 00:00:00 2001 From: nioc Date: Sun, 24 Nov 2019 19:33:54 +0100 Subject: [PATCH] Better handling XMPP start error --- lib/xmpp/index.js | 2 +- test/xmpp.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/xmpp/index.js b/lib/xmpp/index.js index 8c6f22c..71b4311 100644 --- a/lib/xmpp/index.js +++ b/lib/xmpp/index.js @@ -168,7 +168,7 @@ module.exports = (logger, config) => { // connect xmppClient.start() .catch((error) => { - logger.error('XMPP client encountered following error at connection', error) + logger.error('XMPP client encountered following error at connection:', error.message) }) return this diff --git a/test/xmpp.js b/test/xmpp.js index 7b9b73c..cb05abf 100644 --- a/test/xmpp.js +++ b/test/xmpp.js @@ -100,6 +100,36 @@ describe('XMPP component', () => { }) }) + describe('Connect to XMPP server but fail to start', () => { + let xmppStartStub + beforeEach(async () => { + xmppStartStub = sinon.stub().rejects(new Error('Stubed error message')) + mock('@xmpp/client', { + client: () => { + this.start = xmppStartStub + this.stop = xmppCloseStub + this.send = xmppSendStub + this.on = (eventName, callback) => { + simpleXmppEvents.on(eventName, callback) + } + return this + }, + xml: require('@xmpp/xml'), + jid: require('@xmpp/jid') + }) + xmpp = require('./../lib/xmpp')(logger, config) + }) + it('Should log error', (done) => { + require('fs').readFile(config.logger.file.path + config.logger.file.filename, 'utf8', (err, data) => { + if (err) { + throw err + } + data.should.match(new RegExp('XMPP client encountered following error at connection: Stubed error message' + '\n$')) + done() + }) + }) + }) + describe('Bot receive a presence stanza from someone', () => { beforeEach(async () => { await simpleXmppEvents.emit('stanza', xml(