Skip to content

Commit

Permalink
catch error when an error occurs during timeout issue, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
StarpTech committed Nov 27, 2016
1 parent 6d5a659 commit 4d1ae67
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
33 changes: 24 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}

}
}

})
Expand Down
42 changes: 42 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4d1ae67

Please sign in to comment.