Skip to content

Commit

Permalink
Implement crashedLastRun
Browse files Browse the repository at this point in the history
  • Loading branch information
janicduplessis committed Aug 9, 2024
1 parent d84c19e commit 4f3990c
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 0 deletions.
5 changes: 5 additions & 0 deletions android/src/main/java/io/sentry/react/RNSentryModuleImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,11 @@ public String fetchNativePackageName() {
return packageInfo.packageName;
}

public void crashedLastRun(Promise promise) {
Boolean crashedLastRun = Sentry.isCrashedLastRun();
promise.resolve(crashedLastRun != null ? crashedLastRun : false);
}

private void setEventOriginTag(SentryEvent event) {
SdkVersion sdk = event.getSdk();
if (sdk != null) {
Expand Down
5 changes: 5 additions & 0 deletions android/src/newarch/java/io/sentry/react/RNSentryModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,9 @@ public void captureReplay(boolean isHardCrash, Promise promise) {
public String getCurrentReplayId() {
return this.impl.getCurrentReplayId();
}

@Override
public void crashedLastRun(Promise promise) {
this.impl.crashedLastRun(promise);
}
}
5 changes: 5 additions & 0 deletions android/src/oldarch/java/io/sentry/react/RNSentryModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,9 @@ public void captureReplay(boolean isHardCrash, Promise promise) {
public String getCurrentReplayId() {
return this.impl.getCurrentReplayId();
}

@ReactMethod
public void crashedLastRun(Promise promise) {
this.impl.crashedLastRun(promise);
}
}
6 changes: 6 additions & 0 deletions ios/RNSentry.mm
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,12 @@ - (NSDictionary*) fetchNativeStackFramesBy: (NSArray<NSNumber*>*)instructionsAdd
#endif
}

RCT_EXPORT_METHOD(crashedLastRun:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
resolve(@([SentrySDK crashedLastRun]));
}

// Thanks to this guard, we won't compile this code when we build for the old architecture.
#ifdef RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
Expand Down
1 change: 1 addition & 0 deletions src/js/NativeRNSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export interface Spec extends TurboModule {
initNativeReactNavigationNewFrameTracking(): Promise<void>;
captureReplay(isHardCrash: boolean): Promise<string | undefined | null>;
getCurrentReplayId(): string | undefined | null;
crashedLastRun(): Promise<boolean>;
}

export type NativeStackFrame = {
Expand Down
7 changes: 7 additions & 0 deletions src/js/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ export class ReactNativeClient extends BaseClient<ReactNativeClientOptions> {
this._initNativeSdk();
}

/**
* Returns if the app crashed in the last run.
*/
public crashedLastRun(): Promise<boolean> {
return NATIVE.crashedLastRun();
}

/**
* Sets up the integrations
*/
Expand Down
1 change: 1 addition & 0 deletions src/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export {
captureUserFeedback,
withScope,
configureScope,
crashedLastRun,
} from './sdk';
export { TouchEventBoundary, withTouchEventBoundary } from './touchevents';

Expand Down
11 changes: 11 additions & 0 deletions src/js/sdk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,14 @@ export function configureScope(callback: (scope: Scope) => void): ReturnType<Hub
};
getCurrentHub().configureScope(safeCallback);
}

/**
* Returns if the app crashed in the last run.
*/
export async function crashedLastRun(): Promise<boolean> {
const client = getCurrentHub().getClient<ReactNativeClient>();
if (client) {
return client.crashedLastRun();
}
return false;
}
13 changes: 13 additions & 0 deletions src/js/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ interface SentryNativeWrapper {

captureReplay(isHardCrash: boolean): Promise<string | null>;
getCurrentReplayId(): string | null;

crashedLastRun(): Promise<boolean>;
}

const EOL = utf8ToBytes('\n');
Expand Down Expand Up @@ -642,6 +644,17 @@ export const NATIVE: SentryNativeWrapper = {
return RNSentry.getCurrentReplayId() || null;
},

async crashedLastRun(): Promise<boolean> {
if (!this.enableNative) {
return false;
}
if (!this._isModuleLoaded(RNSentry)) {
return false;
}

return RNSentry.crashedLastRun();
},

/**
* Gets the event from envelopeItem and applies the level filter to the selected event.
* @param data An envelope item containing the event.
Expand Down

0 comments on commit 4f3990c

Please sign in to comment.