Skip to content

Commit d09ad7c

Browse files
committed
ref: Change Scope code
1 parent 9a11f50 commit d09ad7c

File tree

6 files changed

+35
-79
lines changed

6 files changed

+35
-79
lines changed

CHANGELOG.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ since we removed some methods from the public API and removed some classes from
3434
- **breaking** [core] ref: Extract all pluggable integrations into a separate `@sentry/integrations` package
3535
- **breaking** [core] ref: Move `extraErrorData` integration to `@sentry/integrations` package
3636
- [core] feat: Add `maxValueLength` option to adjust max string length for values, default is 250.
37-
- [hub] feat: Introduce `setExtra`, `setTags`, `clearBreadcrumbs` additionally some `set` on the Scope now accept no
38-
argument which makes it possible to unset the value.
37+
- [hub] feat: Introduce `setExtras`, `setTags`, `clearBreadcrumbs`.
3938
- **breaking** [all] feat: Move `Mechanism` to `Exception`
4039
- [browser/node] feat: Add `synthetic` to `Mechanism` in exception.
4140
- [browser/node] fix: Use `addExceptionTypeValue` in helpers
@@ -44,7 +43,6 @@ since we removed some methods from the public API and removed some classes from
4443
- **breaking** [all] build: Use `es6` target instead of esnext for ESM builds
4544
- [all] feat: Prefix all private methods with `_`
4645
- [all] build: Use terser instead of uglify
47-
>>>>>>> misc: Bundle size reduction changelog
4846

4947
## 4.6.4
5048

packages/browser/src/backend.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class BrowserBackend extends BaseBackend<BrowserOptions> {
9393
event = eventFromPlainObject(objectException, hint.syntheticException);
9494
addExceptionTypeValue(event, 'Custom Object', undefined, {
9595
handled: true,
96-
synthetic: true,
96+
synthetic: false, // TODO: Make true
9797
type: 'generic',
9898
});
9999
return SyncPromise.resolve(this._buildEvent(event, hint));
@@ -109,7 +109,7 @@ export class BrowserBackend extends BaseBackend<BrowserOptions> {
109109
return this.eventFromMessage(stringException, undefined, hint).then(messageEvent => {
110110
addExceptionTypeValue(messageEvent, `${stringException}`, undefined, {
111111
handled: true,
112-
synthetic: true,
112+
synthetic: false, // TODO: Make true
113113
type: 'generic',
114114
});
115115
return SyncPromise.resolve(this._buildEvent(messageEvent, hint));

packages/hub/src/scope.ts

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Breadcrumb, Event, EventHint, EventProcessor, Scope as ScopeInterface, Severity, User } from '@sentry/types';
2-
import { isPlainObject, isThenable } from '@sentry/utils/is';
2+
import { isThenable } from '@sentry/utils/is';
33
import { getGlobalObject } from '@sentry/utils/misc';
44
import { normalize } from '@sentry/utils/object';
55
import { SyncPromise } from '@sentry/utils/syncpromise';
@@ -99,23 +99,20 @@ export class Scope implements ScopeInterface {
9999
/**
100100
* @inheritdoc
101101
*/
102-
public setUser(user?: User): this {
103-
this._user = user ? normalize(user) : {};
102+
public setUser(user: User): this {
103+
this._user = normalize(user);
104104
this.notifyScopeListeners();
105105
return this;
106106
}
107107

108108
/**
109109
* @inheritdoc
110110
*/
111-
public setTags(tags?: { [key: string]: string }): this {
112-
this._tags =
113-
tags && isPlainObject(tags)
114-
? {
115-
...this._tags,
116-
...normalize(tags),
117-
}
118-
: {};
111+
public setTags(tags: { [key: string]: string }): this {
112+
this._tags = {
113+
...this._tags,
114+
...normalize(tags),
115+
};
119116
this.notifyScopeListeners();
120117
return this;
121118
}
@@ -132,14 +129,11 @@ export class Scope implements ScopeInterface {
132129
/**
133130
* @inheritdoc
134131
*/
135-
public setExtras(extra?: { [key: string]: any }): this {
136-
this._extra =
137-
extra && isPlainObject(extra)
138-
? {
139-
...this._extra,
140-
...normalize(extra),
141-
}
142-
: {};
132+
public setExtras(extra: { [key: string]: any }): this {
133+
this._extra = {
134+
...this._extra,
135+
...normalize(extra),
136+
};
143137
this.notifyScopeListeners();
144138
return this;
145139
}
@@ -156,8 +150,8 @@ export class Scope implements ScopeInterface {
156150
/**
157151
* @inheritdoc
158152
*/
159-
public setFingerprint(fingerprint?: string[]): this {
160-
this._fingerprint = fingerprint ? normalize(fingerprint) : undefined;
153+
public setFingerprint(fingerprint: string[]): this {
154+
this._fingerprint = normalize(fingerprint);
161155
this.notifyScopeListeners();
162156
return this;
163157
}
@@ -166,7 +160,7 @@ export class Scope implements ScopeInterface {
166160
* @inheritdoc
167161
*/
168162
public setLevel(level?: Severity): this {
169-
this._level = level ? normalize(level) : undefined;
163+
this._level = normalize(level);
170164
this.notifyScopeListeners();
171165
return this;
172166
}
@@ -181,9 +175,12 @@ export class Scope implements ScopeInterface {
181175
_scopeListeners: [],
182176
});
183177
if (scope) {
184-
newScope._extra = { ...scope._extra };
185-
newScope._tags = { ...scope._tags };
186178
newScope._breadcrumbs = [...scope._breadcrumbs];
179+
newScope._tags = { ...scope._tags };
180+
newScope._extra = { ...scope._extra };
181+
newScope._user = scope._user;
182+
newScope._level = scope._level;
183+
newScope._fingerprint = scope._fingerprint;
187184
newScope._eventProcessors = [...scope._eventProcessors];
188185
}
189186
return newScope;

packages/hub/test/scope.test.ts

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@ describe('Scope', () => {
1313
scope.setFingerprint(['abcd']);
1414
expect((scope as any)._fingerprint).toEqual(['abcd']);
1515
});
16-
17-
test('unset', () => {
18-
const scope = new Scope();
19-
scope.setFingerprint(['abcd']);
20-
scope.setFingerprint();
21-
expect((scope as any)._fingerprint).toEqual(undefined);
22-
});
2316
});
2417

2518
describe('extra', () => {
@@ -38,8 +31,8 @@ describe('Scope', () => {
3831
test('set undefined', () => {
3932
const scope = new Scope();
4033
scope.setExtra('a', 1);
41-
scope.setExtras();
42-
expect((scope as any)._extra).toEqual({});
34+
scope.setExtras({ a: undefined });
35+
expect((scope as any)._extra).toEqual({ a: '[undefined]' });
4336
});
4437
});
4538

@@ -55,13 +48,6 @@ describe('Scope', () => {
5548
scope.setTags({ a: 'b' });
5649
expect((scope as any)._tags).toEqual({ a: 'b' });
5750
});
58-
59-
test('set undefined', () => {
60-
const scope = new Scope();
61-
scope.setTags({ a: 'b' });
62-
scope.setTags();
63-
expect((scope as any)._tags).toEqual({});
64-
});
6551
});
6652

6753
describe('user', () => {
@@ -70,13 +56,6 @@ describe('Scope', () => {
7056
scope.setUser({ id: '1' });
7157
expect((scope as any)._user).toEqual({ id: '1' });
7258
});
73-
74-
test('unset', () => {
75-
const scope = new Scope();
76-
scope.setUser({ id: '1' });
77-
scope.setUser();
78-
expect((scope as any)._user).toEqual({});
79-
});
8059
});
8160

8261
describe('level', () => {
@@ -85,13 +64,6 @@ describe('Scope', () => {
8564
scope.addBreadcrumb({ message: 'test' }, 100);
8665
expect((scope as any)._breadcrumbs).toEqual([{ message: 'test' }]);
8766
});
88-
89-
test('clear', () => {
90-
const scope = new Scope();
91-
scope.addBreadcrumb({ message: 'test' }, 100);
92-
scope.clearBreadcrumbs();
93-
expect((scope as any)._breadcrumbs).toEqual([]);
94-
});
9567
});
9668

9769
describe('level', () => {
@@ -100,12 +72,6 @@ describe('Scope', () => {
10072
scope.setLevel(Severity.Critical);
10173
expect((scope as any)._level).toEqual(Severity.Critical);
10274
});
103-
test('unset', () => {
104-
const scope = new Scope();
105-
scope.setLevel(Severity.Critical);
106-
scope.setLevel();
107-
expect((scope as any)._level).toEqual(undefined);
108-
});
10975
});
11076

11177
test('chaining', () => {

packages/node/src/backend.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export class NodeBackend extends BaseBackend<NodeOptions> {
104104
// We use synthesized Error here so we can extract a (rough) stack trace.
105105
ex = (hint && hint.syntheticException) || new Error(exception as string);
106106
}
107-
mechanism.synthetic = true;
107+
mechanism.synthetic = false; // TODO: Make true
108108
}
109109

110110
return new SyncPromise<Event>(resolve =>

packages/types/src/scope.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,16 @@ export interface Scope {
1313

1414
/**
1515
* Updates user context information for future events.
16-
* If passed any falsy value, value will be unset.
1716
*
1817
* @param user User context object to be set in the current context.
1918
*/
20-
setUser(user?: User): this;
19+
setUser(user: User): this;
2120

2221
/**
23-
* Set an object that will be sent as tags data with the event.
24-
* If passed any falsy value, value will be unset.
22+
* Set an object that will be merged sent as tags data with the event.
2523
* @param tags Tags context object to merge into current context.
2624
*/
27-
setTags(tags?: { [key: string]: string }): this;
25+
setTags(tags: { [key: string]: string }): this;
2826

2927
/**
3028
* Set key:value that will be sent as tags data with the event.
@@ -34,11 +32,10 @@ export interface Scope {
3432
setTag(key: string, value: string): this;
3533

3634
/**
37-
* Set an object that will be sent as extra data with the event.
38-
* If passed any falsy value, value will be unset.
39-
* @param extra context object to merge into current context.
35+
* Set an object that will be merged sent as extra data with the event.
36+
* @param extras Extras object to merge into current context.
4037
*/
41-
setExtras(extra?: { [key: string]: any }): this;
38+
setExtras(extras: { [key: string]: any }): this;
4239

4340
/**
4441
* Set key:value that will be sent as extra data with the event.
@@ -49,17 +46,15 @@ export interface Scope {
4946

5047
/**
5148
* Sets the fingerprint on the scope to send with the events.
52-
* If passed any falsy value, value will be unset.
5349
* @param fingerprint string[] to group events in Sentry.
5450
*/
55-
setFingerprint(fingerprint?: string[]): this;
51+
setFingerprint(fingerprint: string[]): this;
5652

5753
/**
5854
* Sets the level on the scope for future events.
59-
* If passed any falsy value, value will be unset.
6055
* @param level string {@link Severity}
6156
*/
62-
setLevel(level?: Severity): this;
57+
setLevel(level: Severity): this;
6358

6459
/** Clears the current scope and resets its properties. */
6560
clear(): this;

0 commit comments

Comments
 (0)