Add error customizer for React Native Android

Reviewed By: achen1

Differential Revision: D5954205

fbshipit-source-id: 8b92bf1484a037b333a6f0f4aeddd39ffee25361
This commit is contained in:
Ram N 2017-10-04 13:48:29 -07:00 коммит произвёл Facebook Github Bot
Родитель 678a7f3c39
Коммит 0c234c90a2
4 изменённых файлов: 61 добавлений и 2 удалений

Просмотреть файл

@ -21,6 +21,7 @@ import android.content.pm.PackageManager;
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.DevServerHelper.PackagerCommandListener;
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.File;
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 @@ public class DevSupportManagerImpl implements
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 class DevSupportManagerImpl implements
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 class DevSupportManagerImpl implements
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 class DevSupportManagerImpl implements
// 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 class DisabledDevSupportManager implements DevSupportManager {
return null;
}
@Override
public void registerErrorCustomizer(ErrorCustomizer errorCustomizer) {
}
@Override
public void handleException(Exception e) {
mDefaultNativeModuleCallExceptionHandler.handleException(e);

Просмотреть файл

@ -52,4 +52,5 @@ public interface DevSupportManager extends NativeModuleCallExceptionHandler {
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);
}