Skip to content

Commit da07a85

Browse files
watsonStephen Belanger
authored andcommitted
fix: don't fail if captureError callback isn't provided (#580)
1 parent c76c6d7 commit da07a85

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

lib/agent.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,18 +317,18 @@ Agent.prototype.captureError = function (err, opts, cb) {
317317

318318
if (!error) {
319319
agent.logger.debug('error ignored by filter %o', { id: id })
320-
cb(null, id)
320+
if (cb) cb(null, id)
321321
return
322322
}
323323

324324
if (agent._apmServer) {
325325
agent.logger.info(`Sending error ${id} to Elastic APM`)
326326
agent._apmServer.sendError(error, function () {
327327
agent._apmServer.flush(function (err) {
328-
cb(err, id)
328+
if (cb) cb(err, id)
329329
})
330330
})
331-
} else {
331+
} else if (cb) {
332332
// TODO: Swallow this error just as it's done in agent.flush()?
333333
process.nextTick(cb.bind(null, new Error('cannot capture error before agent is started'), id))
334334
}

test/agent.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,13 +506,19 @@ test('#captureError()', function (t) {
506506
})
507507
})
508508

509-
t.test('capture error before agent is started', function (t) {
509+
t.test('capture error before agent is started - with callback', function (t) {
510510
var agent = Agent()
511511
agent.captureError(new Error('foo'), function (err) {
512512
t.equal(err.message, 'cannot capture error before agent is started')
513513
t.end()
514514
})
515515
})
516+
517+
t.test('capture error before agent is started - without callback', function (t) {
518+
var agent = Agent()
519+
agent.captureError(new Error('foo'))
520+
t.end()
521+
})
516522
})
517523

518524
test('#handleUncaughtExceptions()', function (t) {

0 commit comments

Comments
 (0)