From 292075f2056c005112e87aed445bd635f37c5497 Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Wed, 13 Apr 2022 17:32:27 -0700 Subject: [PATCH] ref(types): Stop using `Severity` enum (#4926) Note: This and https://github.com/getsentry/sentry-javascript/pull/4896 are together a (slightly updated) repeat of https://github.com/getsentry/sentry-javascript/pull/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 --- packages/browser/src/client.ts | 9 +++- packages/browser/src/eventbuilder.ts | 7 ++-- packages/browser/src/exports.ts | 4 +- .../browser/src/integrations/breadcrumbs.ts | 8 ++-- .../src/integrations/globalhandlers.ts | 6 +-- packages/core/src/baseclient.ts | 16 ++++++- packages/core/test/mocks/client.ts | 8 +++- packages/hub/src/hub.ts | 8 +++- packages/hub/src/scope.ts | 9 +++- packages/hub/test/scope.test.ts | 22 +++++----- packages/integrations/src/captureconsole.ts | 4 +- packages/minimal/src/index.ts | 7 +++- packages/minimal/test/lib/minimal.test.ts | 13 +++--- .../multiple_breadcrumbs/scenario.ts | 2 +- .../simple_breadcrumb/scenario.ts | 2 +- .../captureMessage/with_level/scenario.ts | 14 +++---- packages/node/src/client.ts | 9 +++- packages/node/src/eventbuilder.ts | 14 ++++++- packages/node/src/index.ts | 4 +- packages/node/src/integrations/console.ts | 4 +- .../src/integrations/onuncaughtexception.ts | 4 +- packages/serverless/src/awslambda.ts | 2 +- packages/tracing/src/index.bundle.ts | 4 +- packages/types/src/breadcrumb.ts | 5 ++- packages/types/src/client.ts | 17 ++++++-- packages/types/src/event.ts | 5 ++- packages/types/src/hub.ts | 9 +++- packages/types/src/index.ts | 4 +- packages/types/src/scope.ts | 12 ++++-- packages/types/src/severity.ts | 10 ++--- packages/utils/src/enums.ts | 2 - packages/utils/src/index.ts | 1 - packages/utils/src/severity.ts | 42 +++++++++++++------ packages/utils/test/severity.test.ts | 28 +++++-------- packages/vue/src/index.bundle.ts | 3 +- 35 files changed, 199 insertions(+), 119 deletions(-) delete mode 100644 packages/utils/src/enums.ts diff --git a/packages/browser/src/client.ts b/packages/browser/src/client.ts index 4ee24f0ceadf..5116df6104d1 100644 --- a/packages/browser/src/client.ts +++ b/packages/browser/src/client.ts @@ -1,5 +1,5 @@ import { BaseClient, getEnvelopeEndpointWithUrlEncodedAuth, initAPIDetails, Scope, SDK_VERSION } from '@sentry/core'; -import { Event, EventHint, Options, Severity, Transport, TransportOptions } from '@sentry/types'; +import { Event, EventHint, Options, Severity, SeverityLevel, Transport, TransportOptions } from '@sentry/types'; import { getGlobalObject, logger, stackParserFromOptions, supportsFetch } from '@sentry/utils'; import { eventFromException, eventFromMessage } from './eventbuilder'; @@ -89,7 +89,12 @@ export class BrowserClient extends BaseClient { /** * @inheritDoc */ - public eventFromMessage(message: string, level: Severity = Severity.Info, hint?: EventHint): PromiseLike { + public eventFromMessage( + message: string, + // eslint-disable-next-line deprecation/deprecation + level: Severity | SeverityLevel = 'info', + hint?: EventHint, + ): PromiseLike { return eventFromMessage( stackParserFromOptions(this._options), message, diff --git a/packages/browser/src/eventbuilder.ts b/packages/browser/src/eventbuilder.ts index 6ed5181eefa1..c3052c41dd48 100644 --- a/packages/browser/src/eventbuilder.ts +++ b/packages/browser/src/eventbuilder.ts @@ -1,4 +1,4 @@ -import { Event, EventHint, Exception, Severity, StackFrame, StackParser } from '@sentry/types'; +import { Event, EventHint, Exception, Severity, SeverityLevel, StackFrame, StackParser } from '@sentry/types'; import { addExceptionMechanism, addExceptionTypeValue, @@ -150,7 +150,7 @@ export function eventFromException( const syntheticException = (hint && hint.syntheticException) || undefined; const event = eventFromUnknownInput(stackParser, exception, syntheticException, attachStacktrace); addExceptionMechanism(event); // defaults to { type: 'generic', handled: true } - event.level = Severity.Error; + event.level = 'error'; if (hint && hint.event_id) { event.event_id = hint.event_id; } @@ -164,7 +164,8 @@ export function eventFromException( export function eventFromMessage( stackParser: StackParser, message: string, - level: Severity = Severity.Info, + // eslint-disable-next-line deprecation/deprecation + level: Severity | SeverityLevel = 'info', hint?: EventHint, attachStacktrace?: boolean, ): PromiseLike { diff --git a/packages/browser/src/exports.ts b/packages/browser/src/exports.ts index 704096fae520..1ee859e354d6 100644 --- a/packages/browser/src/exports.ts +++ b/packages/browser/src/exports.ts @@ -8,15 +8,15 @@ export { EventStatus, Exception, Response, + // eslint-disable-next-line deprecation/deprecation Severity, + SeverityLevel, StackFrame, Stacktrace, Thread, User, } from '@sentry/types'; -export { SeverityLevel } from '@sentry/utils'; - export { addGlobalEventProcessor, addBreadcrumb, diff --git a/packages/browser/src/integrations/breadcrumbs.ts b/packages/browser/src/integrations/breadcrumbs.ts index 15237f599b15..3cca50248458 100644 --- a/packages/browser/src/integrations/breadcrumbs.ts +++ b/packages/browser/src/integrations/breadcrumbs.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ /* eslint-disable max-lines */ import { getCurrentHub } from '@sentry/core'; -import { Event, Integration, Severity } from '@sentry/types'; +import { Event, Integration } from '@sentry/types'; import { addInstrumentationHandler, getEventDescription, @@ -9,7 +9,7 @@ import { htmlTreeAsString, parseUrl, safeJoin, - severityFromString, + severityLevelFromString, } from '@sentry/utils'; /** JSDoc */ @@ -157,7 +157,7 @@ function _consoleBreadcrumb(handlerData: { [key: string]: any }): void { arguments: handlerData.args, logger: 'console', }, - level: severityFromString(handlerData.level), + level: severityLevelFromString(handlerData.level), message: safeJoin(handlerData.args, ' '), }; @@ -230,7 +230,7 @@ function _fetchBreadcrumb(handlerData: { [key: string]: any }): void { { category: 'fetch', data: handlerData.fetchData, - level: Severity.Error, + level: 'error', type: 'http', }, { diff --git a/packages/browser/src/integrations/globalhandlers.ts b/packages/browser/src/integrations/globalhandlers.ts index 70966e4f7ec5..61e56a533629 100644 --- a/packages/browser/src/integrations/globalhandlers.ts +++ b/packages/browser/src/integrations/globalhandlers.ts @@ -1,6 +1,6 @@ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ import { getCurrentHub } from '@sentry/core'; -import { Event, EventHint, Hub, Integration, Primitive, Severity, StackParser } from '@sentry/types'; +import { Event, EventHint, Hub, Integration, Primitive, StackParser } from '@sentry/types'; import { addExceptionMechanism, addInstrumentationHandler, @@ -100,7 +100,7 @@ function _installGlobalOnErrorHandler(): void { column, ); - event.level = Severity.Error; + event.level = 'error'; addMechanismAndCapture(hub, error, event, 'onerror'); }, @@ -146,7 +146,7 @@ function _installGlobalOnUnhandledRejectionHandler(): void { ? _eventFromRejectionWithPrimitive(error) : eventFromUnknownInput(stackParser, error, undefined, attachStacktrace, true); - event.level = Severity.Error; + event.level = 'error'; addMechanismAndCapture(hub, error, event, 'onunhandledrejection'); return; diff --git a/packages/core/src/baseclient.ts b/packages/core/src/baseclient.ts index 75712f62479b..8486d5e03b9c 100644 --- a/packages/core/src/baseclient.ts +++ b/packages/core/src/baseclient.ts @@ -9,6 +9,7 @@ import { IntegrationClass, Options, Severity, + SeverityLevel, Transport, } from '@sentry/types'; import { @@ -131,7 +132,13 @@ export abstract class BaseClient implements Client { /** * @inheritDoc */ - public captureMessage(message: string, level?: Severity, hint?: EventHint, scope?: Scope): string | undefined { + public captureMessage( + message: string, + // eslint-disable-next-line deprecation/deprecation + level?: Severity | SeverityLevel, + hint?: EventHint, + scope?: Scope, + ): string | undefined { let eventId: string | undefined = hint && hint.event_id; const promisedEvent = isPrimitive(message) @@ -685,7 +692,12 @@ export abstract class BaseClient implements Client { /** * @inheritDoc */ - public abstract eventFromMessage(_message: string, _level?: Severity, _hint?: EventHint): PromiseLike; + public abstract eventFromMessage( + _message: string, + // eslint-disable-next-line deprecation/deprecation + _level?: Severity | SeverityLevel, + _hint?: EventHint, + ): PromiseLike; } /** diff --git a/packages/core/test/mocks/client.ts b/packages/core/test/mocks/client.ts index f399c6018122..36f053429e7f 100644 --- a/packages/core/test/mocks/client.ts +++ b/packages/core/test/mocks/client.ts @@ -1,5 +1,5 @@ import { Session } from '@sentry/hub'; -import { Event, Options, Severity, Transport } from '@sentry/types'; +import { Event, Options, Severity, SeverityLevel, Transport } from '@sentry/types'; import { resolvedSyncPromise } from '@sentry/utils'; import { BaseClient } from '../../src/baseclient'; @@ -38,7 +38,11 @@ export class TestClient extends BaseClient { }); } - public eventFromMessage(message: string, level: Severity = Severity.Info): PromiseLike { + public eventFromMessage( + message: string, + // eslint-disable-next-line deprecation/deprecation + level: Severity | SeverityLevel = 'info', + ): PromiseLike { return resolvedSyncPromise({ message, level }); } diff --git a/packages/hub/src/hub.ts b/packages/hub/src/hub.ts index 94c8e3ea91fe..74a810fafeea 100644 --- a/packages/hub/src/hub.ts +++ b/packages/hub/src/hub.ts @@ -14,6 +14,7 @@ import { Primitive, SessionContext, Severity, + SeverityLevel, Transaction, TransactionContext, User, @@ -213,7 +214,12 @@ export class Hub implements HubInterface { /** * @inheritDoc */ - public captureMessage(message: string, level?: Severity, hint?: EventHint): string { + public captureMessage( + message: string, + // eslint-disable-next-line deprecation/deprecation + level?: Severity | SeverityLevel, + hint?: EventHint, + ): string { const eventId = (this._lastEventId = hint && hint.event_id ? hint.event_id : uuid4()); let finalHint = hint; diff --git a/packages/hub/src/scope.ts b/packages/hub/src/scope.ts index 09dd244734ee..1733cf7f826f 100644 --- a/packages/hub/src/scope.ts +++ b/packages/hub/src/scope.ts @@ -14,6 +14,7 @@ import { Scope as ScopeInterface, ScopeContext, Severity, + SeverityLevel, Span, Transaction, User, @@ -61,7 +62,8 @@ export class Scope implements ScopeInterface { protected _fingerprint?: string[]; /** Severity */ - protected _level?: Severity; + // eslint-disable-next-line deprecation/deprecation + protected _level?: Severity | SeverityLevel; /** Transaction Name */ protected _transactionName?: string; @@ -208,7 +210,10 @@ export class Scope implements ScopeInterface { /** * @inheritDoc */ - public setLevel(level: Severity): this { + public setLevel( + // eslint-disable-next-line deprecation/deprecation + level: Severity | SeverityLevel, + ): this { this._level = level; this._notifyScopeListeners(); return this; diff --git a/packages/hub/test/scope.test.ts b/packages/hub/test/scope.test.ts index 62f2e112f7fe..7cca748fb6f8 100644 --- a/packages/hub/test/scope.test.ts +++ b/packages/hub/test/scope.test.ts @@ -1,4 +1,4 @@ -import { Event, EventHint, Severity } from '@sentry/types'; +import { Event, EventHint } from '@sentry/types'; import { getGlobalObject } from '@sentry/utils'; import { addGlobalEventProcessor, Scope } from '../src'; @@ -85,8 +85,8 @@ describe('Scope', () => { test('setLevel', () => { const scope = new Scope(); - scope.setLevel(Severity.Critical); - expect((scope as any)._level).toEqual(Severity.Critical); + scope.setLevel('critical'); + expect((scope as any)._level).toEqual('critical'); }); test('setTransactionName', () => { @@ -137,8 +137,8 @@ describe('Scope', () => { test('chaining', () => { const scope = new Scope(); - scope.setLevel(Severity.Critical).setUser({ id: '1' }); - expect((scope as any)._level).toEqual(Severity.Critical); + scope.setLevel('critical').setUser({ id: '1' }); + expect((scope as any)._level).toEqual('critical'); expect((scope as any)._user).toEqual({ id: '1' }); }); }); @@ -202,7 +202,7 @@ describe('Scope', () => { scope.setTag('a', 'b'); scope.setUser({ id: '1' }); scope.setFingerprint(['abcd']); - scope.setLevel(Severity.Warning); + scope.setLevel('warning'); scope.setTransactionName('/abc'); scope.addBreadcrumb({ message: 'test' }); scope.setContext('os', { id: '1' }); @@ -294,11 +294,11 @@ describe('Scope', () => { test('scope level should have priority over event level', () => { expect.assertions(1); const scope = new Scope(); - scope.setLevel(Severity.Warning); + scope.setLevel('warning'); const event: Event = {}; - event.level = Severity.Critical; + event.level = 'critical'; return scope.applyToEvent(event).then(processedEvent => { - expect(processedEvent!.level).toEqual(Severity.Warning); + expect(processedEvent!.level).toEqual('warning'); }); }); @@ -410,7 +410,7 @@ describe('Scope', () => { scope.setContext('foo', { id: '1' }); scope.setContext('bar', { id: '2' }); scope.setUser({ id: '1337' }); - scope.setLevel(Severity.Info); + scope.setLevel('info'); scope.setFingerprint(['foo']); scope.setRequestSession({ status: 'ok' }); }); @@ -458,7 +458,7 @@ describe('Scope', () => { localScope.setContext('bar', { id: '3' }); localScope.setContext('baz', { id: '4' }); localScope.setUser({ id: '42' }); - localScope.setLevel(Severity.Warning); + localScope.setLevel('warning'); localScope.setFingerprint(['bar']); (localScope as any)._requestSession = { status: 'ok' }; diff --git a/packages/integrations/src/captureconsole.ts b/packages/integrations/src/captureconsole.ts index 7224df50f286..7243520c0661 100644 --- a/packages/integrations/src/captureconsole.ts +++ b/packages/integrations/src/captureconsole.ts @@ -1,5 +1,5 @@ import { EventProcessor, Hub, Integration } from '@sentry/types'; -import { CONSOLE_LEVELS, fill, getGlobalObject, safeJoin, severityFromString } from '@sentry/utils'; +import { CONSOLE_LEVELS, fill, getGlobalObject, safeJoin, severityLevelFromString } from '@sentry/utils'; const global = getGlobalObject(); @@ -48,7 +48,7 @@ export class CaptureConsole implements Integration { if (hub.getIntegration(CaptureConsole)) { hub.withScope(scope => { - scope.setLevel(severityFromString(level)); + scope.setLevel(severityLevelFromString(level)); scope.setExtra('arguments', args); scope.addEventProcessor(event => { event.logger = 'console'; diff --git a/packages/minimal/src/index.ts b/packages/minimal/src/index.ts index 00a1cdd91d44..7cb0271786ab 100644 --- a/packages/minimal/src/index.ts +++ b/packages/minimal/src/index.ts @@ -8,6 +8,7 @@ import { Extras, Primitive, Severity, + SeverityLevel, Transaction, TransactionContext, User, @@ -52,7 +53,11 @@ export function captureException(exception: any, captureContext?: CaptureContext * @param Severity Define the level of the message. * @returns The generated eventId. */ -export function captureMessage(message: string, captureContext?: CaptureContext | Severity): string { +export function captureMessage( + message: string, + // eslint-disable-next-line deprecation/deprecation + captureContext?: CaptureContext | Severity | SeverityLevel, +): string { const syntheticException = new Error(message); // This is necessary to provide explicit scopes upgrade, without changing the original diff --git a/packages/minimal/test/lib/minimal.test.ts b/packages/minimal/test/lib/minimal.test.ts index 911c19ad1f68..48a117cd5c5d 100644 --- a/packages/minimal/test/lib/minimal.test.ts +++ b/packages/minimal/test/lib/minimal.test.ts @@ -1,5 +1,4 @@ import { getCurrentHub, getHubFromCarrier, Scope } from '@sentry/hub'; -import { Severity } from '@sentry/types'; import { _callOnClient, @@ -165,8 +164,8 @@ describe('Minimal', () => { const client: any = new TestClient({}); const scope = getCurrentHub().pushScope(); getCurrentHub().bindClient(client); - scope.setLevel(Severity.Warning); - expect(global.__SENTRY__.hub._stack[1].scope._level).toEqual(Severity.Warning); + scope.setLevel('warning'); + expect(global.__SENTRY__.hub._stack[1].scope._level).toEqual('warning'); }); }); @@ -245,16 +244,16 @@ describe('Minimal', () => { test('withScope', () => { withScope(scope => { - scope.setLevel(Severity.Warning); + scope.setLevel('warning'); scope.setFingerprint(['1']); withScope(scope2 => { - scope2.setLevel(Severity.Info); + scope2.setLevel('info'); scope2.setFingerprint(['2']); withScope(scope3 => { scope3.clear(); - expect(global.__SENTRY__.hub._stack[1].scope._level).toEqual(Severity.Warning); + expect(global.__SENTRY__.hub._stack[1].scope._level).toEqual('warning'); expect(global.__SENTRY__.hub._stack[1].scope._fingerprint).toEqual(['1']); - expect(global.__SENTRY__.hub._stack[2].scope._level).toEqual(Severity.Info); + expect(global.__SENTRY__.hub._stack[2].scope._level).toEqual('info'); expect(global.__SENTRY__.hub._stack[2].scope._fingerprint).toEqual(['2']); expect(global.__SENTRY__.hub._stack[3].scope._level).toBeUndefined(); }); diff --git a/packages/node-integration-tests/suites/public-api/addBreadcrumb/multiple_breadcrumbs/scenario.ts b/packages/node-integration-tests/suites/public-api/addBreadcrumb/multiple_breadcrumbs/scenario.ts index a94acf718b4b..993049500b08 100644 --- a/packages/node-integration-tests/suites/public-api/addBreadcrumb/multiple_breadcrumbs/scenario.ts +++ b/packages/node-integration-tests/suites/public-api/addBreadcrumb/multiple_breadcrumbs/scenario.ts @@ -8,7 +8,7 @@ Sentry.init({ Sentry.addBreadcrumb({ category: 'foo', message: 'bar', - level: Sentry.Severity.Critical, + level: 'critical', }); Sentry.addBreadcrumb({ diff --git a/packages/node-integration-tests/suites/public-api/addBreadcrumb/simple_breadcrumb/scenario.ts b/packages/node-integration-tests/suites/public-api/addBreadcrumb/simple_breadcrumb/scenario.ts index 108b79c26963..a31b33c68d54 100644 --- a/packages/node-integration-tests/suites/public-api/addBreadcrumb/simple_breadcrumb/scenario.ts +++ b/packages/node-integration-tests/suites/public-api/addBreadcrumb/simple_breadcrumb/scenario.ts @@ -8,7 +8,7 @@ Sentry.init({ Sentry.addBreadcrumb({ category: 'foo', message: 'bar', - level: Sentry.Severity.Critical, + level: 'critical', }); Sentry.captureMessage('test_simple'); diff --git a/packages/node-integration-tests/suites/public-api/captureMessage/with_level/scenario.ts b/packages/node-integration-tests/suites/public-api/captureMessage/with_level/scenario.ts index 61f8e2532ee6..32d46fa171fe 100644 --- a/packages/node-integration-tests/suites/public-api/captureMessage/with_level/scenario.ts +++ b/packages/node-integration-tests/suites/public-api/captureMessage/with_level/scenario.ts @@ -5,10 +5,10 @@ Sentry.init({ release: '1.0', }); -Sentry.captureMessage('debug_message', Sentry.Severity.Debug); -Sentry.captureMessage('info_message', Sentry.Severity.Info); -Sentry.captureMessage('warning_message', Sentry.Severity.Warning); -Sentry.captureMessage('error_message', Sentry.Severity.Error); -Sentry.captureMessage('fatal_message', Sentry.Severity.Fatal); -Sentry.captureMessage('critical_message', Sentry.Severity.Critical); -Sentry.captureMessage('log_message', Sentry.Severity.Log); +Sentry.captureMessage('debug_message', 'debug'); +Sentry.captureMessage('info_message', 'info'); +Sentry.captureMessage('warning_message', 'warning'); +Sentry.captureMessage('error_message', 'error'); +Sentry.captureMessage('fatal_message', 'fatal'); +Sentry.captureMessage('critical_message', 'critical'); +Sentry.captureMessage('log_message', 'log'); diff --git a/packages/node/src/client.ts b/packages/node/src/client.ts index 81f808f5ef56..6c4812b65980 100644 --- a/packages/node/src/client.ts +++ b/packages/node/src/client.ts @@ -1,6 +1,6 @@ import { BaseClient, getEnvelopeEndpointWithUrlEncodedAuth, initAPIDetails, Scope, SDK_VERSION } from '@sentry/core'; import { SessionFlusher } from '@sentry/hub'; -import { Event, EventHint, Severity, Transport, TransportOptions } from '@sentry/types'; +import { Event, EventHint, Severity, SeverityLevel, Transport, TransportOptions } from '@sentry/types'; import { logger, makeDsn, resolvedSyncPromise, stackParserFromOptions } from '@sentry/utils'; import { eventFromMessage, eventFromUnknownInput } from './eventbuilder'; @@ -118,7 +118,12 @@ export class NodeClient extends BaseClient { /** * @inheritDoc */ - public eventFromMessage(message: string, level: Severity = Severity.Info, hint?: EventHint): PromiseLike { + public eventFromMessage( + message: string, + // eslint-disable-next-line deprecation/deprecation + level: Severity | SeverityLevel = 'info', + hint?: EventHint, + ): PromiseLike { return resolvedSyncPromise( eventFromMessage(stackParserFromOptions(this._options), message, level, hint, this._options.attachStacktrace), ); diff --git a/packages/node/src/eventbuilder.ts b/packages/node/src/eventbuilder.ts index e0b6b16261f0..eead089793a0 100644 --- a/packages/node/src/eventbuilder.ts +++ b/packages/node/src/eventbuilder.ts @@ -1,5 +1,14 @@ import { getCurrentHub } from '@sentry/hub'; -import { Event, EventHint, Exception, Mechanism, Severity, StackFrame, StackParser } from '@sentry/types'; +import { + Event, + EventHint, + Exception, + Mechanism, + Severity, + SeverityLevel, + StackFrame, + StackParser, +} from '@sentry/types'; import { addExceptionMechanism, addExceptionTypeValue, @@ -90,7 +99,8 @@ export function eventFromUnknownInput(stackParser: StackParser, exception: unkno export function eventFromMessage( stackParser: StackParser, message: string, - level: Severity = Severity.Info, + // eslint-disable-next-line deprecation/deprecation + level: Severity | SeverityLevel = 'info', hint?: EventHint, attachStacktrace?: boolean, ): Event { diff --git a/packages/node/src/index.ts b/packages/node/src/index.ts index d6a924a04c2e..cb83bd07cdcf 100644 --- a/packages/node/src/index.ts +++ b/packages/node/src/index.ts @@ -8,15 +8,15 @@ export { EventStatus, Exception, Response, + // eslint-disable-next-line deprecation/deprecation Severity, + SeverityLevel, StackFrame, Stacktrace, Thread, User, } from '@sentry/types'; -export { SeverityLevel } from '@sentry/utils'; - export { addGlobalEventProcessor, addBreadcrumb, diff --git a/packages/node/src/integrations/console.ts b/packages/node/src/integrations/console.ts index a9542837fbcc..9b032c75cced 100644 --- a/packages/node/src/integrations/console.ts +++ b/packages/node/src/integrations/console.ts @@ -1,6 +1,6 @@ import { getCurrentHub } from '@sentry/core'; import { Integration } from '@sentry/types'; -import { fill, severityFromString } from '@sentry/utils'; +import { fill, severityLevelFromString } from '@sentry/utils'; import * as util from 'util'; /** Console module integration */ @@ -30,7 +30,7 @@ export class Console implements Integration { */ function createConsoleWrapper(level: string): (originalConsoleMethod: () => void) => void { return function consoleWrapper(originalConsoleMethod: () => void): () => void { - const sentryLevel = severityFromString(level); + const sentryLevel = severityLevelFromString(level); /* eslint-disable prefer-rest-params */ return function (this: typeof console): void { diff --git a/packages/node/src/integrations/onuncaughtexception.ts b/packages/node/src/integrations/onuncaughtexception.ts index 9c8d33913315..dfff26fa0675 100644 --- a/packages/node/src/integrations/onuncaughtexception.ts +++ b/packages/node/src/integrations/onuncaughtexception.ts @@ -1,5 +1,5 @@ import { getCurrentHub, Scope } from '@sentry/core'; -import { Integration, Severity } from '@sentry/types'; +import { Integration } from '@sentry/types'; import { logger } from '@sentry/utils'; import { NodeClient } from '../client'; @@ -78,7 +78,7 @@ export class OnUncaughtException implements Integration { if (hub.getIntegration(OnUncaughtException)) { hub.withScope((scope: Scope) => { - scope.setLevel(Severity.Fatal); + scope.setLevel('fatal'); hub.captureException(error, { originalException: error, data: { mechanism: { handled: false, type: 'onuncaughtexception' } }, diff --git a/packages/serverless/src/awslambda.ts b/packages/serverless/src/awslambda.ts index 3702ffc6a1fd..0b86f560a820 100644 --- a/packages/serverless/src/awslambda.ts +++ b/packages/serverless/src/awslambda.ts @@ -277,7 +277,7 @@ export function wrapHandler( timeoutWarningTimer = setTimeout(() => { withScope(scope => { scope.setTag('timeout', humanReadableTimeout); - captureMessage(`Possible function timeout: ${context.functionName}`, Sentry.Severity.Warning); + captureMessage(`Possible function timeout: ${context.functionName}`, 'warning'); }); }, timeoutWarningDelay); } diff --git a/packages/tracing/src/index.bundle.ts b/packages/tracing/src/index.bundle.ts index 3349dfbd06da..72ce4f3c19db 100644 --- a/packages/tracing/src/index.bundle.ts +++ b/packages/tracing/src/index.bundle.ts @@ -6,15 +6,15 @@ export { EventStatus, Exception, Response, + // eslint-disable-next-line deprecation/deprecation Severity, + SeverityLevel, StackFrame, Stacktrace, Thread, User, } from '@sentry/types'; -export { SeverityLevel } from '@sentry/utils'; - export { addGlobalEventProcessor, addBreadcrumb, diff --git a/packages/types/src/breadcrumb.ts b/packages/types/src/breadcrumb.ts index cab5ac68b3e1..34fe2dfcd16a 100644 --- a/packages/types/src/breadcrumb.ts +++ b/packages/types/src/breadcrumb.ts @@ -1,9 +1,10 @@ -import { Severity } from './severity'; +import { Severity, SeverityLevel } from './severity'; /** JSDoc */ export interface Breadcrumb { type?: string; - level?: Severity; + // eslint-disable-next-line deprecation/deprecation + level?: Severity | SeverityLevel; event_id?: string; category?: string; message?: string; diff --git a/packages/types/src/client.ts b/packages/types/src/client.ts index e11950487fa1..c3f2a9920258 100644 --- a/packages/types/src/client.ts +++ b/packages/types/src/client.ts @@ -4,7 +4,7 @@ import { Integration, IntegrationClass } from './integration'; import { Options } from './options'; import { Scope } from './scope'; import { Session } from './session'; -import { Severity } from './severity'; +import { Severity, SeverityLevel } from './severity'; import { Transport } from './transport'; /** @@ -36,7 +36,13 @@ export interface Client { * @param scope An optional scope containing event metadata. * @returns The event id */ - captureMessage(message: string, level?: Severity, hint?: EventHint, scope?: Scope): string | undefined; + captureMessage( + message: string, + // eslint-disable-next-line deprecation/deprecation + level?: Severity | SeverityLevel, + hint?: EventHint, + scope?: Scope, + ): string | undefined; /** * Captures a manually created event and sends it to Sentry. @@ -99,7 +105,12 @@ export interface Client { eventFromException(exception: any, hint?: EventHint): PromiseLike; /** Creates an {@link Event} from primitive inputs to `captureMessage`. */ - eventFromMessage(message: string, level?: Severity, hint?: EventHint): PromiseLike; + eventFromMessage( + message: string, + // eslint-disable-next-line deprecation/deprecation + level?: Severity | SeverityLevel, + hint?: EventHint, + ): PromiseLike; /** Submits the event to Sentry */ sendEvent(event: Event): void; diff --git a/packages/types/src/event.ts b/packages/types/src/event.ts index 6711322baab4..7cb3047ed4d5 100644 --- a/packages/types/src/event.ts +++ b/packages/types/src/event.ts @@ -7,7 +7,7 @@ import { Primitive } from './misc'; import { Request } from './request'; import { CaptureContext } from './scope'; import { SdkInfo } from './sdkinfo'; -import { Severity } from './severity'; +import { Severity, SeverityLevel } from './severity'; import { Span } from './span'; import { Measurements } from './transaction'; import { User } from './user'; @@ -18,7 +18,8 @@ export interface Event { message?: string; timestamp?: number; start_timestamp?: number; - level?: Severity; + // eslint-disable-next-line deprecation/deprecation + level?: Severity | SeverityLevel; platform?: string; logger?: string; server_name?: string; diff --git a/packages/types/src/hub.ts b/packages/types/src/hub.ts index 49452266fe9f..9a67968a4fc1 100644 --- a/packages/types/src/hub.ts +++ b/packages/types/src/hub.ts @@ -6,7 +6,7 @@ import { Integration, IntegrationClass } from './integration'; import { Primitive } from './misc'; import { Scope } from './scope'; import { Session, SessionContext } from './session'; -import { Severity } from './severity'; +import { Severity, SeverityLevel } from './severity'; import { CustomSamplingContext, Transaction, TransactionContext } from './transaction'; import { User } from './user'; @@ -87,7 +87,12 @@ export interface Hub { * @param hint May contain additional information about the original exception. * @returns The generated eventId. */ - captureMessage(message: string, level?: Severity, hint?: EventHint): string; + captureMessage( + message: string, + // eslint-disable-next-line deprecation/deprecation + level?: Severity | SeverityLevel, + hint?: EventHint, + ): string; /** * Captures a manually created event and sends it to Sentry. diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index 20856f52aa64..b104f2e85930 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -46,8 +46,8 @@ export { SessionFlusherLike, } from './session'; -export { Severity } from './severity'; -export { SeverityLevel, SeverityLevels } from './severity'; +// eslint-disable-next-line deprecation/deprecation +export { Severity, SeverityLevel } from './severity'; export { Span, SpanContext } from './span'; export { StackFrame } from './stackframe'; export { Stacktrace, StackParser, StackLineParser, StackLineParserFn } from './stacktrace'; diff --git a/packages/types/src/scope.ts b/packages/types/src/scope.ts index b46c47ce0759..c3ee56a2a763 100644 --- a/packages/types/src/scope.ts +++ b/packages/types/src/scope.ts @@ -4,7 +4,7 @@ import { EventProcessor } from './eventprocessor'; import { Extra, Extras } from './extra'; import { Primitive } from './misc'; import { RequestSession, Session } from './session'; -import { Severity } from './severity'; +import { Severity, SeverityLevel } from './severity'; import { Span } from './span'; import { Transaction } from './transaction'; import { User } from './user'; @@ -15,7 +15,8 @@ export type CaptureContext = Scope | Partial | ((scope: Scope) => /** JSDocs */ export interface ScopeContext { user: User; - level: Severity; + // eslint-disable-next-line deprecation/deprecation + level: Severity | SeverityLevel; extra: Extras; contexts: Contexts; tags: { [key: string]: Primitive }; @@ -79,9 +80,12 @@ export interface Scope { /** * Sets the level on the scope for future events. - * @param level string {@link Severity} + * @param level string {@link SeverityLevel} */ - setLevel(level: Severity): this; + setLevel( + // eslint-disable-next-line deprecation/deprecation + level: Severity | SeverityLevel, + ): this; /** * Sets the transaction name on the scope for future events. diff --git a/packages/types/src/severity.ts b/packages/types/src/severity.ts index 513c63c7dadb..ad96231cb013 100644 --- a/packages/types/src/severity.ts +++ b/packages/types/src/severity.ts @@ -1,5 +1,6 @@ /** - * TODO(v7): Remove this enum and replace with SeverityLevel + * @deprecated Please use a `SeverityLevel` string instead of the `Severity` enum. Acceptable values are 'fatal', + * 'critical', 'error', 'warning', 'log', 'info', and 'debug'. */ export enum Severity { /** JSDoc */ @@ -18,7 +19,6 @@ export enum Severity { Critical = 'critical', } -// TODO: in v7, these can disappear, because they now also exist in `@sentry/utils`. (Having them there rather than here -// is nice because then it enforces the idea that only types are exported from `@sentry/types`.) -export const SeverityLevels = ['fatal', 'error', 'warning', 'log', 'info', 'debug', 'critical'] as const; -export type SeverityLevel = typeof SeverityLevels[number]; +// Note: If this is ever changed, the `validSeverityLevels` array in `@sentry/utils` needs to be changed, also. (See +// note there for why we can't derive one from the other.) +export type SeverityLevel = 'fatal' | 'error' | 'warning' | 'log' | 'info' | 'debug' | 'critical'; diff --git a/packages/utils/src/enums.ts b/packages/utils/src/enums.ts deleted file mode 100644 index 998540a6677f..000000000000 --- a/packages/utils/src/enums.ts +++ /dev/null @@ -1,2 +0,0 @@ -export const SeverityLevels = ['fatal', 'error', 'warning', 'log', 'info', 'debug', 'critical'] as const; -export type SeverityLevel = typeof SeverityLevels[number]; diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index e4567790f7d3..4d33eeab55e3 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -1,7 +1,6 @@ export * from './async'; export * from './browser'; export * from './dsn'; -export * from './enums'; export * from './error'; export * from './global'; export * from './instrument'; diff --git a/packages/utils/src/severity.ts b/packages/utils/src/severity.ts index 034f7dcbeb99..ba2ad2822851 100644 --- a/packages/utils/src/severity.ts +++ b/packages/utils/src/severity.ts @@ -1,20 +1,36 @@ -import { Severity } from '@sentry/types'; +/* eslint-disable deprecation/deprecation */ +import { Severity, SeverityLevel } from '@sentry/types'; -import { SeverityLevel, SeverityLevels } from './enums'; +// Note: Ideally the `SeverityLevel` type would be derived from `validSeverityLevels`, but that would mean either +// +// a) moving `validSeverityLevels` to `@sentry/types`, +// b) moving the`SeverityLevel` type here, or +// c) importing `validSeverityLevels` from here into `@sentry/types`. +// +// Option A would make `@sentry/types` a runtime dependency of `@sentry/utils` (not good), and options B and C would +// create a circular dependency between `@sentry/types` and `@sentry/utils` (also not good). So a TODO accompanying the +// type, reminding anyone who changes it to change this list also, will have to do. + +export const validSeverityLevels = ['fatal', 'error', 'warning', 'log', 'info', 'debug', 'critical']; -function isSupportedSeverity(level: string): level is Severity { - return SeverityLevels.indexOf(level as SeverityLevel) !== -1; -} /** - * Converts a string-based level into a {@link Severity}. + * Converts a string-based level into a member of the deprecated {@link Severity} enum. * - * @param level string representation of Severity + * @deprecated `severityFromString` is deprecated. Please use `severityLevelFromString` instead. + * + * @param level String representation of Severity * @returns Severity */ -export function severityFromString(level: SeverityLevel | string): Severity { - if (level === 'warn') return Severity.Warning; - if (isSupportedSeverity(level)) { - return level; - } - return Severity.Log; +export function severityFromString(level: Severity | SeverityLevel | string): Severity { + return severityLevelFromString(level) as Severity; +} + +/** + * Converts a string-based level into a `SeverityLevel`, normalizing it along the way. + * + * @param level String representation of desired `SeverityLevel`. + * @returns The `SeverityLevel` corresponding to the given string, or 'log' if the string isn't a valid level. + */ +export function severityLevelFromString(level: SeverityLevel | string): SeverityLevel { + return (level === 'warn' ? 'warning' : validSeverityLevels.includes(level) ? level : 'log') as SeverityLevel; } diff --git a/packages/utils/test/severity.test.ts b/packages/utils/test/severity.test.ts index 7b41c92a2082..51f66e815288 100644 --- a/packages/utils/test/severity.test.ts +++ b/packages/utils/test/severity.test.ts @@ -1,23 +1,17 @@ -import { SeverityLevels } from '../src/enums'; -import { severityFromString } from '../src/severity'; +import { severityLevelFromString, validSeverityLevels } from '../src/severity'; -describe('severityFromString()', () => { - describe('normalize warn and warning', () => { - test('handles warn and warning', () => { - expect(severityFromString('warn')).toBe('warning'); - expect(severityFromString('warning')).toBe('warning'); - }); - test('handles warn and warning', () => { - expect(severityFromString('warn')).toBe('warning'); - expect(severityFromString('warning')).toBe('warning'); - }); +describe('severityLevelFromString()', () => { + test("converts 'warn' to 'warning'", () => { + expect(severityLevelFromString('warn')).toBe('warning'); }); - describe('default to log', () => { - expect(severityFromString('foo')).toBe('log'); + + test('defaults to log', () => { + expect(severityLevelFromString('foo')).toBe('log'); }); - describe('allows ', () => { - for (const level of SeverityLevels) { - expect(severityFromString(level)).toBe(level); + + test('acts as a pass-through for valid level strings', () => { + for (const level of validSeverityLevels) { + expect(severityLevelFromString(level)).toBe(level); } }); }); diff --git a/packages/vue/src/index.bundle.ts b/packages/vue/src/index.bundle.ts index 3b1c401a9af9..232ec67cd0c1 100644 --- a/packages/vue/src/index.bundle.ts +++ b/packages/vue/src/index.bundle.ts @@ -6,14 +6,13 @@ export { EventStatus, Exception, Response, + SeverityLevel, StackFrame, Stacktrace, Thread, User, } from '@sentry/types'; -export { SeverityLevel } from '@sentry/utils'; - export { BrowserClient, BrowserOptions,