react-native-global-exception-handler

website·Indexed 30 days ago

https://geh.darshan09200.dev/

A React Native library for JavaScript and native exception handling, providing graceful fallback flows and crash reporting.

Tokens
656
Snippets
3
Records
7
Agent score
12%

What's inside react-native-global-exception-handler

  1. Overview of react-native-global-exception-handler

    react-native-global-exception-handler is a React Native library used to register global handlers for JavaScript and native exceptions. It allows developers to implement graceful fallback UIs, recovery flows, and integrate crash-reporting services like Sentry or Crashlytics. Key capabilities include preserving existing native handlers and simulating native crashes for release-mode validation.
  2. Configure native exception handler options

    When registering a native exception handler, the following options can be used:

    • forceAppToQuit (default: true): Primarily affects Android; controls whether the app is forced to quit during recovery attempts.
    • callPreviouslyDefinedHandler (default: false): Set to true if another SDK (e.g., Sentry, Crashlytics) has already installed a native handler and you wish to preserve it.
  3. Register a global JavaScript exception handler

    Use setJSExceptionHandler to register a handler for uncaught JavaScript exceptions that occur before the app exits. The allowedInDevMode parameter determines if the handler should trigger during development.
    setJSExceptionHandler((error) => {
      // Handle JS error
    }, true); // allowedInDevMode = true
  4. Retrieve currently registered exception handlers

    The library provides getter functions to inspect or preserve existing handlers, which is useful for chaining handlers:

    • getJSExceptionHandler(): Returns the currently registered JavaScript exception handler.
    • getNativeExceptionHandler(): Returns the currently registered native exception handler.
  5. Register a global native exception handler

    Use setNativeExceptionHandler to register a handler for native crashes. It is recommended to use the options-object signature. Native crash behavior should be validated specifically in release builds as debug behavior is not a reliable substitute.
    setNativeExceptionHandler((error) => {
      // Handle native crash
    }, { 
      forceAppToQuit: true, 
      callPreviouslyDefinedHandler: false 
    });
  6. Simulate a native crash for validation

    The simulateNativeCrash(crashType) function allows developers to trigger a native crash to verify that crash-reporting integrations and handlers work correctly in release builds. This should only be used in controlled test environments and never exposed to end-users.
    simulateNativeCrash('crashType');
  7. Native crash handling platform constraints

    Developers should be aware of the following platform-specific behaviors:

    • General: JavaScript exceptions and native crashes are not interchangeable. Release builds are the only reliable way to validate native crash behavior.
    • iOS: Native crash recovery is limited. Do not assume a standard React Native UI flow is safe after a fatal native crash.
    • Android: Offers more flexibility for restart-oriented recovery flows; the forceAppToQuit option specifically modifies this behavior.