Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/app/common/sentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
19 changes: 19 additions & 0 deletions src/tests/common/sentry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
Expand Down
Loading