Skip to content
This repository has been archived by the owner on May 6, 2022. It is now read-only.

ExceptionHandler to catch known GPRC bug while we wait for an upstream fix #44

Merged
merged 3 commits into from
Mar 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions android/src/main/java/com/pylon/RNSpokestack/ExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.pylon.RNSpokestack;

import android.content.Context;
import android.util.Log;

/* Our very own exception handler for all threads with us as the parent process.
* https://github.com/googleapis/google-cloud-java/issues/4727 necessitates this.
* Hence we silently swallow the specific exception, and let the default exception handler
* do its thing otherwise.
*/
public class ExceptionHandler implements Thread.UncaughtExceptionHandler {

private Context context;
private Thread.UncaughtExceptionHandler rootHandler;

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

/* The specific exception to ignore stems from grpc-okhttp-1, which is present in google-cloud-speech 0.84.0-beta.
* https://github.com/grpc/grpc-java/blob/master/okhttp/src/main/java/io/grpc/okhttp/OkHttpClientTransport.java#L933
*/
@Override
public void uncaughtException(final Thread thread, final Throwable ex) {
if (this.rootHandler != null && ex instanceof java.util.concurrent.RejectedExecutionException) {
Log.i("react-native-spokestack","ignoring known exception: " + ex.toString());
} else if (this.rootHandler != null) {
this.rootHandler.uncaughtException(thread, ex);
} else {
Log.e("react-native-spokestack","no root handlerfor uncaught exception, killing process and exiting.");
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
}
}