diff --git a/packages/sentry/scope.d.ts b/packages/sentry/scope.d.ts index cb0ff5c..04fdac2 100644 --- a/packages/sentry/scope.d.ts +++ b/packages/sentry/scope.d.ts @@ -1 +1,53 @@ -export {}; +import { Scope } from '@sentry/core'; +import type { Attachment, Breadcrumb, User } from '@sentry/types'; +/** + * Extends the scope methods to set scope on the Native SDKs + */ +export declare class NativescriptScope extends Scope { + /** + * @inheritDoc + */ + setUser(user: User | null): this; + /** + * @inheritDoc + */ + setTag(key: string, value: string): this; + /** + * @inheritDoc + */ + setTags(tags: { + [key: string]: string; + }): this; + /** + * @inheritDoc + */ + setExtras(extras: { + [key: string]: any; + }): this; + /** + * @inheritDoc + */ + setExtra(key: string, extra: any): this; + /** + * @inheritDoc + */ + addBreadcrumb(breadcrumb: Breadcrumb, maxBreadcrumbs?: number): this; + /** + * @inheritDoc + */ + clearBreadcrumbs(): this; + /** + * @inheritDoc + */ + setContext(key: string, context: { + [key: string]: any; + } | null): this; + /** + * @inheritDoc + */ + addAttachment(attachment: Attachment): this; + /** + * @inheritDoc + */ + clearAttachments(): this; +} diff --git a/packages/sentry/scope.js b/packages/sentry/scope.js index e6a080e..b852a9f 100644 --- a/packages/sentry/scope.js +++ b/packages/sentry/scope.js @@ -1,94 +1,93 @@ +import { Scope } from '@sentry/core'; +import { DEFAULT_BREADCRUMB_LEVEL } from './breadcrumb'; +import { convertToNormalizedObject } from './utils/normalize'; +import { NATIVE } from './wrapper'; /** * Extends the scope methods to set scope on the Native SDKs */ -const methods = ['addBreadcrumb', 'addAttachment']; -export {}; -// methods.forEach(m=>{ -// const originalMethod =Scope.prototype[m] as Function; -// Scope.prototype[m] = function(...args) { -// NATIVE[m](...args); -// return originalMethod.call(this, ...args); -// }; -// }); -// export class NativescriptScope extends Scope { -// /** -// * @inheritDoc -// */ -// public setUser(user: User | null): this { -// NATIVE.setUser(user); -// return super.setUser(user); -// } -// /** -// * @inheritDoc -// */ -// public setTag(key: string, value: string): this { -// NATIVE.setTag(key, value); -// return super.setTag(key, value); -// } -// /** -// * @inheritDoc -// */ -// public setTags(tags: { [key: string]: string }): this { -// // As native only has setTag, we just loop through each tag key. -// Object.keys(tags).forEach((key) => { -// NATIVE.setTag(key, tags[key]); -// }); -// return super.setTags(tags); -// } -// /** -// * @inheritDoc -// */ -// // eslint-disable-next-line @typescript-eslint/no-explicit-any -// public setExtras(extras: { [key: string]: any }): this { -// Object.keys(extras).forEach((key) => { -// NATIVE.setExtra(key, extras[key]); -// }); -// return super.setExtras(extras); -// } -// /** -// * @inheritDoc -// */ -// // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types,@typescript-eslint/no-explicit-any -// public setExtra(key: string, extra: any): this { -// NATIVE.setExtra(key, extra); -// return super.setExtra(key, extra); -// } -// /** -// * @inheritDoc -// */ -// public addBreadcrumb(breadcrumb: Breadcrumb, maxBreadcrumbs?: number): this { -// NATIVE.addBreadcrumb(breadcrumb); -// return super.addBreadcrumb(breadcrumb, maxBreadcrumbs); -// } -// /** -// * @inheritDoc -// */ -// public clearBreadcrumbs(): this { -// NATIVE.clearBreadcrumbs(); -// return super.clearBreadcrumbs(); -// } -// /** -// * @inheritDoc -// */ -// // eslint-disable-next-line @typescript-eslint/no-explicit-any -// public setContext(key: string, context: { [key: string]: any } | null): this { -// NATIVE.setContext(key, context); -// return this; -// // return super.setContext(key, context); -// } -// /** -// * @inheritDoc -// */ -// public addAttachment(attachment: Attachment): this { -// console.log('test addAttachment', attachment); -// NATIVE.addAttachment(attachment); -// return super.addAttachment(attachment); -// } -// /** -// * @inheritDoc -// */ -// public clearAttachments(): this { -// return super.clearAttachments(); -// } -// } +export class NativescriptScope extends Scope { + /** + * @inheritDoc + */ + setUser(user) { + NATIVE.setUser(user); + return super.setUser(user); + } + /** + * @inheritDoc + */ + setTag(key, value) { + NATIVE.setTag(key, value); + return super.setTag(key, value); + } + /** + * @inheritDoc + */ + setTags(tags) { + // As native only has setTag, we just loop through each tag key. + Object.keys(tags).forEach((key) => { + NATIVE.setTag(key, tags[key]); + }); + return super.setTags(tags); + } + /** + * @inheritDoc + */ + // eslint-disable-next-line @typescript-eslint/no-explicit-any + setExtras(extras) { + Object.keys(extras).forEach((key) => { + NATIVE.setExtra(key, extras[key]); + }); + return super.setExtras(extras); + } + /** + * @inheritDoc + */ + // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types,@typescript-eslint/no-explicit-any + setExtra(key, extra) { + NATIVE.setExtra(key, extra); + return super.setExtra(key, extra); + } + /** + * @inheritDoc + */ + addBreadcrumb(breadcrumb, maxBreadcrumbs) { + const mergedBreadcrumb = { + ...breadcrumb, + level: breadcrumb.level || DEFAULT_BREADCRUMB_LEVEL, + data: breadcrumb.data ? convertToNormalizedObject(breadcrumb.data) : undefined + }; + super.addBreadcrumb(mergedBreadcrumb, maxBreadcrumbs); + const finalBreadcrumb = this._breadcrumbs[this._breadcrumbs.length - 1]; + NATIVE.addBreadcrumb(finalBreadcrumb); + return this; + } + /** + * @inheritDoc + */ + clearBreadcrumbs() { + NATIVE.clearBreadcrumbs(); + return super.clearBreadcrumbs(); + } + /** + * @inheritDoc + */ + // eslint-disable-next-line @typescript-eslint/no-explicit-any + setContext(key, context) { + NATIVE.setContext(key, context); + return super.setContext(key, context); + } + /** + * @inheritDoc + */ + addAttachment(attachment) { + return super.addAttachment(attachment); + } + /** + * @inheritDoc + */ + clearAttachments() { + return super.clearAttachments(); + } +} //# sourceMappingURL=scope.js.map \ No newline at end of file diff --git a/packages/sentry/wrapper.android.d.ts b/packages/sentry/wrapper.android.d.ts index f45f55b..187c59d 100644 --- a/packages/sentry/wrapper.android.d.ts +++ b/packages/sentry/wrapper.android.d.ts @@ -1,4 +1,4 @@ -import { Envelope } from '@sentry/types'; +import { Breadcrumb, Envelope, User } from '@sentry/types'; import { NativescriptOptions } from './options'; export declare namespace NATIVE { function isNativeTransportAvailable(): boolean; @@ -35,4 +35,12 @@ export declare namespace NATIVE { isColdStart: java.lang.Boolean; didFetchAppStart: boolean; }>; + function setUser(user: User | null, otherUserKeys: any): void; + function setTag(key: string, value: string): void; + function setExtra(key: string, extra: any): void; + function addBreadcrumb(breadcrumb: Breadcrumb, maxBreadcrumbs?: number): void; + function clearBreadcrumbs(): void; + function setContext(key: string, context: { + [key: string]: any; + } | null): void; } diff --git a/src/sentry/index.ts b/src/sentry/index.ts index 0a9ba0e..44dbe86 100755 --- a/src/sentry/index.ts +++ b/src/sentry/index.ts @@ -13,8 +13,6 @@ export { setContext, setExtra, setExtras, - withScope, - configureScope, setTag, setTags, setUser, @@ -40,7 +38,7 @@ import { Trace } from '@nativescript/core'; export { NativescriptOptions } from './options'; export { NativescriptClient } from './client'; -export { init, setDist, setRelease, nativeCrash, flush, close, captureUserFeedback } from './sdk'; +export { configureScope, init, setDist, setRelease, nativeCrash, flush, close, captureUserFeedback , withScope} from './sdk'; // export { TouchEventBoundary, withTouchEventBoundary } from './touchevents'; export { diff --git a/src/sentry/scope.ts b/src/sentry/scope.ts index 21d0c34..4583c73 100644 --- a/src/sentry/scope.ts +++ b/src/sentry/scope.ts @@ -1,109 +1,106 @@ import { Scope } from '@sentry/core'; -import { Attachment, Breadcrumb, User } from '@sentry/types'; +import type { Attachment, Breadcrumb, User } from '@sentry/types'; +import { DEFAULT_BREADCRUMB_LEVEL } from './breadcrumb'; +import { convertToNormalizedObject } from './utils/normalize'; import { NATIVE } from './wrapper'; /** * Extends the scope methods to set scope on the Native SDKs */ +export class NativescriptScope extends Scope { + /** + * @inheritDoc + */ + public setUser(user: User | null): this { + NATIVE.setUser(user); + return super.setUser(user); + } -const methods = ['addBreadcrumb', 'addAttachment']; + /** + * @inheritDoc + */ + public setTag(key: string, value: string): this { + NATIVE.setTag(key, value); + return super.setTag(key, value); + } -// methods.forEach(m=>{ -// const originalMethod =Scope.prototype[m] as Function; -// Scope.prototype[m] = function(...args) { -// NATIVE[m](...args); -// return originalMethod.call(this, ...args); -// }; -// }); + /** + * @inheritDoc + */ + public setTags(tags: { [key: string]: string }): this { + // As native only has setTag, we just loop through each tag key. + Object.keys(tags).forEach((key) => { + NATIVE.setTag(key, tags[key]); + }); + return super.setTags(tags); + } -// export class NativescriptScope extends Scope { -// /** -// * @inheritDoc -// */ -// public setUser(user: User | null): this { -// NATIVE.setUser(user); -// return super.setUser(user); -// } + /** + * @inheritDoc + */ + // eslint-disable-next-line @typescript-eslint/no-explicit-any + public setExtras(extras: { [key: string]: any }): this { + Object.keys(extras).forEach((key) => { + NATIVE.setExtra(key, extras[key]); + }); + return super.setExtras(extras); + } -// /** -// * @inheritDoc -// */ -// public setTag(key: string, value: string): this { -// NATIVE.setTag(key, value); -// return super.setTag(key, value); -// } + /** + * @inheritDoc + */ + // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types,@typescript-eslint/no-explicit-any + public setExtra(key: string, extra: any): this { + NATIVE.setExtra(key, extra); + return super.setExtra(key, extra); + } -// /** -// * @inheritDoc -// */ -// public setTags(tags: { [key: string]: string }): this { -// // As native only has setTag, we just loop through each tag key. -// Object.keys(tags).forEach((key) => { -// NATIVE.setTag(key, tags[key]); -// }); -// return super.setTags(tags); -// } + /** + * @inheritDoc + */ + public addBreadcrumb(breadcrumb: Breadcrumb, maxBreadcrumbs?: number): this { + const mergedBreadcrumb: Breadcrumb = { + ...breadcrumb, + level: breadcrumb.level || DEFAULT_BREADCRUMB_LEVEL, + data: breadcrumb.data ? convertToNormalizedObject(breadcrumb.data) : undefined + }; -// /** -// * @inheritDoc -// */ -// // eslint-disable-next-line @typescript-eslint/no-explicit-any -// public setExtras(extras: { [key: string]: any }): this { -// Object.keys(extras).forEach((key) => { -// NATIVE.setExtra(key, extras[key]); -// }); -// return super.setExtras(extras); -// } + super.addBreadcrumb(mergedBreadcrumb, maxBreadcrumbs); -// /** -// * @inheritDoc -// */ -// // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types,@typescript-eslint/no-explicit-any -// public setExtra(key: string, extra: any): this { -// NATIVE.setExtra(key, extra); -// return super.setExtra(key, extra); -// } + const finalBreadcrumb = this._breadcrumbs[this._breadcrumbs.length - 1]; + NATIVE.addBreadcrumb(finalBreadcrumb); + return this; + } -// /** -// * @inheritDoc -// */ -// public addBreadcrumb(breadcrumb: Breadcrumb, maxBreadcrumbs?: number): this { -// NATIVE.addBreadcrumb(breadcrumb); -// return super.addBreadcrumb(breadcrumb, maxBreadcrumbs); -// } + /** + * @inheritDoc + */ + public clearBreadcrumbs(): this { + NATIVE.clearBreadcrumbs(); + return super.clearBreadcrumbs(); + } -// /** -// * @inheritDoc -// */ -// public clearBreadcrumbs(): this { -// NATIVE.clearBreadcrumbs(); -// return super.clearBreadcrumbs(); -// } + /** + * @inheritDoc + */ + // eslint-disable-next-line @typescript-eslint/no-explicit-any + public setContext(key: string, context: { [key: string]: any } | null): this { + NATIVE.setContext(key, context); + return super.setContext(key, context); + } -// /** -// * @inheritDoc -// */ -// // eslint-disable-next-line @typescript-eslint/no-explicit-any -// public setContext(key: string, context: { [key: string]: any } | null): this { -// NATIVE.setContext(key, context); -// return this; -// // return super.setContext(key, context); -// } + /** + * @inheritDoc + */ + public addAttachment(attachment: Attachment): this { + return super.addAttachment(attachment); + } -// /** -// * @inheritDoc -// */ -// public addAttachment(attachment: Attachment): this { -// console.log('test addAttachment', attachment); -// NATIVE.addAttachment(attachment); -// return super.addAttachment(attachment); -// } - -// /** -// * @inheritDoc -// */ -// public clearAttachments(): this { -// return super.clearAttachments(); -// } -// } + /** + * @inheritDoc + */ + public clearAttachments(): this { + return super.clearAttachments(); + } +} diff --git a/src/sentry/sdk.ts b/src/sentry/sdk.ts index 7ce9c5c..a9f7f0c 100755 --- a/src/sentry/sdk.ts +++ b/src/sentry/sdk.ts @@ -21,6 +21,7 @@ import { NATIVE } from './wrapper'; import { parseErrorStack } from './integrations/debugsymbolicator'; import { Screenshot } from './integrations/screenshot'; import { getDefaultEnvironment } from './utils/environment'; +import { NativescriptScope } from './scope'; // const STACKTRACE_LIMIT = 50; // function stripSentryFramesAndReverse(stack) { @@ -86,7 +87,7 @@ const DEFAULT_OPTIONS: NativescriptOptions & NativescriptErrorHandlersOptions = * Inits the SDK */ export function init(passedOptions: NativescriptOptions): void { - const NativescriptHub = new Hub(undefined, new Scope()); + const NativescriptHub = new Hub(undefined, new NativescriptScope()); // const NativescriptHub = new Hub(undefined, new NativescriptScope()); makeMain(NativescriptHub); diff --git a/src/sentry/utils/normalize.ts b/src/sentry/utils/normalize.ts new file mode 100644 index 0000000..d6f50a8 --- /dev/null +++ b/src/sentry/utils/normalize.ts @@ -0,0 +1,19 @@ +import { normalize } from '@sentry/utils'; + +const KEY = 'value'; + +/** + * Converts any input into a valid record with string keys. + */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export function convertToNormalizedObject(data: unknown): Record { + const normalized: unknown = normalize(data); + if (normalized === null || typeof normalized !== 'object') { + return { + [KEY]: normalized + }; + } else { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + return normalized as Record; + } +} diff --git a/src/sentry/wrapper.android.ts b/src/sentry/wrapper.android.ts index 824d35c..bc9232e 100644 --- a/src/sentry/wrapper.android.ts +++ b/src/sentry/wrapper.android.ts @@ -1,6 +1,7 @@ import { createArrayBuffer, pointsFromBuffer } from '@nativescript-community/arraybuffers'; import { Application, Trace, Utils } from '@nativescript/core'; -import { BaseEnvelopeItemHeaders, Breadcrumb, Envelope, EnvelopeItem, Event, SeverityLevel } from '@sentry/types'; +import { dataSerialize } from '@nativescript/core/utils/native-helper'; +import { BaseEnvelopeItemHeaders, Breadcrumb, Envelope, EnvelopeItem, Event, SeverityLevel, User } from '@sentry/types'; import { SentryError } from '@sentry/utils'; import { parseErrorStack } from './integrations/debugsymbolicator'; import { isHardCrash } from './misc'; @@ -791,4 +792,136 @@ export namespace NATIVE { }; } } + + function eventLevel(level) { + switch (level) { + case 'fatal': + return io.sentry.SentryLevel.FATAL; + case 'warning': + return io.sentry.SentryLevel.WARNING; + case 'info': + case 'log': + return io.sentry.SentryLevel.INFO; + case 'debug': + return io.sentry.SentryLevel.DEBUG; + default: + return io.sentry.SentryLevel.ERROR; + } + } + function runOnScope(callback: (scope: io.sentry.IScope) => void) { + io.sentry.Sentry.configureScope( + new io.sentry.ScopeCallback({ + run: callback + }) + ); + } + export function setUser(user: User | null, otherUserKeys) { + if (!enableNative) { + return; + } + runOnScope((scope) => { + if (!user && !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 (otherUserKeys) { + userInstance.setData(dataSerialize(otherUserKeys)); + } + + scope.setUser(userInstance); + } + }); + } + export function setTag(key: string, value: string) { + if (!enableNative) { + return; + } + runOnScope((scope) => { + scope.setTag(key, value); + }); + } + + export function setExtra(key: string, extra: any) { + if (!enableNative) { + return; + } + runOnScope((scope) => { + scope.setExtra(key, extra); + }); + } + + export function addBreadcrumb(breadcrumb: Breadcrumb, maxBreadcrumbs?: number) { + if (!enableNative) { + return; + } + runOnScope((scope) => { + const breadcrumbInstance = new io.sentry.Breadcrumb(); + + if (breadcrumb.level) { + breadcrumbInstance.setLevel(eventLevel(breadcrumb.level)); + } + breadcrumbInstance.setCategory(breadcrumb.category); + breadcrumbInstance.setType(breadcrumb.type); + breadcrumbInstance.setMessage(breadcrumb.message); + + if (breadcrumb.data) { + Object.keys(breadcrumb.data).forEach((key) => breadcrumbInstance.setData(key, dataSerialize(breadcrumb.data[key]))); + } + + scope.addBreadcrumb(breadcrumbInstance); + }); + } + // let scopeScope = null; + // export function withScope(callback: (scope: Scope) => void) { + // scopeScope = SentryScope.alloc().init(); + // // NSSentrySDK.withScope(new io.sentry.ScopeCallback({ + // // run(nscope) { + // // nscope is ignored + // // console.log('native withScope', nscope); + // callback(null); + // scopeScope = null; + // // } + // // })); + // } + // export function addAttachment(attachment: Attachment) { + // if (!enableNative) { + // return; + // } + // runOnScope((scope) => { + + // if (attachment.data) { + // if (typeof attachment.data === 'string') { + // attachment.data = new TextEncoder().encode(attachment.data); + // } + // scope.addAttachment(SentryAttachment.alloc().initWithDataFilenameContentType(attachment.data.buffer as any, attachment.filename, attachment.contentType)); + // } else { + // scope.addAttachment(SentryAttachment.alloc().initWithPath(attachment.filename)); + // } + // }); + // } + export function clearBreadcrumbs() { + if (!enableNative) { + return; + } + runOnScope((scope) => scope.clearBreadcrumbs()); + } + export function setContext(key: string, context: { [key: string]: any } | null) { + if (!enableNative) { + return; + } + runOnScope((scope) => { + if (!context || !key) { + return; + } else { + scope.setContexts(key, dataSerialize(context)); + } + }); + } } diff --git a/src/sentry/wrapper.ios.ts b/src/sentry/wrapper.ios.ts index d61ed5a..037e516 100644 --- a/src/sentry/wrapper.ios.ts +++ b/src/sentry/wrapper.ios.ts @@ -1,4 +1,4 @@ -import { BaseEnvelopeItemHeaders, Breadcrumb, Envelope, EnvelopeItem, Event, SeverityLevel } from '@sentry/types'; +import { BaseEnvelopeItemHeaders, Breadcrumb, Envelope, EnvelopeItem, Event, SeverityLevel, User } from '@sentry/types'; import { SentryError, logger } from '@sentry/utils'; import { parseErrorStack } from './integrations/debugsymbolicator'; import { isHardCrash } from './misc'; @@ -489,91 +489,89 @@ export namespace NATIVE { // } // NSSentrySDK.captureUserFeedback(userFeedback); // } - // function eventLevel(level) { - // switch (level) { - // case 'fatal': - // return SentryLevel.kSentryLevelFatal; - // case 'warning': - // return SentryLevel.kSentryLevelWarning; - // case 'info': - // case 'log': - // return SentryLevel.kSentryLevelInfo; - // case 'debug': - // return SentryLevel.kSentryLevelDebug; - // default: - // return SentryLevel.kSentryLevelError; - // } - // } - - // export function setUser(user: User | null, otherUserKeys) { - // if (!enableNative) { - // return; - // } - // NSSentrySDK.configureScope((scope: SentryScope) => { - // if (!user && !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 (otherUserKeys) { - // const nStr = NSString.stringWithString(JSON.stringify(otherUserKeys)); - // const nData = nStr.dataUsingEncoding(NSUTF8StringEncoding); - // userInstance.data = NSJSONSerialization.JSONObjectWithDataOptionsError(nData, 0); - // } + function eventLevel(level) { + switch (level) { + case 'fatal': + return SentryLevel.kSentryLevelFatal; + case 'warning': + return SentryLevel.kSentryLevelWarning; + case 'info': + case 'log': + return SentryLevel.kSentryLevelInfo; + case 'debug': + return SentryLevel.kSentryLevelDebug; + default: + return SentryLevel.kSentryLevelError; + } + } - // scope.setUser(userInstance); - // } - // }); - // } - // export function setTag(key: string, value: string) { - // if (!enableNative) { - // return; - // } - // NSSentrySDK.configureScope((scope: SentryScope) => { - // scope.setTagValueForKey(value, key); - // }); - // } + export function setUser(user: User | null, otherUserKeys) { + if (!enableNative) { + return; + } + NSSentrySDK.configureScope((scope: SentryScope) => { + if (!user && !otherUserKeys) { + scope.setUser(null); + } else { + const userInstance = SentryUser.alloc().init(); - // export function setExtra(key: string, extra: any) { - // if (!enableNative) { - // return; - // } - // NSSentrySDK.configureScope((scope: SentryScope) => { - // scope.setExtraValueForKey(extra, key); - // }); - // } + if (user) { + userInstance.userId = user.id + ''; + userInstance.email = user.email; + userInstance.username = user.username; + } - // export function addBreadcrumb(breadcrumb: Breadcrumb, maxBreadcrumbs?: number) { - // if (!enableNative) { - // return; - // } - // NSSentrySDK.configureScope((scope: SentryScope) => { - // const breadcrumbInstance = SentryBreadcrumb.alloc().init(); + if (otherUserKeys) { + const nStr = NSString.stringWithString(JSON.stringify(otherUserKeys)); + const nData = nStr.dataUsingEncoding(NSUTF8StringEncoding); + userInstance.data = NSJSONSerialization.JSONObjectWithDataOptionsError(nData, 0 as any); + } - // if (breadcrumb.level) { - // breadcrumbInstance.level = eventLevel(breadcrumb.level); + scope.setUser(userInstance); + } + }); + } + export function setTag(key: string, value: string) { + if (!enableNative) { + return; + } + NSSentrySDK.configureScope((scope: SentryScope) => { + scope.setTagValueForKey(value, key); + }); + } - // } - // breadcrumbInstance.category = breadcrumb.category; + export function setExtra(key: string, extra: any) { + if (!enableNative) { + return; + } + NSSentrySDK.configureScope((scope: SentryScope) => { + scope.setExtraValueForKey(extra, key); + }); + } - // breadcrumbInstance.type = breadcrumb.type; + export function addBreadcrumb(breadcrumb: Breadcrumb, maxBreadcrumbs?: number) { + if (!enableNative) { + return; + } + NSSentrySDK.configureScope((scope: SentryScope) => { + const breadcrumbInstance = SentryBreadcrumb.alloc().init(); - // breadcrumbInstance.message = breadcrumb.message; - // if (breadcrumb.data) { - // const nStr = NSString.stringWithString(JSON.stringify(breadcrumb.data)); - // const nData = nStr.dataUsingEncoding(NSUTF8StringEncoding); - // breadcrumbInstance.data = NSJSONSerialization.JSONObjectWithDataOptionsError(nData, 0); - // } + if (breadcrumb.level) { + breadcrumbInstance.level = eventLevel(breadcrumb.level); + } + breadcrumbInstance.category = breadcrumb.category; + breadcrumbInstance.type = breadcrumb.type; + breadcrumbInstance.message = breadcrumb.message; + + if (breadcrumb.data) { + const nStr = NSString.stringWithString(JSON.stringify(breadcrumb.data)); + const nData = nStr.dataUsingEncoding(NSUTF8StringEncoding); + breadcrumbInstance.data = NSJSONSerialization.JSONObjectWithDataOptionsError(nData, 0 as any); + } - // scope.addBreadcrumb(breadcrumbInstance); - // }); - // } + scope.addBreadcrumb(breadcrumbInstance); + }); + } // let scopeScope = null; // export function withScope(callback: (scope: Scope) => void) { // scopeScope = SentryScope.alloc().init(); @@ -602,28 +600,26 @@ export namespace NATIVE { // } // }); // } - // export function clearBreadcrumbs() { - // if (!enableNative) { - // return; - // } - // NSSentrySDK.configureScope((scope: SentryScope) => { - // scope.clearBreadcrumbs(); - // }); - // } - // export function setContext(key: string, context: { [key: string]: any } | null) { - // if (!enableNative) { - // return; - // } - // NSSentrySDK.configureScope((scope: SentryScope) => { - // if (!context) { - // scope.setContextValueForKey(null, key); - // } else { - // const nStr = NSString.stringWithString(JSON.stringify(context)); - // const nData = nStr.dataUsingEncoding(NSUTF8StringEncoding); - // scope.setContextValueForKey(NSJSONSerialization.JSONObjectWithDataOptionsError(nData, 0), key); - // } - // }); - // } + export function clearBreadcrumbs() { + if (!enableNative) { + return; + } + NSSentrySDK.configureScope((scope: SentryScope) => scope.clearBreadcrumbs()); + } + export function setContext(key: string, context: { [key: string]: any } | null) { + if (!enableNative) { + return; + } + NSSentrySDK.configureScope((scope: SentryScope) => { + if (!context) { + scope.setContextValueForKey(null, key); + } else { + const nStr = NSString.stringWithString(JSON.stringify(context)); + const nData = nStr.dataUsingEncoding(NSUTF8StringEncoding); + scope.setContextValueForKey(NSJSONSerialization.JSONObjectWithDataOptionsError(nData, 0 as any), key); + } + }); + } export function enableNativeFramesTracking() { // Do nothing on iOS, this bridge method only has an effect on android.