-
Notifications
You must be signed in to change notification settings - Fork 24.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[JS] JS API for global handling of unhandled exceptions #1194
Comments
@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? |
This would be great for adding something like Bugsnag to report exceptions when apps are in production. Edit: In fact, I think |
We're looking to log something anytime there is an error, so we can catch bugs early. This would be very useful. |
Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed. |
would be definitely useful!:+1: |
@ide, what is the current recommended approach (or hack) for custom handling of global errors? |
You could either hook into the native layer or monkeypatch the JS ExceptionsManager. |
Is it a bad idea to set my own global error handler? My scenario was to
|
Hi @ajwhite. Any luck completing the 2 points you mentioned? |
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 |
In production mode I call |
@ajwhite Cool. How do you reload the bridge? Would really appreciate it if you could share a gist or something. |
@ajwhite And you have a DEV check I suppose? |
Exactly @qbig Basically if (!__DEV__) {
ErrorUtils.setGlobalHandler(error => {
sentry.capture(error);
NativeModules.BridgeReloader.reload()
});
} Then your Or in an Android module, you just get the running activity, finish it, and restart it @ReactMethod
public void reload() {
Activity activity = getCurrentActivity();
Intent intent = activity.getIntent();
activity.finish();
activity.startActivity(intent);
} |
That's neat:) @ajwhite Thanks so much! ErrorUtils.setGlobalHandler(Raven.captureException.bind(Raven)); But I don't like the fact that it is overwriting the default exception handler. So I was considering something like var defaultHandler = ErrorUtils._globalHandler;
ErrorUtils._globalHandler = function(...args){
defaultHandler(...args);
Raven.captureException(...args);
// other custom handler
}; |
Just took a read through the latest Raven source. The version I had used months back would report exceptions by loading an image.. That obviously doesn't work well for us here in React Native land. Their latest version looks like it has full support now though!
That would work. I just re-read the source for the native error handler. It looks like it does two things:
I had no idea until now that it reloads the bridge by default. I don't know if this is a new development or not, but that's pretty cool. Unfortunately the application I work on is basically a "kiosk mode" type of thing running 24/7. I notice the handler has a So keeping a reference to the original handler and calling it is probably a good thing in your example |
I think |
@ide like a getter for "_globalHandler" ? |
Yes, making it part of the public API. |
var ErrorUtils = {
_inGuard: 0,
_globalHandler: null,
getGlobalHandler : function() {
return ErrorUtils._globalHandler;
},
setGlobalHandler: function(fun) {
ErrorUtils._globalHandler = fun;
},
reportError: function(error) {
ErrorUtils._globalHandler && ErrorUtils._globalHandler(error);
},
reportFatalError: function(error) {
ErrorUtils._globalHandler && ErrorUtils._globalHandler(error, true);
},
// ... Would do ? I'd be more than happy to PR |
@qbig Looks reasonable. |
Summary:As discussed here #1194 . This add an getter the default handler, so that custom handler can be added (monkey patch?) without overwriting the default handler Closes #5575 Differential Revision: D2948994 fb-gh-sync-id: 2b6d1619cfe68f78b326c6d232b9bf57c489c45d shipit-source-id: 2b6d1619cfe68f78b326c6d232b9bf57c489c45d
Summary:As discussed here facebook#1194 . This add an getter the default handler, so that custom handler can be added (monkey patch?) without overwriting the default handler Closes facebook#5575 Differential Revision: D2948994 fb-gh-sync-id: 2b6d1619cfe68f78b326c6d232b9bf57c489c45d shipit-source-id: 2b6d1619cfe68f78b326c6d232b9bf57c489c45d
This issue appears to have been solved by #5575. Closing this issue out for now. |
The 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) |
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?
I then periodically clear the array and send the data to some reporting system. |
@jlo1 Did you ever find a solution for non-debug mode? |
I'm pretty sure that |
What about iOS? Is there a platform-agnostic way to do this? |
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. |
|
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.The text was updated successfully, but these errors were encountered: