Merge pull request #6 from sharelatex/bg-avoid-reporting-errors-more-than-once

avoid reporting the same error object more than once
This commit is contained in:
Brian Gough 2018-11-06 10:48:16 +00:00 committed by GitHub
commit adb081f556
2 changed files with 21 additions and 0 deletions

View file

@ -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

View file

@ -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"