Skip to content

Commit

Permalink
Rename ParsedError to ProcessedError (#48356)
Browse files Browse the repository at this point in the history
Summary:

I think parsed isn't a good enough name.

React native also does a lot of processing of the error.

This also opens the door for eventually forwarding the original error in the future.

Changelog: [Internal]

Reviewed By: alanleedev

Differential Revision: D67526700
  • Loading branch information
RSNara authored and facebook-github-bot committed Dec 20, 2024
1 parent 4c1dd90 commit ca1d517
Show file tree
Hide file tree
Showing 16 changed files with 337 additions and 327 deletions.
6 changes: 3 additions & 3 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -2364,7 +2364,7 @@ public class com/facebook/react/devsupport/StackTraceHelper {
public static fun convertJsStackTrace (Lcom/facebook/react/bridge/ReadableArray;)[Lcom/facebook/react/devsupport/interfaces/StackFrame;
public static fun convertJsStackTrace (Ljava/lang/String;)[Lcom/facebook/react/devsupport/interfaces/StackFrame;
public static fun convertJsStackTrace (Lorg/json/JSONArray;)[Lcom/facebook/react/devsupport/interfaces/StackFrame;
public static fun convertParsedError (Lcom/facebook/react/interfaces/exceptionmanager/ReactJsExceptionHandler$ParsedError;)Lcom/facebook/react/bridge/JavaOnlyMap;
public static fun convertProcessedError (Lcom/facebook/react/interfaces/exceptionmanager/ReactJsExceptionHandler$ProcessedError;)Lcom/facebook/react/bridge/JavaOnlyMap;
public static fun formatFrameSource (Lcom/facebook/react/devsupport/interfaces/StackFrame;)Ljava/lang/String;
public static fun formatStackTrace (Ljava/lang/String;[Lcom/facebook/react/devsupport/interfaces/StackFrame;)Ljava/lang/String;
}
Expand Down Expand Up @@ -2816,7 +2816,7 @@ public abstract interface class com/facebook/react/interfaces/TaskInterface {
public abstract fun waitForCompletion (JLjava/util/concurrent/TimeUnit;)Z
}

public abstract interface class com/facebook/react/interfaces/exceptionmanager/ReactJsExceptionHandler$ParsedError {
public abstract interface class com/facebook/react/interfaces/exceptionmanager/ReactJsExceptionHandler$ProcessedError {
public abstract fun getComponentStack ()Ljava/lang/String;
public abstract fun getExtraData ()Lcom/facebook/react/bridge/ReadableMap;
public abstract fun getId ()I
Expand All @@ -2827,7 +2827,7 @@ public abstract interface class com/facebook/react/interfaces/exceptionmanager/R
public abstract fun isFatal ()Z
}

public abstract interface class com/facebook/react/interfaces/exceptionmanager/ReactJsExceptionHandler$ParsedError$StackFrame {
public abstract interface class com/facebook/react/interfaces/exceptionmanager/ReactJsExceptionHandler$ProcessedError$StackFrame {
public abstract fun getColumn ()Ljava/lang/Integer;
public abstract fun getFile ()Ljava/lang/String;
public abstract fun getLineNumber ()Ljava/lang/Integer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import com.facebook.react.bridge.ReadableType;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.devsupport.interfaces.StackFrame;
import com.facebook.react.interfaces.exceptionmanager.ReactJsExceptionHandler.ParsedError;
import com.facebook.react.interfaces.exceptionmanager.ReactJsExceptionHandler.ProcessedError;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -263,10 +263,10 @@ public static String formatStackTrace(String title, StackFrame[] stack) {
return stackTrace.toString();
}

public static JavaOnlyMap convertParsedError(ParsedError error) {
List<ParsedError.StackFrame> frames = error.getStack();
public static JavaOnlyMap convertProcessedError(ProcessedError error) {
List<ProcessedError.StackFrame> frames = error.getStack();
List<ReadableMap> readableMapList = new ArrayList<>();
for (ParsedError.StackFrame frame : frames) {
for (ProcessedError.StackFrame frame : frames) {
JavaOnlyMap map = new JavaOnlyMap();
map.putDouble(COLUMN_KEY, frame.getColumn());
map.putDouble(LINE_NUMBER_KEY, frame.getLineNumber());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import java.util.ArrayList
@UnstableReactNativeAPI
public fun interface ReactJsExceptionHandler {
@DoNotStripAny
public interface ParsedError {
public interface ProcessedError {
@DoNotStripAny
public interface StackFrame {
public val file: String?
Expand All @@ -37,24 +37,24 @@ public fun interface ReactJsExceptionHandler {
}

@DoNotStripAny
private data class ParsedStackFrameImpl(
private data class ProcessedErrorStackFrameImpl(
override val file: String?,
override val methodName: String,
override val lineNumber: Int?,
override val column: Int?,
) : ParsedError.StackFrame
) : ProcessedError.StackFrame

@DoNotStripAny
private data class ParsedErrorImpl(
private data class ProcessedErrorImpl(
override val message: String,
override val originalMessage: String?,
override val name: String?,
override val componentStack: String?,
override val stack: ArrayList<ParsedStackFrameImpl>,
override val stack: ArrayList<ProcessedErrorStackFrameImpl>,
override val id: Int,
override val isFatal: Boolean,
override val extraData: ReadableNativeMap,
) : ParsedError
) : ProcessedError

public fun reportJsException(errorMap: ParsedError)
public fun reportJsException(errorMap: ProcessedError)
}
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@ private class ReactJsExceptionHandlerImpl implements ReactJsExceptionHandler {
}

@Override
public void reportJsException(ParsedError error) {
JavaOnlyMap data = StackTraceHelper.convertParsedError(error);
public void reportJsException(ProcessedError error) {
JavaOnlyMap data = StackTraceHelper.convertProcessedError(error);

try {
NativeExceptionsManagerSpec exceptionsManager =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@
namespace facebook::react {

namespace {
class ParsedError : public facebook::jni::JavaClass<ParsedError> {
class ProcessedError : public facebook::jni::JavaClass<ProcessedError> {
public:
static auto constexpr kJavaDescriptor =
"Lcom/facebook/react/interfaces/exceptionmanager/ReactJsExceptionHandler$ParsedError;";
"Lcom/facebook/react/interfaces/exceptionmanager/ReactJsExceptionHandler$ProcessedError;";
};

class ParsedStackFrameImpl
: public facebook::jni::JavaClass<ParsedStackFrameImpl> {
class ProcessedErrorStackFrameImpl
: public facebook::jni::JavaClass<ProcessedErrorStackFrameImpl> {
public:
static auto constexpr kJavaDescriptor =
"Lcom/facebook/react/interfaces/exceptionmanager/ReactJsExceptionHandler$ParsedStackFrameImpl;";
"Lcom/facebook/react/interfaces/exceptionmanager/ReactJsExceptionHandler$ProcessedErrorStackFrameImpl;";

static facebook::jni::local_ref<ParsedStackFrameImpl> create(
const JsErrorHandler::ParsedError::StackFrame& frame) {
static facebook::jni::local_ref<ProcessedErrorStackFrameImpl> create(
const JsErrorHandler::ProcessedError::StackFrame& frame) {
return newInstance(
frame.file ? jni::make_jstring(*frame.file) : nullptr,
frame.methodName,
Expand All @@ -38,18 +38,19 @@ class ParsedStackFrameImpl
}
};

class ParsedErrorImpl
: public facebook::jni::JavaClass<ParsedErrorImpl, ParsedError> {
class ProcessedErrorImpl
: public facebook::jni::JavaClass<ProcessedErrorImpl, ProcessedError> {
public:
static auto constexpr kJavaDescriptor =
"Lcom/facebook/react/interfaces/exceptionmanager/ReactJsExceptionHandler$ParsedErrorImpl;";
"Lcom/facebook/react/interfaces/exceptionmanager/ReactJsExceptionHandler$ProcessedErrorImpl;";

static facebook::jni::local_ref<ParsedErrorImpl> create(
static facebook::jni::local_ref<ProcessedErrorImpl> create(
jsi::Runtime& runtime,
const JsErrorHandler::ParsedError& error) {
auto stack = facebook::jni::JArrayList<ParsedStackFrameImpl>::create();
const JsErrorHandler::ProcessedError& error) {
auto stack =
facebook::jni::JArrayList<ProcessedErrorStackFrameImpl>::create();
for (const auto& frame : error.stack) {
stack->add(ParsedStackFrameImpl::create(frame));
stack->add(ProcessedErrorStackFrameImpl::create(frame));
}

auto extraDataDynamic =
Expand All @@ -75,12 +76,12 @@ class ParsedErrorImpl

void JReactExceptionManager::reportJsException(
jsi::Runtime& runtime,
const JsErrorHandler::ParsedError& error) {
const JsErrorHandler::ProcessedError& error) {
static const auto method =
javaClassStatic()->getMethod<void(jni::alias_ref<ParsedError>)>(
javaClassStatic()->getMethod<void(jni::alias_ref<ProcessedError>)>(
"reportJsException");
if (self() != nullptr) {
method(self(), ParsedErrorImpl::create(runtime, error));
method(self(), ProcessedErrorImpl::create(runtime, error));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class JReactExceptionManager

void reportJsException(
jsi::Runtime& runtime,
const JsErrorHandler::ParsedError& error);
const JsErrorHandler::ProcessedError& error);
};

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ JReactInstance::JReactInstance(
auto onJsError =
[weakJReactExceptionManager = jni::make_weak(jReactExceptionManager)](
jsi::Runtime& runtime,
const JsErrorHandler::ParsedError& error) mutable noexcept {
const JsErrorHandler::ProcessedError& error) mutable noexcept {
if (auto jReactExceptionManager =
weakJReactExceptionManager.lockLocal()) {
jReactExceptionManager->reportJsException(runtime, error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ class StackTraceHelperTest {
}

@Test
fun testConvertParsedError() {
val error = getParsedErrorTestData()
fun testConvertProcessedError() {
val error = getProcessedErrorTestData()

val data = StackTraceHelper.convertParsedError(error)
val data = StackTraceHelper.convertProcessedError(error)
assertThat(data.getString("message")).isEqualTo("error message")
assertThat(data.getInt("id")).isEqualTo(123)
assertThat(data.getBoolean("isFatal")).isEqualTo(true)
Expand Down Expand Up @@ -96,17 +96,17 @@ class StackTraceHelperTest {
assertThat(map.getDouble("column").toInt()).isEqualTo(columnNumber)
}

private fun getParsedErrorTestData(): ParsedError {
private fun getProcessedErrorTestData(): ProcessedError {
val frame1 =
object : ParsedError.StackFrame {
object : ProcessedError.StackFrame {
override val file = "file1"
override val methodName = "method1"
override val lineNumber = 1
override val column = 10
}

val frame2 =
object : ParsedError.StackFrame {
object : ProcessedError.StackFrame {
override val file = "file2"
override val methodName = "method2"
override val lineNumber = 2
Expand All @@ -115,7 +115,7 @@ class StackTraceHelperTest {

val frames = listOf(frame1, frame2)

return object : ParsedError {
return object : ProcessedError {
override val message = "error message"
override val originalMessage = null
override val name = null
Expand Down
22 changes: 11 additions & 11 deletions packages/react-native/ReactCommon/jserrorhandler/JsErrorHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ jsi::Value getBundleMetadata(jsi::Runtime& runtime, jsi::JSError& error) {
namespace facebook::react {

template <>
struct Bridging<JsErrorHandler::ParsedError::StackFrame> {
struct Bridging<JsErrorHandler::ProcessedError::StackFrame> {
static jsi::Value toJs(
jsi::Runtime& runtime,
const JsErrorHandler::ParsedError::StackFrame& frame) {
const JsErrorHandler::ProcessedError::StackFrame& frame) {
auto stackFrame = jsi::Object(runtime);
auto file = bridging::toJs(runtime, frame.file, nullptr);
auto lineNumber = bridging::toJs(runtime, frame.lineNumber, nullptr);
Expand All @@ -142,10 +142,10 @@ struct Bridging<JsErrorHandler::ParsedError::StackFrame> {
};

template <>
struct Bridging<JsErrorHandler::ParsedError> {
struct Bridging<JsErrorHandler::ProcessedError> {
static jsi::Value toJs(
jsi::Runtime& runtime,
const JsErrorHandler::ParsedError& error) {
const JsErrorHandler::ProcessedError& error) {
auto data = jsi::Object(runtime);
data.setProperty(runtime, "message", error.message);
data.setProperty(
Expand Down Expand Up @@ -175,7 +175,7 @@ struct Bridging<JsErrorHandler::ParsedError> {

std::ostream& operator<<(
std::ostream& os,
const JsErrorHandler::ParsedError::StackFrame& frame) {
const JsErrorHandler::ProcessedError::StackFrame& frame) {
auto file = frame.file ? quote(*frame.file) : "nil";
auto methodName = quote(frame.methodName);
auto lineNumber =
Expand All @@ -188,7 +188,7 @@ std::ostream& operator<<(
}
std::ostream& operator<<(
std::ostream& os,
const JsErrorHandler::ParsedError& error) {
const JsErrorHandler::ProcessedError& error) {
auto message = quote(error.message);
auto originalMessage =
error.originalMessage ? quote(*error.originalMessage) : "nil";
Expand All @@ -199,7 +199,7 @@ std::ostream& operator<<(
auto isFatal = std::to_string(static_cast<int>(error.isFatal));
auto extraData = "jsi::Object{ <omitted> } ";

os << "ParsedError {\n"
os << "ProcessedError {\n"
<< " .message = " << message << "\n"
<< " .originalMessage = " << originalMessage << "\n"
<< " .name = " << name << "\n"
Expand Down Expand Up @@ -329,7 +329,7 @@ void JsErrorHandler::handleErrorWithCppPipeline(

auto id = nextExceptionId();

ParsedError parsedError = {
ProcessedError processedError = {
.message =
_isRuntimeReady ? message : ("[runtime not ready]: " + message),
.originalMessage = originalMessage,
Expand All @@ -341,7 +341,7 @@ void JsErrorHandler::handleErrorWithCppPipeline(
.extraData = std::move(extraData),
};

auto data = bridging::toJs(runtime, parsedError).asObject(runtime);
auto data = bridging::toJs(runtime, processedError).asObject(runtime);

auto isComponentError =
isTruthy(runtime, errorObj.getProperty(runtime, "isComponentError"));
Expand All @@ -351,7 +351,7 @@ void JsErrorHandler::handleErrorWithCppPipeline(
auto console = runtime.global().getPropertyAsObject(runtime, "console");
auto errorFn = console.getPropertyAsFunction(runtime, "error");
auto finalMessage =
jsi::String::createFromUtf8(runtime, parsedError.message);
jsi::String::createFromUtf8(runtime, processedError.message);
errorFn.callWithThis(runtime, console, finalMessage);
}

Expand Down Expand Up @@ -397,7 +397,7 @@ void JsErrorHandler::handleErrorWithCppPipeline(
_hasHandledFatalError = true;
}

_onJsError(runtime, parsedError);
_onJsError(runtime, processedError);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace facebook::react {

class JsErrorHandler {
public:
struct ParsedError {
struct ProcessedError {
struct StackFrame {
std::optional<std::string> file;
std::string methodName;
Expand All @@ -34,11 +34,13 @@ class JsErrorHandler {
int id;
bool isFatal;
jsi::Object extraData;
friend std::ostream& operator<<(std::ostream& os, const ParsedError& error);
friend std::ostream& operator<<(
std::ostream& os,
const ProcessedError& error);
};

using OnJsError =
std::function<void(jsi::Runtime& runtime, const ParsedError& error)>;
std::function<void(jsi::Runtime& runtime, const ProcessedError& error)>;

explicit JsErrorHandler(OnJsError onJsError);
~JsErrorHandler();
Expand Down
Loading

0 comments on commit ca1d517

Please sign in to comment.