From bf8c2c5ef87a8be6eefe362ac8b375ce3fd8e82e Mon Sep 17 00:00:00 2001 From: Alex Sorafumo Date: Thu, 29 Oct 2026 13:24:22 +1100 Subject: [PATCH] fix(sentry): stop reporting Monaco cancellations when an editor closes Monaco drops the promise from its word highlighter delay. When an editor is disposed during that delay, the promise rejects with "Canceled" and Angular sends it to the error handler. The handler now ignores Monaco cancellations, as VS Code does. Fixes BACKOFFICE-4V Co-Authored-By: Claude Opus 5.5 --- src/app/common/sentry.ts | 17 ++++++++++++++++- src/tests/common/sentry.spec.ts | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/app/common/sentry.ts b/src/app/common/sentry.ts index bd4f66dbb..798e4c3c6 100644 --- a/src/app/common/sentry.ts +++ b/src/app/common/sentry.ts @@ -27,13 +27,28 @@ export function reportError(error: unknown) { if (_queue && _queue.length < MAX_QUEUED_ERRORS) _queue.push(error); } +/** + * True for the error Monaco uses to cancel pending work. Monaco does not + * handle some of these rejections, for example when an editor is disposed + * while a word highlight is pending. They are not failures. + * Same check as Monaco's `isCancellationError`. + */ +function isMonacoCancellation(error: unknown) { + return ( + error instanceof Error && + error.name === 'Canceled' && + error.message === 'Canceled' + ); +} + /** * Angular error handler. Logs errors to the console as the default handler - * does, and sends them to Sentry. + * does, and sends them to Sentry. Ignores Monaco cancellations. */ @Injectable() export class SentryReportingErrorHandler implements ErrorHandler { public handleError(error: unknown) { + if (isMonacoCancellation(error)) return; console.error(error); reportError(error); } diff --git a/src/tests/common/sentry.spec.ts b/src/tests/common/sentry.spec.ts index 260a16715..f3be3fe25 100644 --- a/src/tests/common/sentry.spec.ts +++ b/src/tests/common/sentry.spec.ts @@ -63,6 +63,25 @@ describe('sentry', () => { expect(handle_error).toHaveBeenCalledWith('late'); }); + it('ignores Monaco cancellation errors', async () => { + const { initSentry, SentryReportingErrorHandler } = await loadSentry(); + setAuthorityDsn(undefined); + await initSentry(injector, 'user-1'); + const console_error = vi + .spyOn(console, 'error') + .mockImplementation(() => undefined); + const handler = new SentryReportingErrorHandler(); + const canceled = new Error('Canceled'); + canceled.name = 'Canceled'; + handler.handleError(canceled); + expect(handle_error).not.toHaveBeenCalled(); + expect(console_error).not.toHaveBeenCalled(); + const error = new Error('Canceled'); + handler.handleError(error); + expect(handle_error).toHaveBeenCalledWith(error); + console_error.mockRestore(); + }); + it('does not load Sentry when the authority turns it off', async () => { const { initSentry, reportError } = await loadSentry(); setAuthorityDsn('');