Skip to content

Commit edba6fb

Browse files
authored
Merge branch 'master' into jb/bundle/drop-severity
2 parents 2dc1d7a + 21ea870 commit edba6fb

File tree

2 files changed

+38
-71
lines changed

2 files changed

+38
-71
lines changed

Diff for: packages/browser/src/integrations/linkederrors.ts

+28-28
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import { computeStackTrace } from '../tracekit';
88
const DEFAULT_KEY = 'cause';
99
const DEFAULT_LIMIT = 5;
1010

11+
interface LinkedErrorsOptions {
12+
key: string;
13+
limit: number;
14+
}
15+
1116
/** Adds SDK info to an event. */
1217
export class LinkedErrors implements Integration {
1318
/**
@@ -23,17 +28,17 @@ export class LinkedErrors implements Integration {
2328
/**
2429
* @inheritDoc
2530
*/
26-
private readonly _key: string;
31+
private readonly _key: LinkedErrorsOptions['key'];
2732

2833
/**
2934
* @inheritDoc
3035
*/
31-
private readonly _limit: number;
36+
private readonly _limit: LinkedErrorsOptions['limit'];
3237

3338
/**
3439
* @inheritDoc
3540
*/
36-
public constructor(options: { key?: string; limit?: number } = {}) {
41+
public constructor(options: Partial<LinkedErrorsOptions> = {}) {
3742
this._key = options.key || DEFAULT_KEY;
3843
this._limit = options.limit || DEFAULT_LIMIT;
3944
}
@@ -43,36 +48,31 @@ export class LinkedErrors implements Integration {
4348
*/
4449
public setupOnce(): void {
4550
addGlobalEventProcessor((event: Event, hint?: EventHint) => {
46-
const self = getCurrentHub().getIntegration(LinkedErrors);
47-
if (self) {
48-
const handler = self._handler && self._handler.bind(self);
49-
return typeof handler === 'function' ? handler(event, hint) : event;
50-
}
51-
return event;
51+
return getCurrentHub().getIntegration(LinkedErrors) ? _handler(this._key, this._limit, event, hint) : event;
5252
});
5353
}
54+
}
5455

55-
/**
56-
* @inheritDoc
57-
*/
58-
private _handler(event: Event, hint?: EventHint): Event | null {
59-
if (!event.exception || !event.exception.values || !hint || !isInstanceOf(hint.originalException, Error)) {
60-
return event;
61-
}
62-
const linkedErrors = this._walkErrorTree(hint.originalException as ExtendedError, this._key);
63-
event.exception.values = [...linkedErrors, ...event.exception.values];
56+
/**
57+
* @inheritDoc
58+
*/
59+
export function _handler(key: string, limit: number, event: Event, hint?: EventHint): Event | null {
60+
if (!event.exception || !event.exception.values || !hint || !isInstanceOf(hint.originalException, Error)) {
6461
return event;
6562
}
63+
const linkedErrors = _walkErrorTree(limit, hint.originalException as ExtendedError, key);
64+
event.exception.values = [...linkedErrors, ...event.exception.values];
65+
return event;
66+
}
6667

67-
/**
68-
* @inheritDoc
69-
*/
70-
private _walkErrorTree(error: ExtendedError, key: string, stack: Exception[] = []): Exception[] {
71-
if (!isInstanceOf(error[key], Error) || stack.length + 1 >= this._limit) {
72-
return stack;
73-
}
74-
const stacktrace = computeStackTrace(error[key]);
75-
const exception = exceptionFromStacktrace(stacktrace);
76-
return this._walkErrorTree(error[key], key, [exception, ...stack]);
68+
/**
69+
* JSDOC
70+
*/
71+
export function _walkErrorTree(limit: number, error: ExtendedError, key: string, stack: Exception[] = []): Exception[] {
72+
if (!isInstanceOf(error[key], Error) || stack.length + 1 >= limit) {
73+
return stack;
7774
}
75+
const stacktrace = computeStackTrace(error[key]);
76+
const exception = exceptionFromStacktrace(stacktrace);
77+
return _walkErrorTree(limit, error[key], key, [exception, ...stack]);
7878
}

Diff for: packages/browser/test/unit/integrations/linkederrors.test.ts

+10-43
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,29 @@
11
import { ExtendedError } from '@sentry/types';
2-
import { stub } from 'sinon';
32

43
import { BrowserBackend } from '../../../src/backend';
5-
import { LinkedErrors } from '../../../src/integrations/linkederrors';
6-
7-
let linkedErrors: any;
4+
import * as LinkedErrorsModule from '../../../src/integrations/linkederrors';
85

96
describe('LinkedErrors', () => {
10-
beforeEach(() => {
11-
linkedErrors = new LinkedErrors();
12-
});
13-
147
describe('handler', () => {
15-
it('should bail out if event doesnt contain exception', () => {
16-
const spy = stub(linkedErrors, '_walkErrorTree');
8+
it('should bail out if event does not contain exception', () => {
179
const event = {
1810
message: 'foo',
1911
};
20-
const result = linkedErrors._handler(event);
21-
expect(spy.called).toBe(false);
12+
const result = LinkedErrorsModule._handler('cause', 5, event);
2213
expect(result).toEqual(event);
2314
});
2415

2516
it('should bail out if event contains exception, but no hint', () => {
26-
const spy = stub(linkedErrors, '_walkErrorTree');
2717
const event = {
2818
exception: {
2919
values: [],
3020
},
3121
message: 'foo',
3222
};
33-
const result = linkedErrors._handler(event);
34-
expect(spy.called).toBe(false);
23+
const result = LinkedErrorsModule._handler('cause', 5, event);
3524
expect(result).toEqual(event);
3625
});
3726

38-
it('should call walkErrorTree if event contains exception and hint with originalException', () => {
39-
const spy = stub(linkedErrors, '_walkErrorTree').callsFake(() => []);
40-
const event = {
41-
exception: {
42-
values: [],
43-
},
44-
message: 'foo',
45-
};
46-
const hint = {
47-
originalException: new Error('originalException'),
48-
};
49-
linkedErrors._handler(event, hint);
50-
expect(spy.calledOnce).toBe(true);
51-
});
52-
5327
it('should recursively walk error to find linked exceptions and assign them to the event', async () => {
5428
const three: ExtendedError = new SyntaxError('three');
5529

@@ -62,7 +36,7 @@ describe('LinkedErrors', () => {
6236
const originalException = one;
6337
const backend = new BrowserBackend({});
6438
return backend.eventFromException(originalException).then(event => {
65-
const result = linkedErrors._handler(event, {
39+
const result = LinkedErrorsModule._handler('cause', 5, event, {
6640
originalException,
6741
});
6842

@@ -81,10 +55,6 @@ describe('LinkedErrors', () => {
8155
});
8256

8357
it('should allow to change walk key', async () => {
84-
linkedErrors = new LinkedErrors({
85-
key: 'reason',
86-
});
87-
8858
const three: ExtendedError = new SyntaxError('three');
8959

9060
const two: ExtendedError = new TypeError('two');
@@ -96,7 +66,7 @@ describe('LinkedErrors', () => {
9666
const originalException = one;
9767
const backend = new BrowserBackend({});
9868
return backend.eventFromException(originalException).then(event => {
99-
const result = linkedErrors._handler(event, {
69+
const result = LinkedErrorsModule._handler('reason', 5, event, {
10070
originalException,
10171
});
10272

@@ -114,20 +84,17 @@ describe('LinkedErrors', () => {
11484
});
11585

11686
it('should allow to change stack size limit', async () => {
117-
linkedErrors = new LinkedErrors({
118-
limit: 2,
119-
});
120-
12187
const one: ExtendedError = new Error('one');
12288
const two: ExtendedError = new TypeError('two');
12389
const three: ExtendedError = new SyntaxError('three');
12490
one.cause = two;
12591
two.cause = three;
12692

12793
const backend = new BrowserBackend({});
128-
return backend.eventFromException(one).then(event => {
129-
const result = linkedErrors._handler(event, {
130-
originalException: one,
94+
const originalException = one;
95+
return backend.eventFromException(originalException).then(event => {
96+
const result = LinkedErrorsModule._handler('cause', 2, event, {
97+
originalException,
13198
});
13299

133100
expect(result.exception.values.length).toBe(2);

0 commit comments

Comments
 (0)