Skip to content

[JS] JS API for global handling of unhandled exceptions #1194

Closed

Description

@ide

The API I envision is similar to window.onerror but not necessarily the same. This would allow us to make RCTExceptionsManager.reportUnhandledException a lot smaller (we'd move much of the code into JS to keep the current behavior) and make it easier to do custom error handling.

Activity

  1. changed the title [-]JS API for global handling of unhandled exceptions[/-] [+][JS] JS API for global handling of unhandled exceptions[/+] on May 30, 2015
  2. d-vine commented on Jun 11, 2015

    @d-vine

    @ide Hi. I would very much appreciate such an API. In the mean time is there a way to prevent an production app from crashing on any random JavaScript exception?

  3. JoeStanton commented on Jul 20, 2015

    @JoeStanton
    Contributor

    This would be great for adding something like Bugsnag to report exceptions when apps are in production.

    Edit: In fact, I think ErrorUtils.setGlobalHandler((err, isFatal) => { ... }); might do the job here.

  4. richardgill commented on Jul 30, 2015

    @richardgill

    We're looking to log something anytime there is an error, so we can catch bugs early. This would be very useful.

  5. despairblue commented on Sep 1, 2015

    @despairblue
    Contributor

    would be definitely useful!:+1:

  6. sjmueller commented on Sep 24, 2015

    @sjmueller
    Contributor

    @ide, what is the current recommended approach (or hack) for custom handling of global errors?

  7. ide commented on Sep 24, 2015

    @ide
    ContributorAuthor

    You could either hook into the native layer or monkeypatch the JS ExceptionsManager.

  8. atticoos commented on Dec 15, 2015

    @atticoos
    Contributor

    Is it a bad idea to set my own global error handler?

    My scenario was to

    • Report exception to Sentry
    • Possibly reload the bridge to avoid being stuck in the error state
  9. qbig commented on Jan 26, 2016

    @qbig
    Contributor

    Hi @ajwhite. Any luck completing the 2 points you mentioned?

  10. qbig commented on Jan 27, 2016

    @qbig
    Contributor

    Here: https://github.com/facebook/react-native/blob/d33b554f5d4bb7856cf2fc21175bab23d8225fe4/packager/react-packager/src/Resolver/polyfills/error-guard.js

    var ErrorUtils = {
      _inGuard: 0,
      _globalHandler: null,
      setGlobalHandler: function(fun) {
        ErrorUtils._globalHandler = fun;
      },
    //...

    'setGlobalHandler' would overwrite a lot of the RN behaviour (eg. RedBox in development). Understanding that we could just monkey patch globalHandler, the "" seems alarming. So maybe an API like addGlobalHandler, so that we could add custom handler without breaking the default behaviour and and without messing with "internal" variables ? @ide @satya164

  11. atticoos commented on Jan 27, 2016

    @atticoos
    Contributor

    In production mode I call setGlobalHandler with my own handler, which does the two things listed above -- logging to sentry & reloading the bridge. Seems to work just fine.

  12. qbig commented on Jan 27, 2016

    @qbig
    Contributor

    @ajwhite Cool. How do you reload the bridge? Would really appreciate it if you could share a gist or something.

  13. qbig commented on Jan 27, 2016

    @qbig
    Contributor

    @ajwhite And you have a DEV check I suppose?

  14. 9 remaining items

  15. ghost added a commit that references this issue on Feb 18, 2016
  16. jsierles commented on Mar 20, 2016

    @jsierles
    Contributor

    This issue appears to have been solved by #5575. Closing this issue out for now.

  17. jlo1 commented on Apr 12, 2016

    @jlo1

    The ErrorUtils.setGlobalHandler works great when the app's process is attached to the debugger (regardless if in debug / release bundle): I see a red screen with the error message, and my own custom handler is called fine.

    But how do I handle errors for production apps, where a debugger won't be attached? When a debugger isn't attached and a crash occurs, the app doesn't show a red screen and just closes the app and goes to the home screen. In this case, my custom handler isn't called at all.

    (Specifically describing the flow for iOS bundled app here)

  18. npomfret commented on Jun 3, 2016

    @npomfret
    Contributor

    Hi, I'm fairly new to both RN and javascript. I've put the following into my code... It catches errors and warnings in an array before reporting them as normal to the console.

    It sits at the top of my main.js file. It seems to work, but is this a terrible idea?

    const orig_console_warn = console.warn;
    const orig_console_error = console.error;
    
    let errors = [];
    let warnings = [];
    
    console.warn = function() {
      warnings.push(arguments);
      orig_console_warn.apply(console, arguments);
    };
    
    console.error = function() {
      errors.push(arguments);
      orig_console_error.apply(console, arguments);
    };
    
    

    I then periodically clear the array and send the data to some reporting system.

  19. GeoffreyPlitt commented on Aug 26, 2016

    @GeoffreyPlitt

    @jlo1 Did you ever find a solution for non-debug mode?

  20. antoinerousseau commented on Aug 27, 2016

    @antoinerousseau
    Contributor

    I'm pretty sure that global.ErrorUtils.setGlobalHandler works in production mode too on Android, fyi.

  21. GeoffreyPlitt commented on Aug 28, 2016

    @GeoffreyPlitt

    What about iOS? Is there a platform-agnostic way to do this?

  22. npomfret commented on Aug 29, 2016

    @npomfret
    Contributor

    I ended up trapping console.log, console.info, console.warn and console.error and writing the data to an in-memory buffer that I can view in my app. I also send error and warn messages to a server that records the data so I can see error from my clients in one central place.

  23. nsyujian commented on May 31, 2018

    @nsyujian
    iOS
      RCTSetFatalHandler(^(NSError *error) {
        [Bugly reportError:error];
        
         [rootView.bridge reload];
      });
    
  24. locked as resolved and limited conversation to collaborators on Jul 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions