a7ul / react-native-exception-handler

A react native module that lets you to register a global error handler that can capture fatal/non fatal uncaught exceptions.
MIT License
1.58k stars 133 forks source link

Android not sending fetch in setNativeExceptionHandler #155

Open smallzZz8 opened 2 years ago

smallzZz8 commented 2 years ago

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);
    }
});```
bombillazo commented 2 years ago

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.

W1nstar commented 2 years ago

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?

bombillazo commented 2 years ago

@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);
W1nstar commented 2 years ago

@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.

bombillazo commented 2 years ago

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.

W1nstar commented 2 years ago

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.