diff --git a/src/sentry/utils/object.ts b/src/sentry/utils/object.ts new file mode 100644 index 0000000..29baa99 --- /dev/null +++ b/src/sentry/utils/object.ts @@ -0,0 +1,16 @@ +export function splitObject(obj: T | null, keys: K): [Pick | null, Omit | null] { + if (!obj) { + return [null, null]; + } + const picked = {} as Pick; + const omitted = { ...obj } as Omit; + + keys.forEach((key) => { + if (key in obj) { + picked[key] = obj[key]; + delete omitted[key as any]; + } + }); + + return [Object.keys(picked).length > 0 ? picked : null, Object.keys(omitted).length > 0 ? omitted : null]; +} diff --git a/src/sentry/wrapper.android.ts b/src/sentry/wrapper.android.ts index 6ee1769..37cecae 100644 --- a/src/sentry/wrapper.android.ts +++ b/src/sentry/wrapper.android.ts @@ -10,6 +10,7 @@ import { utf8ToBytes } from './vendor'; import { SDK_NAME } from './version'; import { CLog, CLogTypes } from '.'; import { rewriteFrameIntegration } from './integrations/default'; +import { splitObject } from './utils/object'; enum JavaType { Long, @@ -840,20 +841,28 @@ export namespace NATIVE { }) ); } - export function setUser(user: User | null, otherUserKeys) { + export function setUser(user: User | null) { if (!enableNative) { return; } runOnScope((scope) => { - if (!user && !otherUserKeys) { + const [filteredUser, otherUserKeys] = splitObject(user, ['id', 'email', 'username', 'ip_address']); + if (!filteredUser && !otherUserKeys) { scope.setUser(null); } else { const userInstance = new io.sentry.protocol.User(); - if (user) { - userInstance.setId(user.id + ''); - userInstance.setEmail(user.email); - userInstance.setUsername(user.username); + if (typeof filteredUser?.id === 'number' || typeof filteredUser?.id === 'string') { + userInstance.setId(`${filteredUser.id}`); + } + if (typeof filteredUser?.email === 'string') { + userInstance.setEmail(filteredUser.email); + } + if (typeof filteredUser?.username === 'string') { + userInstance.setUsername(filteredUser.username); + } + if (typeof filteredUser?.ip_address === 'string') { + userInstance.setIpAddress(filteredUser.ip_address); } if (otherUserKeys) { diff --git a/src/sentry/wrapper.ios.ts b/src/sentry/wrapper.ios.ts index 03d05b8..a6f48bb 100644 --- a/src/sentry/wrapper.ios.ts +++ b/src/sentry/wrapper.ios.ts @@ -5,6 +5,7 @@ import { isHardCrash } from './misc'; import { NativescriptOptions } from './options'; import { utf8ToBytes } from './vendor'; import { rewriteFrameIntegration } from './integrations/default'; +import { splitObject } from './utils/object'; const numberHasDecimals = function (value: number): boolean { return !(value % 1 === 0); @@ -505,20 +506,28 @@ export namespace NATIVE { } } - export function setUser(user: User | null, otherUserKeys) { + export function setUser(user: User | null) { if (!enableNative) { return; } NSSentrySDK.configureScope((scope: SentryScope) => { - if (!user && !otherUserKeys) { + const [filteredUser, otherUserKeys] = splitObject(user, ['id', 'email', 'username', 'ip_address']); + + if (!filteredUser && !otherUserKeys) { scope.setUser(null); } else { const userInstance = SentryUser.alloc().init(); - - if (user) { - userInstance.userId = user.id + ''; - userInstance.email = user.email; - userInstance.username = user.username; + if (typeof filteredUser?.id === 'number' || typeof filteredUser?.id === 'string') { + userInstance.userId = `${filteredUser.id}`; + } + if (typeof filteredUser?.email === 'string') { + userInstance.email = filteredUser.email; + } + if (typeof filteredUser?.username === 'string') { + userInstance.username = filteredUser.username; + } + if (typeof filteredUser.ip_address === 'string') { + userInstance.ipAddress = filteredUser.ip_address; } if (otherUserKeys) {