From 3676f78ff9b259ffc375b0415bd23731335ab8a2 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 2 Nov 2018 15:53:21 +0000 Subject: [PATCH] avoid reporting the same error object more than once --- libraries/logger/logging-manager.coffee | 6 ++++++ .../test/unit/coffee/loggingManagerTests.coffee | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/libraries/logger/logging-manager.coffee b/libraries/logger/logging-manager.coffee index a2e2616193..95fa048740 100644 --- a/libraries/logger/logging-manager.coffee +++ b/libraries/logger/logging-manager.coffee @@ -19,6 +19,9 @@ module.exports = Logger = attributes = {err: new Error(attributes)} # extract any error object error = attributes.err or attributes.error + # avoid reporting errors twice + for key, value of attributes + return if value instanceof Error && value.reportedToSentry # include our log message in the error report if not error? error = {message: message} if typeof message is 'string' @@ -50,6 +53,9 @@ module.exports = Logger = # send the error to sentry try @raven.captureException(error, {tags: tags, extra: extra, level: level}) + # put a flag on the errors to avoid reporting them multiple times + for key, value of attributes + value.reportedToSentry = true if value instanceof Error catch return # ignore any errors diff --git a/libraries/logger/test/unit/coffee/loggingManagerTests.coffee b/libraries/logger/test/unit/coffee/loggingManagerTests.coffee index 307bca4a1e..3b50a880bb 100644 --- a/libraries/logger/test/unit/coffee/loggingManagerTests.coffee +++ b/libraries/logger/test/unit/coffee/loggingManagerTests.coffee @@ -1,3 +1,6 @@ +# run this with +# ./node_modules/.bin/mocha --reporter tap --compilers coffee:coffee-script/register test/unit/coffee/loggingManagerTests.coffee + require('coffee-script') chai = require('chai') should = chai.should() @@ -24,6 +27,18 @@ describe 'logger.error', -> @logger.error {foo:'bar'}, "message" @captureException.called.should.equal true + it 'should report the same error to sentry only once', () -> + error1 = new Error('this is the error') + @logger.error {foo: error1}, "first message" + @logger.error {bar: error1}, "second message" + @captureException.callCount.should.equal 1 + + it 'should report two different errors to sentry individually', () -> + error1 = new Error('this is the error') + error2 = new Error('this is the error') + @logger.error {foo: error1}, "first message" + @logger.error {bar: error2}, "second message" + @captureException.callCount.should.equal 2 it 'for multiple errors should only report a maximum of 5 errors to sentry', () -> @logger.error {foo:'bar'}, "message"