Permalink
Browse files

Add error customizer for React Native Android

Reviewed By: achen1

Differential Revision: D5954205

fbshipit-source-id: 8b92bf1484a037b333a6f0f4aeddd39ffee25361
  • Loading branch information...
axe-fb authored and facebook-github-bot committed Oct 4, 2017
1 parent 678a7f3 commit 0c234c90a2b831eb0f3e054de17fa26a4cee7934
@@ -21,6 +21,7 @@
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Pair;
import android.widget.Toast;
import com.facebook.common.logging.FLog;
import com.facebook.debug.holder.PrinterHolder;
@@ -44,6 +45,7 @@
import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener;
import com.facebook.react.devsupport.interfaces.DevOptionHandler;
import com.facebook.react.devsupport.interfaces.DevSupportManager;
import com.facebook.react.devsupport.interfaces.ErrorCustomizer;
import com.facebook.react.devsupport.interfaces.PackagerStatusCallback;
import com.facebook.react.devsupport.interfaces.StackFrame;
import com.facebook.react.modules.debug.interfaces.DeveloperSettings;
@@ -53,6 +55,7 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
@@ -134,6 +137,7 @@
private int mLastErrorCookie = 0;
private @Nullable ErrorType mLastErrorType;
private @Nullable DevBundleDownloadListener mBundleDownloadListener;
private @Nullable List<ErrorCustomizer> mErrorCustomizers;
private static class JscProfileTask extends AsyncTask<String, Void, Void> {
private static final MediaType JSON =
@@ -286,6 +290,29 @@ public void showNewJSError(String message, ReadableArray details, int errorCooki
showNewError(message, StackTraceHelper.convertJsStackTrace(details), errorCookie, ErrorType.JS);
}
@Override
public void registerErrorCustomizer(ErrorCustomizer errorCustomizer){
if (mErrorCustomizers == null){
mErrorCustomizers = new ArrayList<>();
}
mErrorCustomizers.add(errorCustomizer);
}
private Pair<String, StackFrame[]> processErrorCustomizers(
Pair<String, StackFrame[]> errorInfo) {
if (mErrorCustomizers == null) {
return errorInfo;
} else {
for (ErrorCustomizer errorCustomizer : mErrorCustomizers) {
Pair<String, StackFrame[]> result = errorCustomizer.customizeErrorInfo(errorInfo);
if (result != null) {
errorInfo = result;
}
}
return errorInfo;
}
}
@Override
public void updateJSError(
final String message,
@@ -304,7 +331,9 @@ public void run() {
return;
}
StackFrame[] stack = StackTraceHelper.convertJsStackTrace(details);
mRedBoxDialog.setExceptionDetails(message, stack);
Pair<String, StackFrame[]> errorInfo =
processErrorCustomizers(Pair.create(message, stack));
mRedBoxDialog.setExceptionDetails(errorInfo.first, errorInfo.second);
updateLastErrorInfo(message, stack, errorCookie, ErrorType.JS);
// JS errors are reported here after source mapping.
if (mRedBoxHandler != null) {
@@ -342,7 +371,8 @@ public void run() {
// show the first and most actionable one.
return;
}
mRedBoxDialog.setExceptionDetails(message, stack);
Pair<String, StackFrame[]> errorInfo = processErrorCustomizers(Pair.create(message, stack));
mRedBoxDialog.setExceptionDetails(errorInfo.first, errorInfo.second);
updateLastErrorInfo(message, stack, errorCookie, errorType);
// Only report native errors here. JS errors are reported
// inside {@link #updateJSError} after source mapping.
@@ -9,6 +9,7 @@
package com.facebook.react.devsupport;
import com.facebook.react.devsupport.interfaces.ErrorCustomizer;
import javax.annotation.Nullable;
import java.io.File;
@@ -161,6 +162,11 @@ public void isPackagerRunning(PackagerStatusCallback callback) {
return null;
}
@Override
public void registerErrorCustomizer(ErrorCustomizer errorCustomizer) {
}
@Override
public void handleException(Exception e) {
mDefaultNativeModuleCallExceptionHandler.handleException(e);
@@ -52,4 +52,5 @@
final File outputFile);
@Nullable String getLastErrorTitle();
@Nullable StackFrame[] getLastErrorStack();
void registerErrorCustomizer(ErrorCustomizer errorCustomizer);
}
@@ -0,0 +1,22 @@
/**
* Copyright (c) 2015-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.react.devsupport.interfaces;
import android.util.Pair;
/**
* Interface that lets parts of the app process the errors before showing the redbox
*/
public interface ErrorCustomizer {
/**
* The function that need to be registered using {@link DevSupportManager}.registerErrorCustomizer
* and is called before passing the error to the RedBox.
*/
Pair<String, StackFrame[]> customizeErrorInfo(Pair<String, StackFrame[]> errorInfo);
}

0 comments on commit 0c234c9

Please sign in to comment.