Skip to content

Commit 2df499b

Browse files
AbhiPrasadLuca Forstner
andauthored
ref: Make it easier to use stackParser (#5015)
Co-authored-by: Luca Forstner <[email protected]>
1 parent 16356cb commit 2df499b

25 files changed

+102
-102
lines changed

MIGRATION.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,42 @@ import {
3232
} from '@sentry/minimal';
3333
```
3434

35+
## Explicit Client Options
36+
37+
In v7, we've updated the `Client` to have options seperate from the options passed into `Sentry.init`. This means that constructing a client now requires 3 options: `integrations`, `transport` and `stackParser`. These can be customized as you see fit.
38+
39+
```ts
40+
import { BrowserClient, defaultStackParser, defaultIntegrations, makeFetchTransport } from '@sentry/browser';
41+
42+
// New in v7:
43+
const client = new BrowserClient({
44+
transport: makeFetchTransport,
45+
stackParser: defaultStackParser,
46+
integrations: [...defaultIntegrations],
47+
});
48+
49+
// Before:
50+
const client = new BrowserClient();
51+
```
52+
53+
Since you now explicitly pass in the dependencies of the client, you can also tree-shake out dependencies that you do not use this way. For example, you can tree-shake out the SDK's default integrations and only use the ones that you want like so:
54+
55+
```ts
56+
import { BrowserClient, defaultStackParser, Integrations, makeFetchTransport } from '@sentry/browser';
57+
58+
// New in v7:
59+
const client = new BrowserClient({
60+
transport: makeFetchTransport,
61+
stackParser: defaultStackParser,
62+
integrations: [
63+
new Integrations.Breadcrumbs(),
64+
new Integrations.GlobalHandlers(),
65+
new Integrations.LinkedErrors(),
66+
new Integrations.Dedupe(),
67+
],
68+
});
69+
```
70+
3571
## Removal Of Old Platform Integrations From `@sentry/integrations` Package
3672

3773
The following classes will be removed from the `@sentry/integrations` package and can no longer be used:

packages/browser/src/eventbuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
} from '@sentry/utils';
1515

1616
/**
17-
* This function creates an exception from an TraceKitStackTrace
17+
* This function creates an exception from a JavaScript Error
1818
*/
1919
export function exceptionFromError(stackParser: StackParser, ex: Error): Exception {
2020
// Get the frames first since Opera can lose the stack if we touch anything else first

packages/browser/src/exports.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ export {
4444
} from '@sentry/core';
4545

4646
export { BrowserClient } from './client';
47+
export { makeFetchTransport, makeXHRTransport } from './transports';
4748
export {
48-
defaultStackParsers,
49-
chromeStackParser,
50-
geckoStackParser,
51-
opera10StackParser,
52-
opera11StackParser,
53-
winjsStackParser,
49+
defaultStackParser,
50+
defaultStackLineParsers,
51+
chromeStackLineParser,
52+
geckoStackLineParser,
53+
opera10StackLineParser,
54+
opera11StackLineParser,
55+
winjsStackLineParser,
5456
} from './stack-parsers';
5557
export { defaultIntegrations, forceLoad, init, lastEventId, onLoad, showReportDialog, flush, close, wrap } from './sdk';
5658
export { SDK_NAME } from './version';

packages/browser/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { Integrations as CoreIntegrations } from '@sentry/core';
44
import { getGlobalObject } from '@sentry/utils';
55

66
import * as BrowserIntegrations from './integrations';
7-
import * as Transports from './transports';
87

98
let windowIntegrations = {};
109

@@ -20,4 +19,4 @@ const INTEGRATIONS = {
2019
...BrowserIntegrations,
2120
};
2221

23-
export { INTEGRATIONS as Integrations, Transports };
22+
export { INTEGRATIONS as Integrations };

packages/browser/src/sdk.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import {
1111
getGlobalObject,
1212
logger,
1313
resolvedSyncPromise,
14-
stackParserFromOptions,
14+
stackParserFromStackParserOptions,
1515
supportsFetch,
1616
} from '@sentry/utils';
1717

1818
import { BrowserClient, BrowserClientOptions, BrowserOptions } from './client';
1919
import { IS_DEBUG_BUILD } from './flags';
2020
import { ReportDialogOptions, wrap as internalWrap } from './helpers';
2121
import { Breadcrumbs, Dedupe, GlobalHandlers, LinkedErrors, TryCatch, UserAgent } from './integrations';
22-
import { defaultStackParsers } from './stack-parsers';
22+
import { defaultStackParser } from './stack-parsers';
2323
import { makeFetchTransport, makeXHRTransport } from './transports';
2424

2525
export const defaultIntegrations = [
@@ -110,7 +110,7 @@ export function init(options: BrowserOptions = {}): void {
110110

111111
const clientOptions: BrowserClientOptions = {
112112
...options,
113-
stackParser: stackParserFromOptions(options.stackParser || defaultStackParsers),
113+
stackParser: stackParserFromStackParserOptions(options.stackParser || defaultStackParser),
114114
integrations: getIntegrationsToSetup(options),
115115
transport: options.transport || (supportsFetch() ? makeFetchTransport : makeXHRTransport),
116116
};

packages/browser/src/stack-parsers.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { StackFrame, StackLineParser, StackLineParserFn } from '@sentry/types';
2+
import { createStackParser } from '@sentry/utils';
23

34
// global reference to slice
45
const UNKNOWN_FUNCTION = '?';
@@ -60,7 +61,7 @@ const chrome: StackLineParserFn = line => {
6061
return;
6162
};
6263

63-
export const chromeStackParser: StackLineParser = [CHROME_PRIORITY, chrome];
64+
export const chromeStackLineParser: StackLineParser = [CHROME_PRIORITY, chrome];
6465

6566
// gecko regex: `(?:bundle|\d+\.js)`: `bundle` is for react native, `\d+\.js` also but specifically for ram bundles because it
6667
// generates filenames without a prefix like `file://` the filenames in the stacktrace are just 42.js
@@ -96,7 +97,7 @@ const gecko: StackLineParserFn = line => {
9697
return;
9798
};
9899

99-
export const geckoStackParser: StackLineParser = [GECKO_PRIORITY, gecko];
100+
export const geckoStackLineParser: StackLineParser = [GECKO_PRIORITY, gecko];
100101

101102
const winjsRegex =
102103
/^\s*at (?:((?:\[object object\])?.+) )?\(?((?:file|ms-appx|https?|webpack|blob):.*?):(\d+)(?::(\d+))?\)?\s*$/i;
@@ -109,7 +110,7 @@ const winjs: StackLineParserFn = line => {
109110
: undefined;
110111
};
111112

112-
export const winjsStackParser: StackLineParser = [WINJS_PRIORITY, winjs];
113+
export const winjsStackLineParser: StackLineParser = [WINJS_PRIORITY, winjs];
113114

114115
const opera10Regex = / line (\d+).*script (?:in )?(\S+)(?:: in function (\S+))?$/i;
115116

@@ -118,7 +119,7 @@ const opera10: StackLineParserFn = line => {
118119
return parts ? createFrame(parts[2], parts[3] || UNKNOWN_FUNCTION, +parts[1]) : undefined;
119120
};
120121

121-
export const opera10StackParser: StackLineParser = [OPERA10_PRIORITY, opera10];
122+
export const opera10StackLineParser: StackLineParser = [OPERA10_PRIORITY, opera10];
122123

123124
const opera11Regex =
124125
/ line (\d+), column (\d+)\s*(?:in (?:<anonymous function: ([^>]+)>|([^)]+))\(.*\))? in (.*):\s*$/i;
@@ -128,9 +129,11 @@ const opera11: StackLineParserFn = line => {
128129
return parts ? createFrame(parts[5], parts[3] || parts[4] || UNKNOWN_FUNCTION, +parts[1], +parts[2]) : undefined;
129130
};
130131

131-
export const opera11StackParser: StackLineParser = [OPERA11_PRIORITY, opera11];
132+
export const opera11StackLineParser: StackLineParser = [OPERA11_PRIORITY, opera11];
132133

133-
export const defaultStackParsers = [chromeStackParser, geckoStackParser, winjsStackParser];
134+
export const defaultStackLineParsers = [chromeStackLineParser, geckoStackLineParser, winjsStackLineParser];
135+
136+
export const defaultStackParser = createStackParser(...defaultStackLineParsers);
134137

135138
/**
136139
* Safari web extensions, starting version unknown, can produce "frames-only" stacktraces.

packages/browser/test/unit/integrations/linkederrors.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import { Event as SentryEvent, Exception, ExtendedError } from '@sentry/types';
2-
import { createStackParser } from '@sentry/utils';
32

43
import { BrowserClient } from '../../../src/client';
54
import * as LinkedErrorsModule from '../../../src/integrations/linkederrors';
6-
import { defaultStackParsers } from '../../../src/stack-parsers';
5+
import { defaultStackParser as parser } from '../../../src/stack-parsers';
76
import { getDefaultBrowserClientOptions } from '../helper/browser-client-options';
87

9-
const parser = createStackParser(...defaultStackParsers);
10-
118
type EventWithException = SentryEvent & {
129
exception: {
1310
values: Exception[];

packages/browser/test/unit/tracekit/chromium.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import { createStackParser } from '@sentry/utils';
2-
31
import { exceptionFromError } from '../../../src/eventbuilder';
4-
import { defaultStackParsers } from '../../../src/stack-parsers';
5-
6-
const parser = createStackParser(...defaultStackParsers);
2+
import { defaultStackParser as parser } from '../../../src/stack-parsers';
73

84
describe('Tracekit - Chrome Tests', () => {
95
it('should parse Chrome error with no location', () => {

packages/browser/test/unit/tracekit/firefox.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import { createStackParser } from '@sentry/utils';
2-
31
import { exceptionFromError } from '../../../src/eventbuilder';
4-
import { defaultStackParsers } from '../../../src/stack-parsers';
5-
6-
const parser = createStackParser(...defaultStackParsers);
2+
import { defaultStackParser as parser } from '../../../src/stack-parsers';
73

84
describe('Tracekit - Firefox Tests', () => {
95
it('should parse Firefox 3 error', () => {

packages/browser/test/unit/tracekit/ie.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import { createStackParser } from '@sentry/utils';
2-
31
import { exceptionFromError } from '../../../src/eventbuilder';
4-
import { defaultStackParsers } from '../../../src/stack-parsers';
5-
6-
const parser = createStackParser(...defaultStackParsers);
2+
import { defaultStackParser as parser } from '../../../src/stack-parsers';
73

84
describe('Tracekit - IE Tests', () => {
95
it('should parse IE 10 error', () => {

0 commit comments

Comments
 (0)