Skip to content

Commit 2df499b

Browse files
AbhiPrasadlforst
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

+36
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

+1-1
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

+8-6
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

+1-2
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

+3-3
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

+9-6
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

+1-4
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

+1-5
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

+1-5
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

+1-5
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', () => {

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

+1-5
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 - Misc Tests', () => {
95
it('should parse PhantomJS 1.19 error', () => {

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { createStackParser } from '@sentry/utils';
22

33
import { exceptionFromError } from '../../../src/eventbuilder';
4-
import { defaultStackParsers, opera10StackParser, opera11StackParser } from '../../../src/stack-parsers';
4+
import { defaultStackParser, opera10StackLineParser, opera11StackLineParser } from '../../../src/stack-parsers';
55

6-
const operaParser = createStackParser(opera10StackParser, opera11StackParser);
7-
const chromiumParser = createStackParser(...defaultStackParsers);
6+
const operaParser = createStackParser(opera10StackLineParser, opera11StackLineParser);
7+
const chromiumParser = defaultStackParser;
88

99
describe('Tracekit - Opera Tests', () => {
1010
it('should parse Opera 10 error', () => {

packages/browser/test/unit/tracekit/react-native.test.ts

+1-5
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 - React Native Tests', () => {
95
it('should parse exceptions for react-native-v8', () => {

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

+1-5
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 - React Tests', () => {
95
it('should correctly parse Invariant Violation errors and use framesToPop to drop info message', () => {

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

+1-5
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 - Safari Tests', () => {
95
it('should parse Safari 6 error', () => {

packages/node/src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,25 @@ export {
4343
} from '@sentry/core';
4444

4545
export { NodeClient } from './client';
46+
export { makeNodeTransport } from './transports';
4647
export { defaultIntegrations, init, lastEventId, flush, close, getSentryRelease } from './sdk';
4748
export { deepReadDirSync } from './utils';
4849
export { SDK_NAME } from './version';
49-
export { nodeStackParser } from './stack-parser';
50+
export { defaultStackParser } from './stack-parser';
5051

5152
import { Integrations as CoreIntegrations } from '@sentry/core';
5253
import { getMainCarrier } from '@sentry/hub';
5354
import * as domain from 'domain';
5455

5556
import * as Handlers from './handlers';
5657
import * as NodeIntegrations from './integrations';
57-
import * as Transports from './transports';
5858

5959
const INTEGRATIONS = {
6060
...CoreIntegrations,
6161
...NodeIntegrations,
6262
};
6363

64-
export { INTEGRATIONS as Integrations, Transports, Handlers };
64+
export { INTEGRATIONS as Integrations, Handlers };
6565

6666
// We need to patch domain on the global __SENTRY__ object to make it work for node in cross-platform packages like
6767
// @sentry/hub. If we don't do this, browser bundlers will have troubles resolving `require('domain')`.

packages/node/src/sdk.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { getCurrentHub, getIntegrationsToSetup, initAndBind, Integrations as CoreIntegrations } from '@sentry/core';
22
import { getMainCarrier, setHubOnCarrier } from '@sentry/hub';
33
import { SessionStatus } from '@sentry/types';
4-
import { getGlobalObject, logger, stackParserFromOptions } from '@sentry/utils';
4+
import { getGlobalObject, logger, stackParserFromStackParserOptions } from '@sentry/utils';
55
import * as domain from 'domain';
66

77
import { NodeClient } from './client';
88
import { IS_DEBUG_BUILD } from './flags';
99
import { Console, ContextLines, Http, LinkedErrors, OnUncaughtException, OnUnhandledRejection } from './integrations';
10-
import { nodeStackParser } from './stack-parser';
10+
import { defaultStackParser } from './stack-parser';
1111
import { makeNodeTransport } from './transports';
1212
import { NodeClientOptions, NodeOptions } from './types';
1313

@@ -130,7 +130,7 @@ export function init(options: NodeOptions = {}): void {
130130
// TODO(v7): Refactor this to reduce the logic above
131131
const clientOptions: NodeClientOptions = {
132132
...options,
133-
stackParser: stackParserFromOptions(options.stackParser || [nodeStackParser]),
133+
stackParser: stackParserFromStackParserOptions(options.stackParser || defaultStackParser),
134134
integrations: getIntegrationsToSetup(options),
135135
transport: options.transport || makeNodeTransport,
136136
};

packages/node/src/stack-parser.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { StackLineParser, StackLineParserFn } from '@sentry/types';
2-
import { basename, dirname } from '@sentry/utils';
2+
import { basename, createStackParser, dirname } from '@sentry/utils';
33

44
/** Gets the module */
55
function getModule(filename: string | undefined): string | undefined {
@@ -114,4 +114,6 @@ const node: StackLineParserFn = (line: string) => {
114114
};
115115
};
116116

117-
export const nodeStackParser: StackLineParser = [90, node];
117+
export const nodeStackLineParser: StackLineParser = [90, node];
118+
119+
export const defaultStackParser = createStackParser(nodeStackLineParser);

packages/node/test/context-lines.test.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import { StackFrame } from '@sentry/types';
2-
import { createStackParser } from '@sentry/utils';
32
import * as fs from 'fs';
43

54
import { parseStackFrames } from '../src/eventbuilder';
65
import { ContextLines, resetFileContentCache } from '../src/integrations/contextlines';
7-
import { nodeStackParser } from '../src/stack-parser';
6+
import { defaultStackParser } from '../src/stack-parser';
87
import { getError } from './helper/error';
98

10-
const parser = createStackParser(nodeStackParser);
11-
129
describe('ContextLines', () => {
1310
let readFileSpy: jest.SpyInstance;
1411
let contextLines: ContextLines;
@@ -31,7 +28,7 @@ describe('ContextLines', () => {
3128
test('parseStack with same file', async () => {
3229
expect.assertions(1);
3330

34-
const frames = parseStackFrames(parser, new Error('test'));
31+
const frames = parseStackFrames(defaultStackParser, new Error('test'));
3532

3633
await addContext(Array.from(frames));
3734

@@ -61,12 +58,12 @@ describe('ContextLines', () => {
6158

6259
test('parseStack with adding different file', async () => {
6360
expect.assertions(1);
64-
const frames = parseStackFrames(parser, new Error('test'));
61+
const frames = parseStackFrames(defaultStackParser, new Error('test'));
6562

6663
await addContext(frames);
6764

6865
const numCalls = readFileSpy.mock.calls.length;
69-
const parsedFrames = parseStackFrames(parser, getError());
66+
const parsedFrames = parseStackFrames(defaultStackParser, getError());
7067
await addContext(parsedFrames);
7168

7269
const newErrorCalls = readFileSpy.mock.calls.length;
@@ -104,7 +101,7 @@ describe('ContextLines', () => {
104101
contextLines = new ContextLines({ frameContextLines: 0 });
105102

106103
expect.assertions(1);
107-
const frames = parseStackFrames(parser, new Error('test'));
104+
const frames = parseStackFrames(defaultStackParser, new Error('test'));
108105

109106
await addContext(frames);
110107
expect(readFileSpy).toHaveBeenCalledTimes(0);

0 commit comments

Comments
 (0)