Skip to content

Commit c472229

Browse files
lobsterkatieLms24
authored andcommitted
ref(types): Stop using Severity enum (#4926)
Note: This and #4896 are together a (slightly updated) repeat of #4497, to get it onto the main v7 branch. Our original v7 plan called for deprecating and then removing the `Severity` enum which lives in `@sentry/types`, because enums transpile to a two-way lookup function with a fairly hefty footprint (see this SO answer[1] from one of the maintainers of TS). We therefore added a new `SeverityLevel` type, which is the union of all of `Severity`'s underlying values, and directed people to use that instead. Though we subsequently decided not to remove `Severity` (at least in v7), we agreed that we should stop using it internally. This implements that change. Key Changes: - `Severity` and `severityFromString` have been redeprecated. - The original plan to have `SeverityLevel` live in `@sentry/utils` has been reverted, and it now lives only in `@sentry/types`. - The internal `SeverityLevels` array on which we were basing `SeverityLevel` has been removed. While we lose the elegance of the derived type, we gain the ability to truly only export types from `@sentry/types`. - Wherever we accept a `Severity` value, we now also accept a `SeverityLevel`. - All internal uses of `Severity` values have been replaced with the equivalent string constants. - A new `severityLevelFromString` function has been introduced, and is now used in place of `SeverityFromString`. - The tests for `severityFromString` have been cleaned up and replaced with equivalent tests for `SeverityLevelFromString`. [1] https://stackoverflow.com/a/28818850
1 parent c9ebbe5 commit c472229

File tree

35 files changed

+199
-119
lines changed

35 files changed

+199
-119
lines changed

packages/browser/src/client.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BaseClient, getEnvelopeEndpointWithUrlEncodedAuth, initAPIDetails, Scope, SDK_VERSION } from '@sentry/core';
2-
import { Event, EventHint, Options, Severity, Transport, TransportOptions } from '@sentry/types';
2+
import { Event, EventHint, Options, Severity, SeverityLevel, Transport, TransportOptions } from '@sentry/types';
33
import { getGlobalObject, logger, stackParserFromOptions, supportsFetch } from '@sentry/utils';
44

55
import { eventFromException, eventFromMessage } from './eventbuilder';
@@ -89,7 +89,12 @@ export class BrowserClient extends BaseClient<BrowserOptions> {
8989
/**
9090
* @inheritDoc
9191
*/
92-
public eventFromMessage(message: string, level: Severity = Severity.Info, hint?: EventHint): PromiseLike<Event> {
92+
public eventFromMessage(
93+
message: string,
94+
// eslint-disable-next-line deprecation/deprecation
95+
level: Severity | SeverityLevel = 'info',
96+
hint?: EventHint,
97+
): PromiseLike<Event> {
9398
return eventFromMessage(
9499
stackParserFromOptions(this._options),
95100
message,

packages/browser/src/eventbuilder.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Event, EventHint, Exception, Severity, StackFrame, StackParser } from '@sentry/types';
1+
import { Event, EventHint, Exception, Severity, SeverityLevel, StackFrame, StackParser } from '@sentry/types';
22
import {
33
addExceptionMechanism,
44
addExceptionTypeValue,
@@ -150,7 +150,7 @@ export function eventFromException(
150150
const syntheticException = (hint && hint.syntheticException) || undefined;
151151
const event = eventFromUnknownInput(stackParser, exception, syntheticException, attachStacktrace);
152152
addExceptionMechanism(event); // defaults to { type: 'generic', handled: true }
153-
event.level = Severity.Error;
153+
event.level = 'error';
154154
if (hint && hint.event_id) {
155155
event.event_id = hint.event_id;
156156
}
@@ -164,7 +164,8 @@ export function eventFromException(
164164
export function eventFromMessage(
165165
stackParser: StackParser,
166166
message: string,
167-
level: Severity = Severity.Info,
167+
// eslint-disable-next-line deprecation/deprecation
168+
level: Severity | SeverityLevel = 'info',
168169
hint?: EventHint,
169170
attachStacktrace?: boolean,
170171
): PromiseLike<Event> {

packages/browser/src/exports.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ export {
88
EventStatus,
99
Exception,
1010
Response,
11+
// eslint-disable-next-line deprecation/deprecation
1112
Severity,
13+
SeverityLevel,
1214
StackFrame,
1315
Stacktrace,
1416
Thread,
1517
User,
1618
} from '@sentry/types';
1719

18-
export { SeverityLevel } from '@sentry/utils';
19-
2020
export {
2121
addGlobalEventProcessor,
2222
addBreadcrumb,

packages/browser/src/integrations/breadcrumbs.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
22
/* eslint-disable max-lines */
33
import { getCurrentHub } from '@sentry/core';
4-
import { Event, Integration, Severity } from '@sentry/types';
4+
import { Event, Integration } from '@sentry/types';
55
import {
66
addInstrumentationHandler,
77
getEventDescription,
88
getGlobalObject,
99
htmlTreeAsString,
1010
parseUrl,
1111
safeJoin,
12-
severityFromString,
12+
severityLevelFromString,
1313
} from '@sentry/utils';
1414

1515
/** JSDoc */
@@ -157,7 +157,7 @@ function _consoleBreadcrumb(handlerData: { [key: string]: any }): void {
157157
arguments: handlerData.args,
158158
logger: 'console',
159159
},
160-
level: severityFromString(handlerData.level),
160+
level: severityLevelFromString(handlerData.level),
161161
message: safeJoin(handlerData.args, ' '),
162162
};
163163

@@ -230,7 +230,7 @@ function _fetchBreadcrumb(handlerData: { [key: string]: any }): void {
230230
{
231231
category: 'fetch',
232232
data: handlerData.fetchData,
233-
level: Severity.Error,
233+
level: 'error',
234234
type: 'http',
235235
},
236236
{

packages/browser/src/integrations/globalhandlers.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
22
import { getCurrentHub } from '@sentry/core';
3-
import { Event, EventHint, Hub, Integration, Primitive, Severity, StackParser } from '@sentry/types';
3+
import { Event, EventHint, Hub, Integration, Primitive, StackParser } from '@sentry/types';
44
import {
55
addExceptionMechanism,
66
addInstrumentationHandler,
@@ -100,7 +100,7 @@ function _installGlobalOnErrorHandler(): void {
100100
column,
101101
);
102102

103-
event.level = Severity.Error;
103+
event.level = 'error';
104104

105105
addMechanismAndCapture(hub, error, event, 'onerror');
106106
},
@@ -146,7 +146,7 @@ function _installGlobalOnUnhandledRejectionHandler(): void {
146146
? _eventFromRejectionWithPrimitive(error)
147147
: eventFromUnknownInput(stackParser, error, undefined, attachStacktrace, true);
148148

149-
event.level = Severity.Error;
149+
event.level = 'error';
150150

151151
addMechanismAndCapture(hub, error, event, 'onunhandledrejection');
152152
return;

packages/core/src/baseclient.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
IntegrationClass,
1010
Options,
1111
Severity,
12+
SeverityLevel,
1213
Transport,
1314
} from '@sentry/types';
1415
import {
@@ -131,7 +132,13 @@ export abstract class BaseClient<O extends Options> implements Client<O> {
131132
/**
132133
* @inheritDoc
133134
*/
134-
public captureMessage(message: string, level?: Severity, hint?: EventHint, scope?: Scope): string | undefined {
135+
public captureMessage(
136+
message: string,
137+
// eslint-disable-next-line deprecation/deprecation
138+
level?: Severity | SeverityLevel,
139+
hint?: EventHint,
140+
scope?: Scope,
141+
): string | undefined {
135142
let eventId: string | undefined = hint && hint.event_id;
136143

137144
const promisedEvent = isPrimitive(message)
@@ -685,7 +692,12 @@ export abstract class BaseClient<O extends Options> implements Client<O> {
685692
/**
686693
* @inheritDoc
687694
*/
688-
public abstract eventFromMessage(_message: string, _level?: Severity, _hint?: EventHint): PromiseLike<Event>;
695+
public abstract eventFromMessage(
696+
_message: string,
697+
// eslint-disable-next-line deprecation/deprecation
698+
_level?: Severity | SeverityLevel,
699+
_hint?: EventHint,
700+
): PromiseLike<Event>;
689701
}
690702

691703
/**

packages/core/test/mocks/client.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Session } from '@sentry/hub';
2-
import { Event, Options, Severity, Transport } from '@sentry/types';
2+
import { Event, Options, Severity, SeverityLevel, Transport } from '@sentry/types';
33
import { resolvedSyncPromise } from '@sentry/utils';
44

55
import { BaseClient } from '../../src/baseclient';
@@ -38,7 +38,11 @@ export class TestClient extends BaseClient<TestOptions> {
3838
});
3939
}
4040

41-
public eventFromMessage(message: string, level: Severity = Severity.Info): PromiseLike<Event> {
41+
public eventFromMessage(
42+
message: string,
43+
// eslint-disable-next-line deprecation/deprecation
44+
level: Severity | SeverityLevel = 'info',
45+
): PromiseLike<Event> {
4246
return resolvedSyncPromise({ message, level });
4347
}
4448

packages/hub/src/hub.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
Primitive,
1515
SessionContext,
1616
Severity,
17+
SeverityLevel,
1718
Transaction,
1819
TransactionContext,
1920
User,
@@ -213,7 +214,12 @@ export class Hub implements HubInterface {
213214
/**
214215
* @inheritDoc
215216
*/
216-
public captureMessage(message: string, level?: Severity, hint?: EventHint): string {
217+
public captureMessage(
218+
message: string,
219+
// eslint-disable-next-line deprecation/deprecation
220+
level?: Severity | SeverityLevel,
221+
hint?: EventHint,
222+
): string {
217223
const eventId = (this._lastEventId = hint && hint.event_id ? hint.event_id : uuid4());
218224
let finalHint = hint;
219225

packages/hub/src/scope.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
Scope as ScopeInterface,
1515
ScopeContext,
1616
Severity,
17+
SeverityLevel,
1718
Span,
1819
Transaction,
1920
User,
@@ -61,7 +62,8 @@ export class Scope implements ScopeInterface {
6162
protected _fingerprint?: string[];
6263

6364
/** Severity */
64-
protected _level?: Severity;
65+
// eslint-disable-next-line deprecation/deprecation
66+
protected _level?: Severity | SeverityLevel;
6567

6668
/** Transaction Name */
6769
protected _transactionName?: string;
@@ -208,7 +210,10 @@ export class Scope implements ScopeInterface {
208210
/**
209211
* @inheritDoc
210212
*/
211-
public setLevel(level: Severity): this {
213+
public setLevel(
214+
// eslint-disable-next-line deprecation/deprecation
215+
level: Severity | SeverityLevel,
216+
): this {
212217
this._level = level;
213218
this._notifyScopeListeners();
214219
return this;

packages/hub/test/scope.test.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Event, EventHint, Severity } from '@sentry/types';
1+
import { Event, EventHint } from '@sentry/types';
22
import { getGlobalObject } from '@sentry/utils';
33

44
import { addGlobalEventProcessor, Scope } from '../src';
@@ -85,8 +85,8 @@ describe('Scope', () => {
8585

8686
test('setLevel', () => {
8787
const scope = new Scope();
88-
scope.setLevel(Severity.Critical);
89-
expect((scope as any)._level).toEqual(Severity.Critical);
88+
scope.setLevel('critical');
89+
expect((scope as any)._level).toEqual('critical');
9090
});
9191

9292
test('setTransactionName', () => {
@@ -137,8 +137,8 @@ describe('Scope', () => {
137137

138138
test('chaining', () => {
139139
const scope = new Scope();
140-
scope.setLevel(Severity.Critical).setUser({ id: '1' });
141-
expect((scope as any)._level).toEqual(Severity.Critical);
140+
scope.setLevel('critical').setUser({ id: '1' });
141+
expect((scope as any)._level).toEqual('critical');
142142
expect((scope as any)._user).toEqual({ id: '1' });
143143
});
144144
});
@@ -202,7 +202,7 @@ describe('Scope', () => {
202202
scope.setTag('a', 'b');
203203
scope.setUser({ id: '1' });
204204
scope.setFingerprint(['abcd']);
205-
scope.setLevel(Severity.Warning);
205+
scope.setLevel('warning');
206206
scope.setTransactionName('/abc');
207207
scope.addBreadcrumb({ message: 'test' });
208208
scope.setContext('os', { id: '1' });
@@ -294,11 +294,11 @@ describe('Scope', () => {
294294
test('scope level should have priority over event level', () => {
295295
expect.assertions(1);
296296
const scope = new Scope();
297-
scope.setLevel(Severity.Warning);
297+
scope.setLevel('warning');
298298
const event: Event = {};
299-
event.level = Severity.Critical;
299+
event.level = 'critical';
300300
return scope.applyToEvent(event).then(processedEvent => {
301-
expect(processedEvent!.level).toEqual(Severity.Warning);
301+
expect(processedEvent!.level).toEqual('warning');
302302
});
303303
});
304304

@@ -410,7 +410,7 @@ describe('Scope', () => {
410410
scope.setContext('foo', { id: '1' });
411411
scope.setContext('bar', { id: '2' });
412412
scope.setUser({ id: '1337' });
413-
scope.setLevel(Severity.Info);
413+
scope.setLevel('info');
414414
scope.setFingerprint(['foo']);
415415
scope.setRequestSession({ status: 'ok' });
416416
});
@@ -458,7 +458,7 @@ describe('Scope', () => {
458458
localScope.setContext('bar', { id: '3' });
459459
localScope.setContext('baz', { id: '4' });
460460
localScope.setUser({ id: '42' });
461-
localScope.setLevel(Severity.Warning);
461+
localScope.setLevel('warning');
462462
localScope.setFingerprint(['bar']);
463463
(localScope as any)._requestSession = { status: 'ok' };
464464

packages/integrations/src/captureconsole.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EventProcessor, Hub, Integration } from '@sentry/types';
2-
import { CONSOLE_LEVELS, fill, getGlobalObject, safeJoin, severityFromString } from '@sentry/utils';
2+
import { CONSOLE_LEVELS, fill, getGlobalObject, safeJoin, severityLevelFromString } from '@sentry/utils';
33

44
const global = getGlobalObject<Window | NodeJS.Global>();
55

@@ -48,7 +48,7 @@ export class CaptureConsole implements Integration {
4848

4949
if (hub.getIntegration(CaptureConsole)) {
5050
hub.withScope(scope => {
51-
scope.setLevel(severityFromString(level));
51+
scope.setLevel(severityLevelFromString(level));
5252
scope.setExtra('arguments', args);
5353
scope.addEventProcessor(event => {
5454
event.logger = 'console';

packages/minimal/src/index.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Extras,
99
Primitive,
1010
Severity,
11+
SeverityLevel,
1112
Transaction,
1213
TransactionContext,
1314
User,
@@ -52,7 +53,11 @@ export function captureException(exception: any, captureContext?: CaptureContext
5253
* @param Severity Define the level of the message.
5354
* @returns The generated eventId.
5455
*/
55-
export function captureMessage(message: string, captureContext?: CaptureContext | Severity): string {
56+
export function captureMessage(
57+
message: string,
58+
// eslint-disable-next-line deprecation/deprecation
59+
captureContext?: CaptureContext | Severity | SeverityLevel,
60+
): string {
5661
const syntheticException = new Error(message);
5762

5863
// This is necessary to provide explicit scopes upgrade, without changing the original

packages/minimal/test/lib/minimal.test.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { getCurrentHub, getHubFromCarrier, Scope } from '@sentry/hub';
2-
import { Severity } from '@sentry/types';
32

43
import {
54
_callOnClient,
@@ -165,8 +164,8 @@ describe('Minimal', () => {
165164
const client: any = new TestClient({});
166165
const scope = getCurrentHub().pushScope();
167166
getCurrentHub().bindClient(client);
168-
scope.setLevel(Severity.Warning);
169-
expect(global.__SENTRY__.hub._stack[1].scope._level).toEqual(Severity.Warning);
167+
scope.setLevel('warning');
168+
expect(global.__SENTRY__.hub._stack[1].scope._level).toEqual('warning');
170169
});
171170
});
172171

@@ -245,16 +244,16 @@ describe('Minimal', () => {
245244

246245
test('withScope', () => {
247246
withScope(scope => {
248-
scope.setLevel(Severity.Warning);
247+
scope.setLevel('warning');
249248
scope.setFingerprint(['1']);
250249
withScope(scope2 => {
251-
scope2.setLevel(Severity.Info);
250+
scope2.setLevel('info');
252251
scope2.setFingerprint(['2']);
253252
withScope(scope3 => {
254253
scope3.clear();
255-
expect(global.__SENTRY__.hub._stack[1].scope._level).toEqual(Severity.Warning);
254+
expect(global.__SENTRY__.hub._stack[1].scope._level).toEqual('warning');
256255
expect(global.__SENTRY__.hub._stack[1].scope._fingerprint).toEqual(['1']);
257-
expect(global.__SENTRY__.hub._stack[2].scope._level).toEqual(Severity.Info);
256+
expect(global.__SENTRY__.hub._stack[2].scope._level).toEqual('info');
258257
expect(global.__SENTRY__.hub._stack[2].scope._fingerprint).toEqual(['2']);
259258
expect(global.__SENTRY__.hub._stack[3].scope._level).toBeUndefined();
260259
});

packages/node-integration-tests/suites/public-api/addBreadcrumb/multiple_breadcrumbs/scenario.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Sentry.init({
88
Sentry.addBreadcrumb({
99
category: 'foo',
1010
message: 'bar',
11-
level: Sentry.Severity.Critical,
11+
level: 'critical',
1212
});
1313

1414
Sentry.addBreadcrumb({

packages/node-integration-tests/suites/public-api/addBreadcrumb/simple_breadcrumb/scenario.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Sentry.init({
88
Sentry.addBreadcrumb({
99
category: 'foo',
1010
message: 'bar',
11-
level: Sentry.Severity.Critical,
11+
level: 'critical',
1212
});
1313

1414
Sentry.captureMessage('test_simple');

0 commit comments

Comments
 (0)