diff --git a/providers/logging/logger.js b/providers/logging/logger.js index ed84b923..f4a7dd4e 100644 --- a/providers/logging/logger.js +++ b/providers/logging/logger.js @@ -39,28 +39,32 @@ class ApplicationInsightsTransport extends Transport { return } - const properties = buildProperties(info) - if (info.level === 'error') { - if (info.stack) { - const err = new Error(info.message) - err.stack = info.stack - this.aiClient.trackException({ exception: err, properties }) + try { + const properties = buildProperties(info) + if (info.level === 'error') { + if (info.stack) { + const err = new Error(info.message) + err.stack = info.stack + this.aiClient.trackException({ exception: err, properties }) + } else { + this.aiClient.trackTrace({ + message: info.message, + severity: appInsights.KnownSeverityLevel.Error, + properties + }) + } } else { this.aiClient.trackTrace({ message: info.message, - severity: appInsights.KnownSeverityLevel.Error, + severity: mapLevel(info.level), properties }) } - } else { - this.aiClient.trackTrace({ - message: info.message, - severity: mapLevel(info.level), - properties - }) + } catch (err) { + console.error('ApplicationInsights telemetry failed', err) + } finally { + callback() } - - callback() } } diff --git a/test/unit/providers/logging/loggerTests.js b/test/unit/providers/logging/loggerTests.js index 59a65d90..77a2dc74 100644 --- a/test/unit/providers/logging/loggerTests.js +++ b/test/unit/providers/logging/loggerTests.js @@ -63,4 +63,32 @@ describe('logger', function () { expect(trackTrace.firstCall.args[0].message).to.contain('plain error without stack') expect(trackTrace.firstCall.args[0].severity).to.equal('Error') }) + + it('still calls callback if trackTrace throws', done => { + const consoleError = sinon.stub(console, 'error') + trackTrace.throws(new Error('AI unavailable')) + const logger = factory({ aiClient }) + logger.info('test message') + clock.runAll() + setImmediate(() => { + expect(trackTrace.callCount).to.equal(1) + consoleError.restore() + done() + }) + clock.runAll() + }) + + it('still calls callback if trackException throws', done => { + const consoleError = sinon.stub(console, 'error') + trackException.throws(new Error('AI unavailable')) + const logger = factory({ aiClient }) + logger.log({ level: 'error', message: 'boom', stack: 'Error: boom\n at test:1:1' }) + clock.runAll() + setImmediate(() => { + expect(trackException.callCount).to.equal(1) + consoleError.restore() + done() + }) + clock.runAll() + }) })