Skip to content

Commit fda1a48

Browse files
author
farfromrefug
committed
chore: withScope fix and crashedLastRun
1 parent e558924 commit fda1a48

File tree

4 files changed

+31
-42
lines changed

4 files changed

+31
-42
lines changed

src/sentry/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import { Trace } from '@nativescript/core';
3838
export { NativescriptOptions } from './options';
3939
export { NativescriptClient } from './client';
4040

41-
export { configureScope, init, setDist, setRelease, nativeCrash, flush, close, captureUserFeedback , withScope} from './sdk';
41+
export { init, setDist, setRelease, nativeCrash, flush, close, captureUserFeedback, withScope, crashedLastRun } from './sdk';
4242
// export { TouchEventBoundary, withTouchEventBoundary } from './touchevents';
4343

4444
export {

src/sentry/sdk.ts

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
1-
import { Integrations, getCurrentHub, defaultIntegrations as sentryDefaultIntegrations } from '@sentry/browser';
2-
import { Hub, Scope, getIntegrationsToSetup, initAndBind, makeMain, setExtra } from '@sentry/core';
3-
import { RewriteFrames } from '@sentry/integrations';
4-
import { Integration, StackFrame, UserFeedback } from '@sentry/types';
1+
import { getCurrentHub } from '@sentry/browser';
2+
import { Hub, Scope, withScope as coreWithScope, getClient, getIntegrationsToSetup, initAndBind, makeMain, setExtra } from '@sentry/core';
3+
import { Integration, UserFeedback } from '@sentry/types';
54
import { logger, stackParserFromStackParserOptions } from '@sentry/utils';
65

76
import { NativescriptClientOptions, NativescriptOptions } from './options';
87

98
import { NativescriptClient } from './client';
10-
import { DeviceContext, NativescriptErrorHandlers, Release } from './integrations';
11-
import { EventOrigin } from './integrations/eventorigin';
12-
import { NativescriptErrorHandlersOptions } from './integrations/nativescripterrorhandlers';
13-
import { SdkInfo } from './integrations/sdkinfo';
149
import { getDefaultIntegrations } from './integrations/default';
10+
import { NativescriptErrorHandlersOptions } from './integrations/nativescripterrorhandlers';
1511
// import { NativescriptScope } from './scope';
16-
import { NativescriptTracing } from './tracing';
12+
import { parseErrorStack } from './integrations/debugsymbolicator';
13+
import { NativescriptScope } from './scope';
1714
import { DEFAULT_BUFFER_SIZE, makeNativescriptTransport } from './transports/native';
1815
import { makeUtf8TextEncoder } from './transports/TextEncoder';
19-
import { safeFactory, safeTracesSampler } from './utils/safe';
20-
import { NATIVE } from './wrapper';
21-
import { parseErrorStack } from './integrations/debugsymbolicator';
22-
import { Screenshot } from './integrations/screenshot';
2316
import { getDefaultEnvironment } from './utils/environment';
24-
import { NativescriptScope } from './scope';
17+
import { safeFactory, safeTracesSampler } from './utils/safe';
18+
import { NATIVE } from './wrapper.ios';
2519

2620
// const STACKTRACE_LIMIT = 50;
2721
// function stripSentryFramesAndReverse(stack) {
@@ -201,10 +195,7 @@ export function setDist(dist: string): void {
201195
* Use this only for testing purposes.
202196
*/
203197
export function nativeCrash(): void {
204-
const client = getCurrentHub().getClient<NativescriptClient>();
205-
if (client) {
206-
client.nativeCrash();
207-
}
198+
NATIVE.nativeCrash();
208199
}
209200

210201
/**
@@ -214,7 +205,7 @@ export function nativeCrash(): void {
214205
*/
215206
export async function flush(timeout: number = 0): Promise<boolean> {
216207
try {
217-
const client = getCurrentHub().getClient<NativescriptClient>();
208+
const client = getClient<NativescriptClient>();
218209

219210
if (client) {
220211
const result = await client.flush(timeout);
@@ -234,7 +225,7 @@ export async function flush(timeout: number = 0): Promise<boolean> {
234225
*/
235226
export async function close(): Promise<void> {
236227
try {
237-
const client = getCurrentHub().getClient<NativescriptClient>();
228+
const client = getClient<NativescriptClient>();
238229

239230
if (client) {
240231
await client.close();
@@ -247,7 +238,7 @@ export async function close(): Promise<void> {
247238
* Captures user feedback and sends it to Sentry.
248239
*/
249240
export function captureUserFeedback(feedback: UserFeedback): void {
250-
getCurrentHub().getClient<NativescriptClient>()?.captureUserFeedback(feedback);
241+
getClient<NativescriptClient>()?.captureUserFeedback(feedback);
251242
}
252243

253244
/**
@@ -263,30 +254,21 @@ export function captureUserFeedback(feedback: UserFeedback): void {
263254
*
264255
* @param callback that will be enclosed into push/popScope.
265256
*/
266-
export function withScope(callback: (scope: Scope) => void): ReturnType<Hub['withScope']> {
267-
const safeCallback = (scope: Scope): void => {
257+
export function withScope<T>(callback: (scope: Scope) => T): T | undefined {
258+
const safeCallback = (scope: Scope): T | undefined => {
268259
try {
269-
NATIVE.withScope((nscope) => {
270-
callback(scope);
271-
});
260+
return callback(scope);
272261
} catch (e) {
273262
logger.error('Error while running withScope callback', e);
263+
return undefined;
274264
}
275265
};
276-
return getCurrentHub().withScope(safeCallback);
266+
return coreWithScope(safeCallback);
277267
}
278268

279269
/**
280-
* Callback to set context information onto the scope.
281-
* @param callback Callback function that receives Scope.
270+
* Returns if the app crashed in the last run.
282271
*/
283-
export function configureScope(callback: (scope: Scope) => void): ReturnType<Hub['configureScope']> {
284-
const safeCallback = (scope: Scope): void => {
285-
try {
286-
callback(scope);
287-
} catch (e) {
288-
logger.error('Error while running configureScope callback', e);
289-
}
290-
};
291-
getCurrentHub().configureScope(safeCallback);
272+
export async function crashedLastRun(): Promise<boolean | null> {
273+
return NATIVE.crashedLastRun();
292274
}

src/sentry/wrapper.android.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ enum JavaType {
2020
const OPTIONS_SPECIAL_TYPES = {
2121
sampleRate: JavaType.Double,
2222
tracesSampleRate: JavaType.Double,
23-
enableTracing: JavaType.Boolean,
23+
enableTracing: JavaType.Boolean
2424
};
2525

2626
function capitalize(value) {
@@ -869,7 +869,7 @@ export namespace NATIVE {
869869
return;
870870
}
871871
runOnScope((scope) => {
872-
if (value){
872+
if (value) {
873873
scope.setTag(key, value);
874874
} else {
875875
scope.removeTag(key);
@@ -957,4 +957,8 @@ export namespace NATIVE {
957957
}
958958
});
959959
}
960+
961+
export async function crashedLastRun() {
962+
return io.sentry.Sentry.isCrashedLastRun();
963+
}
960964
}

src/sentry/wrapper.ios.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ export namespace NATIVE {
536536
return;
537537
}
538538
NSSentrySDK.configureScope((scope: SentryScope) => {
539-
if (value){
539+
if (value) {
540540
scope.setTagValueForKey(key, value);
541541
} else {
542542
scope.removeTagForKey(key);
@@ -691,4 +691,7 @@ export namespace NATIVE {
691691
}
692692
return res;
693693
}
694+
export async function crashedLastRun() {
695+
return SentrySDK.crashedLastRun;
696+
}
694697
}

0 commit comments

Comments
 (0)