Skip to content

Commit

Permalink
fix(common): CHECKOUT-4418 Only log exception event if it is raised b…
Browse files Browse the repository at this point in the history
…y error
  • Loading branch information
davidchin committed Sep 16, 2019
1 parent 3fde446 commit 99d156d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
38 changes: 29 additions & 9 deletions src/app/common/error/SentryErrorLogger.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { captureException, init, withScope, BrowserOptions, Exception, Scope, Severity } from '@sentry/browser';
import { captureException, init, withScope, BrowserOptions, Scope, Severity } from '@sentry/browser';
import { RewriteFrames } from '@sentry/integrations';
import { Integration } from '@sentry/types';

Expand Down Expand Up @@ -36,21 +36,26 @@ describe('SentryErrorLogger', () => {
new SentryErrorLogger(config);

const clientOptions: BrowserOptions = (init as jest.Mock).mock.calls[0][0];
const exception: Exception = { type: 'Error', value: '' };

DEFAULT_ERROR_TYPES.forEach(type => {
const event = { exception: { values: [{ ...exception, type }] } };
const event = { exception: { values: [{ type, value: `${type} error message` }] } };
const originalException = new Error(`${type} error message`);

originalException.name = type;

// tslint:disable-next-line:no-non-null-assertion
expect(clientOptions.beforeSend!(event))
expect(clientOptions.beforeSend!(event, { originalException }))
.toEqual(event);
});

['Foo', 'Bar'].forEach(type => {
const event = { exception: { values: [{ ...exception, type }] } };
const event = { exception: { values: [{ type, value: `${type} error message` }] } };
const originalException = new Error(`${type} error message`);

originalException.name = type;

// tslint:disable-next-line:no-non-null-assertion
expect(clientOptions.beforeSend!(event))
expect(clientOptions.beforeSend!(event, { originalException }))
.toEqual(null);
});
});
Expand All @@ -60,17 +65,32 @@ describe('SentryErrorLogger', () => {
new SentryErrorLogger(config, { errorTypes: ['Foo', 'Bar'] });

const clientOptions: BrowserOptions = (init as jest.Mock).mock.calls[0][0];
const exception: Exception = { type: 'Error', value: '' };

['Foo', 'Bar'].forEach(type => {
const event = { exception: { values: [{ ...exception, type }] } };
const event = { exception: { values: [{ type, value: `${type} error message` }] } };
const originalException = new Error(`${type} error message`);

originalException.name = type;

// tslint:disable-next-line:no-non-null-assertion
expect(clientOptions.beforeSend!(event))
expect(clientOptions.beforeSend!(event, { originalException }))
.toEqual(event);
});
});

it('does not log exception event if it is raised by error', () => {
// tslint:disable-next-line:no-unused-expression
new SentryErrorLogger(config);

const clientOptions: BrowserOptions = (init as jest.Mock).mock.calls[0][0];
const event = { exception: { values: [{ type: 'Error', value: 'Unexpected error' }] } };
const hint = { originalException: 'Unexpected error' };

// tslint:disable-next-line:no-non-null-assertion
expect(clientOptions.beforeSend!(event, hint))
.toEqual(null);
});

it('configures client to rewrite filename of error frames', () => {
// tslint:disable-next-line:no-unused-expression
new SentryErrorLogger(config, { publicPath: 'https://cdn.foo.bar' });
Expand Down
9 changes: 6 additions & 3 deletions src/app/common/error/SentryErrorLogger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { captureException, init, withScope, BrowserOptions, Event, Severity, StackFrame } from '@sentry/browser';
import { RewriteFrames } from '@sentry/integrations';
import { EventHint } from '@sentry/types';

import computeErrorCode from './computeErrorCode';
import ErrorLogger, { ErrorLevelType, ErrorLoggerOptions, ErrorTags } from './ErrorLogger';
Expand Down Expand Up @@ -73,9 +74,11 @@ export default class SentryErrorLogger implements ErrorLogger {
}
}

private handleBeforeSend: (event: Event) => Event | null = event => {
if (event.exception && event.exception.values) {
return event.exception.values.some(exception => exception.type && this.errorTypes.indexOf(exception.type) >= 0) ?
private handleBeforeSend: (event: Event, hint?: EventHint) => Event | null = (event, hint) => {
if (event.exception) {
const { originalException = null } = hint || {};

return originalException instanceof Error && this.errorTypes.indexOf(originalException.name) >= 0 ?
event :
null;
}
Expand Down

0 comments on commit 99d156d

Please sign in to comment.