react-native-exception-handler icon indicating copy to clipboard operation
react-native-exception-handler copied to clipboard

Android not sending fetch in setNativeExceptionHandler

Open smallzZz8 opened this issue 3 years ago • 6 comments

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);
	}
});```

smallzZz8 avatar Oct 20 '21 21:10 smallzZz8

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.

bombillazo avatar Dec 20 '21 06:12 bombillazo

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 avatar Aug 03 '22 09:08 W1nstar

@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 avatar Aug 04 '22 12:08 bombillazo

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

W1nstar avatar Aug 05 '22 09:08 W1nstar

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.

bombillazo avatar Aug 05 '22 16:08 bombillazo

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.

W1nstar avatar Aug 08 '22 08:08 W1nstar