Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/sentry/utils/object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export function splitObject<T extends object, K extends (keyof T)[]>(obj: T | null, keys: K): [Pick<T, K[number]> | null, Omit<T, K[number]> | null] {
if (!obj) {
return [null, null];
}
const picked = {} as Pick<T, K[number]>;
const omitted = { ...obj } as Omit<T, K[number]>;

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];
}
21 changes: 15 additions & 6 deletions src/sentry/wrapper.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
23 changes: 16 additions & 7 deletions src/sentry/wrapper.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down