react-native-exception-handler
react-native-exception-handler copied to clipboard
Android not sending fetch in setNativeExceptionHandler
I am trying to send an error to a webhook on native crashes. Everything works fine on iOS but does not work on android. Does anyone have a solution?
I have confirmed that the setNativeExceptionHandler is running for android since it does print the console log. Any Idea?
setNativeExceptionHandler((errorString) => {
// You can do something like call an api to report to dev team here
console.log('setNativeExceptionHandler');
try {
const myHeaders = new Headers();
myHeaders.append('Content-Type', 'application/json');
const raw = `{"text": "Type: 'setNativeExceptionHandler',\nPlatform: ${
Platform.OS
},\nIs Fatal: Yes,\nCan OTA?: No\nError Message: ${errorString.toString().split('"').join("'")}\n-----\n\n"}`;
const requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw
};
fetch('THEURL', requestOptions)
.then((response) => response.text())
.then((result) => console.log(result))
.catch((error) => console.log('error', error));
} catch (error) {
console.log(error);
}
});```
I have the same issue, Im trying to send Crashlytics info in the native error handler on the Android app but nothing inside this function runs.
Still happening on 2022. Method gets executed in a js thread, logs are shown in logcat but a simple does not work. It doesn't even throw an error to catch, so I'm out of ideas. Of cource, Sentry isn't working either.
@bombillazo did you find any workaround?
@W1nstar I think it did, I implemented it like this:
const nativeExceptionhandler: NativeExceptionHandler = (error) => {
console.debug('NATIVE EXCEPTION!');
crashlytics().recordError(new Error(error));
};
setNativeExceptionHandler(nativeExceptionhandler, false, false);
@bombillazo thanks a ton for the input!
I'm unfamiliar with crashlytics, but I guess it writes a file with the exception, for a later use? By my own findings, it seems like theres no way to make an http request of any kind inside nativeExceptionHandler.
Verify your Android HTTP setup, I believe by default some modern android versions block http
by default, I'm not an expert in Crashlytics either but so far we do get events streamed to firebase.
Yeah, I did, everything works correctly. Tested it on Androids 5 to 9, emulators and phyiscal terminals, same issue. Guess I'll have to store the event and somehow send it later.