Global (Native) Error Catcher for Java (React Native)


#1

I am building a React Native Application, and am attempting to build a completely native error handler as a final fallback in case of crashes at the native level, which bypass javascript entirely.

I have created a global error logger by adapting this blog post:

http://sterl.org/2016/02/android-global-exception-handler/

package com.myPackage;

import io.sentry.Sentry;
import android.content.Context;
import android.util.Log;

public class MyExceptionHandler implements Thread.UncaughtExceptionHandler {

    private Context context;
    private Thread.UncaughtExceptionHandler rootHandler;

    public FlockExceptionHandler(Context context) {
        this.context = context;
        this.rootHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);
    }

    @Override
    public void uncaughtException(final Thread thread, final Throwable ex) {
        Log.e("Alert","in handler");
        Sentry.capture(ex);
        if (this.rootHandler != null) {
            this.rootHandler.uncaughtException(thread, ex);
        } else {
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
        }
    }
}

Which gets consumed in both MainActivity.java:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Alert","onCreateActivity");
Log.e("Alert", String.valueOf(BuildConfig.PRODUCTION_RELEASE));
if (BuildConfig.PRODUCTION_RELEASE) {
  Context ctx = this.getApplicationContext();

  String sentryDsn = BuildConfig.SENTRY_DSN;
  Sentry.init(sentryDsn, new AndroidSentryClientFactory(ctx));

  Log.e("Alert","instantiation happens next");
  new FlockExceptionHandler(MainActivity.this);

  }
  }

And also MainApplication:

@Override
  public void onCreate() {
    super.onCreate();
    new FlockExceptionHandler(this);
    AppEventsLogger.activateApp(this);
  };

This works, if I add a

int x = 2/0;

in the MainApplication,

The custom error handler is triggered (as evidenced by logging. The SENTRY_DSN is logged, correctly). However sentry receives no event. Any help would be appreciated!