Skip to content

Commit b833312

Browse files
committed
use typeguard for isPrimitive and take advantage of that where possible
1 parent 54a4bbb commit b833312

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

packages/browser/src/integrations/globalhandlers.ts

Lines changed: 9 additions & 5 deletions
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, Integration, Severity } from '@sentry/types';
3+
import { Event, Integration, Primitive, Severity } from '@sentry/types';
44
import {
55
addExceptionMechanism,
66
addInstrumentationHandler,
@@ -152,7 +152,7 @@ export class GlobalHandlers implements Integration {
152152

153153
const client = currentHub.getClient();
154154
const event = isPrimitive(error)
155-
? this._eventFromIncompleteRejection(error)
155+
? this._eventFromRejectionWithPrimitive(error)
156156
: eventFromUnknownInput(error, undefined, {
157157
attachStacktrace: client && client.getOptions().attachStacktrace,
158158
rejection: true,
@@ -211,16 +211,20 @@ export class GlobalHandlers implements Integration {
211211
}
212212

213213
/**
214-
* This function creates an Event from an TraceKitStackTrace that has part of it missing.
214+
* Create an event from a promise rejection where the `reason` is a primitive.
215+
*
216+
* @param reason: The `reason` property of the promise rejection
217+
* @returns An Event object with an appropriate `exception` value
215218
*/
216219
// eslint-disable-next-line @typescript-eslint/no-explicit-any
217-
private _eventFromIncompleteRejection(error: any): Event {
220+
private _eventFromRejectionWithPrimitive(reason: Primitive): Event {
218221
return {
219222
exception: {
220223
values: [
221224
{
222225
type: 'UnhandledRejection',
223-
value: `Non-Error promise rejection captured with value: ${error}`,
226+
// String() is needed because the Primitive type includes symbols (which can't be automatically stringified)
227+
value: `Non-Error promise rejection captured with value: ${String(reason)}`,
224228
},
225229
],
226230
},

packages/core/src/baseclient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
119119
let eventId: string | undefined = hint && hint.event_id;
120120

121121
const promisedEvent = isPrimitive(message)
122-
? this._getBackend().eventFromMessage(`${message}`, level, hint)
122+
? this._getBackend().eventFromMessage(String(message), level, hint)
123123
: this._getBackend().eventFromException(message, hint);
124124

125125
this._process(

packages/utils/src/is.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
3+
4+
import { Primitive } from '@sentry/types';
35
/**
46
* Checks whether given value's type is one of a few Error or Error-like
57
* {@link isError}.
@@ -65,13 +67,13 @@ export function isString(wat: any): boolean {
6567
}
6668

6769
/**
68-
* Checks whether given value's is a primitive (undefined, null, number, boolean, string)
70+
* Checks whether given value's is a primitive (undefined, null, number, boolean, string, bigint, symbol)
6971
* {@link isPrimitive}.
7072
*
7173
* @param wat A value to be checked.
7274
* @returns A boolean representing the result.
7375
*/
74-
export function isPrimitive(wat: any): boolean {
76+
export function isPrimitive(wat: any): wat is Primitive {
7577
return wat === null || (typeof wat !== 'object' && typeof wat !== 'function');
7678
}
7779

packages/utils/src/object.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,13 @@ export function normalizeToSize<T>(
170170
return serialized as T;
171171
}
172172

173-
/** Transforms any input value into a string form, either primitive value or a type of the input */
173+
/**
174+
* Transform any non-primitive or Symbol-type value into a string. Acts as a no-op on non-Symbol primitives.
175+
*
176+
* @param value The value to stringify
177+
* @returns For non-primitive and Symbol-type values, a string denoting the value's type, or in the case of a Symbol,
178+
* its type and description. For non-Symbol primitives, the original value, unchanged.
179+
*/
174180
function serializeValue(value: any): any {
175181
const type = Object.prototype.toString.call(value);
176182

@@ -236,6 +242,10 @@ function normalizeValue<T>(value: T, key?: any): T | string {
236242
return `[Function: ${getFunctionName(value)}]`;
237243
}
238244

245+
if (typeof value === 'symbol') {
246+
return `[${String(value)}]`;
247+
}
248+
239249
return value;
240250
}
241251

0 commit comments

Comments
 (0)