Skip to content

Commit 922562f

Browse files
authored
Revert "ref(utils): Use type predicates in is utility functions" (#4149)
This reverts commit b99ee25.
1 parent bafe7bb commit 922562f

File tree

13 files changed

+36
-40
lines changed

13 files changed

+36
-40
lines changed

packages/browser/src/eventbuilder.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@ export function eventFromUnknownInput(
6565
): Event {
6666
let event: Event;
6767

68-
if (isErrorEvent(exception)) {
68+
if (isErrorEvent(exception as ErrorEvent) && (exception as ErrorEvent).error) {
6969
// If it is an ErrorEvent with `error` property, extract it to get actual Error
70+
const errorEvent = exception as ErrorEvent;
7071
// eslint-disable-next-line no-param-reassign
71-
exception = exception.error;
72-
event = eventFromStacktrace(computeStackTrace(exception));
72+
exception = errorEvent.error;
73+
event = eventFromStacktrace(computeStackTrace(exception as Error));
7374
return event;
7475
}
75-
if (isDOMError(exception) || isDOMException(exception)) {
76+
if (isDOMError(exception as DOMError) || isDOMException(exception as DOMException)) {
7677
// If it is a DOMError or DOMException (which are legacy APIs, but still supported in some browsers)
7778
// then we just extract the name, code, and message, as they don't provide anything else
7879
// https://developer.mozilla.org/en-US/docs/Web/API/DOMError
@@ -89,9 +90,9 @@ export function eventFromUnknownInput(
8990

9091
return event;
9192
}
92-
if (isError(exception)) {
93+
if (isError(exception as Error)) {
9394
// we have a real Error object, do nothing
94-
event = eventFromStacktrace(computeStackTrace(exception));
95+
event = eventFromStacktrace(computeStackTrace(exception as Error));
9596
return event;
9697
}
9798
if (isPlainObject(exception) || isEvent(exception)) {

packages/integrations/src/ember.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class Ember implements Integration {
5858
getCurrentHub().withScope(scope => {
5959
if (isInstanceOf(reason, Error)) {
6060
scope.setExtra('context', 'Unhandled Promise error detected');
61-
getCurrentHub().captureException(reason, { originalException: reason });
61+
getCurrentHub().captureException(reason, { originalException: reason as Error });
6262
} else {
6363
scope.setExtra('reason', reason);
6464
getCurrentHub().captureMessage('Unhandled Promise error detected');

packages/integrations/src/extraerrordata.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export class ExtraErrorData implements Integration {
9797
continue;
9898
}
9999
const value = error[key];
100-
extraErrorInfo[key] = isError(value) ? value.toString() : value;
100+
extraErrorInfo[key] = isError(value) ? (value as Error).toString() : value;
101101
}
102102

103103
// Check if someone attached `toJSON` method to grab even more properties (eg. axios is doing that)
@@ -106,7 +106,7 @@ export class ExtraErrorData implements Integration {
106106

107107
for (const key of Object.keys(serializedError)) {
108108
const value = serializedError[key];
109-
extraErrorInfo[key] = isError(value) ? value.toString() : value;
109+
extraErrorInfo[key] = isError(value) ? (value as Error).toString() : value;
110110
}
111111
}
112112

packages/nextjs/src/utils/instrumentServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ function makeWrappedReqHandler(origReqHandler: ReqHandler): WrappedReqHandler {
227227
// If there is a trace header set, extract the data from it (parentSpanId, traceId, and sampling decision)
228228
let traceparentData;
229229
if (req.headers && isString(req.headers['sentry-trace'])) {
230-
traceparentData = extractTraceparentData(req.headers['sentry-trace']);
230+
traceparentData = extractTraceparentData(req.headers['sentry-trace'] as string);
231231
logger.log(`[Tracing] Continuing trace ${traceparentData?.traceId}.`);
232232
}
233233

packages/nextjs/src/utils/withSentry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const withSentry = (origHandler: NextApiHandler): WrappedNextApiHandler =
3939
// If there is a trace header set, extract the data from it (parentSpanId, traceId, and sampling decision)
4040
let traceparentData;
4141
if (req.headers && isString(req.headers['sentry-trace'])) {
42-
traceparentData = extractTraceparentData(req.headers['sentry-trace']);
42+
traceparentData = extractTraceparentData(req.headers['sentry-trace'] as string);
4343
logger.log(`[Tracing] Continuing trace ${traceparentData?.traceId}.`);
4444
}
4545

packages/node/src/backend.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class NodeBackend extends BaseBackend<NodeOptions> {
4141
const message = `Non-Error exception captured with keys: ${extractExceptionKeysForMessage(exception)}`;
4242

4343
getCurrentHub().configureScope(scope => {
44-
scope.setExtra('__serialized__', normalizeToSize(exception));
44+
scope.setExtra('__serialized__', normalizeToSize(exception as Record<string, unknown>));
4545
});
4646

4747
ex = (hint && hint.syntheticException) || new Error(message);

packages/node/src/handlers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export function tracingHandler(): (
5656
// If there is a trace header set, we extract the data from it (parentSpanId, traceId, and sampling decision)
5757
let traceparentData;
5858
if (req.headers && isString(req.headers['sentry-trace'])) {
59-
traceparentData = extractTraceparentData(req.headers['sentry-trace']);
59+
traceparentData = extractTraceparentData(req.headers['sentry-trace'] as string);
6060
}
6161

6262
const transaction = startTransaction(

packages/serverless/src/awslambda.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ export function wrapHandler<TEvent, TResult>(
284284
let traceparentData;
285285
const eventWithHeaders = event as { headers?: { [key: string]: string } };
286286
if (eventWithHeaders.headers && isString(eventWithHeaders.headers['sentry-trace'])) {
287-
traceparentData = extractTraceparentData(eventWithHeaders.headers['sentry-trace']);
287+
traceparentData = extractTraceparentData(eventWithHeaders.headers['sentry-trace'] as string);
288288
}
289289
const transaction = startTransaction({
290290
name: context.functionName,

packages/serverless/src/gcpfunction/http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function _wrapHttpFunction(fn: HttpFunction, wrapOptions: Partial<HttpFunctionWr
5353
let traceparentData;
5454
const reqWithHeaders = req as { headers?: { [key: string]: string } };
5555
if (reqWithHeaders.headers && isString(reqWithHeaders.headers['sentry-trace'])) {
56-
traceparentData = extractTraceparentData(reqWithHeaders.headers['sentry-trace']);
56+
traceparentData = extractTraceparentData(reqWithHeaders.headers['sentry-trace'] as string);
5757
}
5858
const transaction = startTransaction({
5959
name: `${reqMethod} ${reqUrl}`,

packages/tracing/src/browser/request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export function fetchCallback(
185185
const options = (handlerData.args[1] = (handlerData.args[1] as { [key: string]: any }) || {});
186186
let headers = options.headers;
187187
if (isInstanceOf(request, Request)) {
188-
headers = request.headers;
188+
headers = (request as Request).headers;
189189
}
190190
if (headers) {
191191
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access

packages/tracing/src/transaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
3737
super(transactionContext);
3838

3939
if (isInstanceOf(hub, Hub)) {
40-
this._hub = hub;
40+
this._hub = hub as Hub;
4141
}
4242

4343
this.name = transactionContext.name || '';

packages/utils/src/is.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Primitive } from '@sentry/types';
99
* @param wat A value to be checked.
1010
* @returns A boolean representing the result.
1111
*/
12-
export function isError(wat: any): wat is Error {
12+
export function isError(wat: any): boolean {
1313
switch (Object.prototype.toString.call(wat)) {
1414
case '[object Error]':
1515
return true;
@@ -29,7 +29,7 @@ export function isError(wat: any): wat is Error {
2929
* @param wat A value to be checked.
3030
* @returns A boolean representing the result.
3131
*/
32-
export function isErrorEvent(wat: any): wat is ErrorEvent {
32+
export function isErrorEvent(wat: any): boolean {
3333
return Object.prototype.toString.call(wat) === '[object ErrorEvent]';
3434
}
3535

@@ -40,7 +40,7 @@ export function isErrorEvent(wat: any): wat is ErrorEvent {
4040
* @param wat A value to be checked.
4141
* @returns A boolean representing the result.
4242
*/
43-
export function isDOMError(wat: any): wat is DOMError {
43+
export function isDOMError(wat: any): boolean {
4444
return Object.prototype.toString.call(wat) === '[object DOMError]';
4545
}
4646

@@ -51,18 +51,18 @@ export function isDOMError(wat: any): wat is DOMError {
5151
* @param wat A value to be checked.
5252
* @returns A boolean representing the result.
5353
*/
54-
export function isDOMException(wat: any): wat is DOMException {
54+
export function isDOMException(wat: any): boolean {
5555
return Object.prototype.toString.call(wat) === '[object DOMException]';
5656
}
5757

5858
/**
59-
* Checks whether the given value's type is string
59+
* Checks whether given value's type is a string
6060
* {@link isString}.
6161
*
6262
* @param wat A value to be checked.
6363
* @returns A boolean representing the result.
6464
*/
65-
export function isString(wat: any): wat is string {
65+
export function isString(wat: any): boolean {
6666
return Object.prototype.toString.call(wat) === '[object String]';
6767
}
6868

@@ -84,7 +84,7 @@ export function isPrimitive(wat: any): wat is Primitive {
8484
* @param wat A value to be checked.
8585
* @returns A boolean representing the result.
8686
*/
87-
export function isPlainObject(wat: any): wat is { [key: string]: unknown } {
87+
export function isPlainObject(wat: any): boolean {
8888
return Object.prototype.toString.call(wat) === '[object Object]';
8989
}
9090

@@ -95,7 +95,7 @@ export function isPlainObject(wat: any): wat is { [key: string]: unknown } {
9595
* @param wat A value to be checked.
9696
* @returns A boolean representing the result.
9797
*/
98-
export function isEvent(wat: any): wat is Event {
98+
export function isEvent(wat: any): boolean {
9999
return typeof Event !== 'undefined' && isInstanceOf(wat, Event);
100100
}
101101

@@ -106,7 +106,7 @@ export function isEvent(wat: any): wat is Event {
106106
* @param wat A value to be checked.
107107
* @returns A boolean representing the result.
108108
*/
109-
export function isElement(wat: any): wat is Element {
109+
export function isElement(wat: any): boolean {
110110
return typeof Element !== 'undefined' && isInstanceOf(wat, Element);
111111
}
112112

@@ -117,7 +117,7 @@ export function isElement(wat: any): wat is Element {
117117
* @param wat A value to be checked.
118118
* @returns A boolean representing the result.
119119
*/
120-
export function isRegExp(wat: any): wat is RegExp {
120+
export function isRegExp(wat: any): boolean {
121121
return Object.prototype.toString.call(wat) === '[object RegExp]';
122122
}
123123

@@ -140,12 +140,6 @@ export function isThenable(wat: any): boolean {
140140
export function isSyntheticEvent(wat: any): boolean {
141141
return isPlainObject(wat) && 'nativeEvent' in wat && 'preventDefault' in wat && 'stopPropagation' in wat;
142142
}
143-
144-
// Having the two different types is necessary because some types packages type classes as functions (in addition to
145-
// being objects with a `new` method), and some only type them as `new`-able objects
146-
type Constructor<T> = (...args: any[]) => T;
147-
type ClassWithConstructor<T> = { new (...args: any[]): T };
148-
149143
/**
150144
* Checks whether given value's type is an instance of provided constructor.
151145
* {@link isInstanceOf}.
@@ -154,7 +148,7 @@ type ClassWithConstructor<T> = { new (...args: any[]): T };
154148
* @param base A constructor to be used in a check.
155149
* @returns A boolean representing the result.
156150
*/
157-
export function isInstanceOf<T>(wat: any, base: Constructor<T> | ClassWithConstructor<T>): wat is T {
151+
export function isInstanceOf(wat: any, base: any): boolean {
158152
try {
159153
return wat instanceof base;
160154
} catch (_e) {

packages/utils/src/object.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function getWalkSource(
103103
currentTarget?: unknown;
104104
}
105105

106-
const event = (value as unknown) as SimpleEvent;
106+
const event = value as SimpleEvent;
107107

108108
const source: {
109109
[key: string]: any;
@@ -381,8 +381,9 @@ export function extractExceptionKeysForMessage(exception: any, maxLength: number
381381
* Given any object, return the new object with removed keys that value was `undefined`.
382382
* Works recursively on objects and arrays.
383383
*/
384-
export function dropUndefinedKeys<T>(obj: T): T {
385-
if (isPlainObject(obj)) {
384+
export function dropUndefinedKeys<T>(val: T): T {
385+
if (isPlainObject(val)) {
386+
const obj = val as { [key: string]: any };
386387
const rv: { [key: string]: any } = {};
387388
for (const key of Object.keys(obj)) {
388389
if (typeof obj[key] !== 'undefined') {
@@ -392,11 +393,11 @@ export function dropUndefinedKeys<T>(obj: T): T {
392393
return rv as T;
393394
}
394395

395-
if (Array.isArray(obj)) {
396-
return (obj as any[]).map(dropUndefinedKeys) as any;
396+
if (Array.isArray(val)) {
397+
return (val as any[]).map(dropUndefinedKeys) as any;
397398
}
398399

399-
return obj;
400+
return val;
400401
}
401402

402403
/**

0 commit comments

Comments
 (0)