Skip to content

Commit 2c0dc05

Browse files
authored
Merge pull request #31 from nativescript-community/fix/support-all-user-fields
fix: ensure setUser also includes custom fields
2 parents a0d0548 + aca4850 commit 2c0dc05

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

src/sentry/utils/object.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
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] {
2+
if (!obj) {
3+
return [null, null];
4+
}
5+
const picked = {} as Pick<T, K[number]>;
6+
const omitted = { ...obj } as Omit<T, K[number]>;
7+
8+
keys.forEach((key) => {
9+
if (key in obj) {
10+
picked[key] = obj[key];
11+
delete omitted[key as any];
12+
}
13+
});
14+
15+
return [Object.keys(picked).length > 0 ? picked : null, Object.keys(omitted).length > 0 ? omitted : null];
16+
}

src/sentry/wrapper.android.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { utf8ToBytes } from './vendor';
1010
import { SDK_NAME } from './version';
1111
import { CLog, CLogTypes } from '.';
1212
import { rewriteFrameIntegration } from './integrations/default';
13+
import { splitObject } from './utils/object';
1314

1415
enum JavaType {
1516
Long,
@@ -840,20 +841,28 @@ export namespace NATIVE {
840841
})
841842
);
842843
}
843-
export function setUser(user: User | null, otherUserKeys) {
844+
export function setUser(user: User | null) {
844845
if (!enableNative) {
845846
return;
846847
}
847848
runOnScope((scope) => {
848-
if (!user && !otherUserKeys) {
849+
const [filteredUser, otherUserKeys] = splitObject(user, ['id', 'email', 'username', 'ip_address']);
850+
if (!filteredUser && !otherUserKeys) {
849851
scope.setUser(null);
850852
} else {
851853
const userInstance = new io.sentry.protocol.User();
852854

853-
if (user) {
854-
userInstance.setId(user.id + '');
855-
userInstance.setEmail(user.email);
856-
userInstance.setUsername(user.username);
855+
if (typeof filteredUser?.id === 'number' || typeof filteredUser?.id === 'string') {
856+
userInstance.setId(`${filteredUser.id}`);
857+
}
858+
if (typeof filteredUser?.email === 'string') {
859+
userInstance.setEmail(filteredUser.email);
860+
}
861+
if (typeof filteredUser?.username === 'string') {
862+
userInstance.setUsername(filteredUser.username);
863+
}
864+
if (typeof filteredUser?.ip_address === 'string') {
865+
userInstance.setIpAddress(filteredUser.ip_address);
857866
}
858867

859868
if (otherUserKeys) {

src/sentry/wrapper.ios.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { isHardCrash } from './misc';
55
import { NativescriptOptions } from './options';
66
import { utf8ToBytes } from './vendor';
77
import { rewriteFrameIntegration } from './integrations/default';
8+
import { splitObject } from './utils/object';
89

910
const numberHasDecimals = function (value: number): boolean {
1011
return !(value % 1 === 0);
@@ -505,20 +506,28 @@ export namespace NATIVE {
505506
}
506507
}
507508

508-
export function setUser(user: User | null, otherUserKeys) {
509+
export function setUser(user: User | null) {
509510
if (!enableNative) {
510511
return;
511512
}
512513
NSSentrySDK.configureScope((scope: SentryScope) => {
513-
if (!user && !otherUserKeys) {
514+
const [filteredUser, otherUserKeys] = splitObject(user, ['id', 'email', 'username', 'ip_address']);
515+
516+
if (!filteredUser && !otherUserKeys) {
514517
scope.setUser(null);
515518
} else {
516519
const userInstance = SentryUser.alloc().init();
517-
518-
if (user) {
519-
userInstance.userId = user.id + '';
520-
userInstance.email = user.email;
521-
userInstance.username = user.username;
520+
if (typeof filteredUser?.id === 'number' || typeof filteredUser?.id === 'string') {
521+
userInstance.userId = `${filteredUser.id}`;
522+
}
523+
if (typeof filteredUser?.email === 'string') {
524+
userInstance.email = filteredUser.email;
525+
}
526+
if (typeof filteredUser?.username === 'string') {
527+
userInstance.username = filteredUser.username;
528+
}
529+
if (typeof filteredUser.ip_address === 'string') {
530+
userInstance.ipAddress = filteredUser.ip_address;
522531
}
523532

524533
if (otherUserKeys) {

0 commit comments

Comments
 (0)