diff --git a/lib/index.js b/lib/index.js index 59b8112a5..f416d991b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -50,14 +50,14 @@ var //Errors var - HemeraError = SuperError.subclass('HemeraError'), - ParseError = HemeraError.subclass('HemeraParseError'), - TimeoutError = HemeraError.subclass('TimeoutError'), - ImplementationError = HemeraError.subclass('ImplementationError'), - BusinessError = HemeraError.subclass('BusinessError'), - FatalError = HemeraError.subclass('FatalError'), - PatternNotFound = HemeraError.subclass('PatternNotFound'), - PayloadValidationError = SuperError.subclass('PayloadValidationError') + HemeraError = SuperError.subclass('HemeraError'), + ParseError = HemeraError.subclass('HemeraParseError'), + TimeoutError = HemeraError.subclass('TimeoutError'), + ImplementationError = HemeraError.subclass('ImplementationError'), + BusinessError = HemeraError.subclass('BusinessError'), + FatalError = HemeraError.subclass('FatalError'), + PatternNotFound = HemeraError.subclass('PatternNotFound'), + PayloadValidationError = SuperError.subclass('PayloadValidationError') //Config @@ -508,7 +508,22 @@ class Hemera extends EventEmitter { this.log().error(error, pattern) if (typeof cb === 'function') { - cb(error) + + try { + + cb(error) + } catch (err) { + + let error = new FatalError().causedBy(err) + this.log().fatal(error) + + //Let it crash + if (this.config.crashOnFatal) { + + this.fatal() + } + + } } }) diff --git a/test/index.spec.js b/test/index.spec.js index 152b261b7..f6f12d3d2 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -662,6 +662,48 @@ describe('Error handling', function () { }) + it('Should crash when an unexpected error thrown during timeout issue', function (done) { + + const nats = require('nats').connect(authUrl) + + const hemera = new Hemera(nats, { + crashOnFatal: true, + timeout: 20 + }) + + var stub = Sinon.stub(hemera, "fatal") + + stub.onCall(1) + + stub.returns(true) + + hemera.ready(() => { + + + hemera.act({ + topic: 'email', + cmd: 'send', + email: 'foobar@gmail.com', + msg: 'Hi!' + }, (err, resp) => { + + //Fatal Error will be throw after the server proceed the msg + setTimeout(() => { + expect(stub.called).to.be.equals(true) + stub.restore() + hemera.close() + done() + }, 500) + + throw (new Error('Test')) + + + }) + + }) + + }) + it('Should crash on unhandled business errors', function (done) { const nats = require('nats').connect(authUrl)