From ea55a850f9f8e1dc16008a2574c4cc71f65ae820 Mon Sep 17 00:00:00 2001 From: Jakob Ackermann Date: Tue, 1 Oct 2024 22:54:26 +0200 Subject: [PATCH] Merge pull request #20761 from overleaf/jpa-error-instance [logger] forward an Error instance to Sentry.captureException GitOrigin-RevId: 3a310994622c85f3791181b87d4d382c34f6fd32 --- libraries/logger/sentry-manager.js | 6 +++++- libraries/logger/test/unit/sentry-manager-tests.js | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/libraries/logger/sentry-manager.js b/libraries/logger/sentry-manager.js index c17616294e..15a667fd2a 100644 --- a/libraries/logger/sentry-manager.js +++ b/libraries/logger/sentry-manager.js @@ -81,9 +81,13 @@ class SentryManager { extra.info = error.info delete error.info + // Sentry wants to receive an Error instance. + const errInstance = new Error(error.message) + Object.assign(errInstance, error) + try { // send the error to sentry - this.Sentry.captureException(error, { tags, extra, level }) + this.Sentry.captureException(errInstance, { tags, extra, level }) // put a flag on the errors to avoid reporting them multiple times for (const key in attributes) { diff --git a/libraries/logger/test/unit/sentry-manager-tests.js b/libraries/logger/test/unit/sentry-manager-tests.js index a53483d374..d00c10ca4d 100644 --- a/libraries/logger/test/unit/sentry-manager-tests.js +++ b/libraries/logger/test/unit/sentry-manager-tests.js @@ -138,7 +138,11 @@ describe('SentryManager', function () { }, }) ) - expect(this.Sentry.captureException.args[0][0]).to.deep.equal(expectedErr) + // Chai is very picky with comparing Error instances. Go the long way of comparing all the fields manually. + const gotErr = this.Sentry.captureException.args[0][0] + for (const [key, wanted] of Object.entries(expectedErr)) { + expect(gotErr).to.have.property(key, wanted) + } }) it('should sanitize request', function () { const req = {