-
Notifications
You must be signed in to change notification settings - Fork 24.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
POC: Mixed stack traces for Android and iOS Turbo Modules #36925
Closed
krystofwoldrich
wants to merge
10
commits into
facebook:main
from
krystofwoldrich:kw-poc-tm-mixed-stack-traces
Closed
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a5dec1b
Add throw stringified NSException from Obj-C and capture it in JSC
krystofwoldrich 6a721fa
Add java stringified stack trace to the JS error
krystofwoldrich e3d9b33
Add JSError with cause for NSExceptions
krystofwoldrich 954e720
fix typo
krystofwoldrich 4b69db0
Add JSError with cause for Throwable
krystofwoldrich e17d3f9
Refactor cause, remove duplicate data, extract exception conversion t…
krystofwoldrich 847043e
Use dedicated method to get throwable name and message
krystofwoldrich 3a8279c
Apply suggestions from code review
krystofwoldrich 1e7232e
Fix format
krystofwoldrich 956bf59
Merge branch 'main' into kw-poc-tm-mixed-stack-traces
krystofwoldrich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -401,6 +401,48 @@ jsi::Value convertFromJMapToValue(JNIEnv *env, jsi::Runtime &rt, jobject arg) { | |
return jsi::valueFromDynamic(rt, result->cthis()->consume()); | ||
} | ||
|
||
jsi::Value createJSRuntimeError(jsi::Runtime &runtime, const std::string& message) | ||
{ | ||
return runtime.global().getPropertyAsFunction(runtime, "Error").call(runtime, message); | ||
} | ||
|
||
/** | ||
* Creates JSError with current JS runtime stack and Throwable stack trace. | ||
*/ | ||
jsi::JSError convertThrowableToJSError(jsi::Runtime &runtime, facebook::jni::local_ref<facebook::jni::JThrowable> throwable) | ||
{ | ||
auto stackTrace = throwable->getStackTrace(); | ||
|
||
jsi::Array stackElements(runtime, stackTrace->size()); | ||
for (int i = 0; i < stackTrace->size(); ++i) { | ||
auto frame = stackTrace->getElement(i); | ||
|
||
jsi::Object frameObject(runtime); | ||
frameObject.setProperty(runtime, "className", frame->getClassName()); | ||
frameObject.setProperty(runtime, "fileName", frame->getFileName()); | ||
frameObject.setProperty(runtime, "lineNumber", frame->getLineNumber()); | ||
frameObject.setProperty(runtime, "methodName", frame->getMethodName()); | ||
stackElements.setValueAtIndex(runtime, i, std::move(frameObject)); | ||
} | ||
|
||
jsi::Object cause(runtime); | ||
auto getName = throwable->getClass()->getClass() | ||
->getMethod<facebook::jni::local_ref<facebook::jni::JString>()>("getSimpleName"); | ||
auto getMessage = throwable->getClass() | ||
->getMethod<facebook::jni::local_ref<facebook::jni::JString>()>("getMessage"); | ||
krystofwoldrich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
auto message = getMessage(throwable)->toStdString(); | ||
cause.setProperty( | ||
runtime, | ||
"name", | ||
getName(throwable->getClass())->toStdString()); | ||
cause.setProperty(runtime,"message",message); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: formatting |
||
cause.setProperty(runtime, "stackElements", std::move(stackElements)); | ||
|
||
jsi::Value error = createJSRuntimeError(runtime, "Exception in HostFunction: " + message); | ||
error.asObject(runtime).setProperty(runtime, "cause", std::move(cause)); | ||
return {runtime, std::move(error)}; | ||
} | ||
|
||
} // namespace | ||
|
||
jsi::Value JavaTurboModule::invokeJavaMethod( | ||
|
@@ -468,7 +510,9 @@ jsi::Value JavaTurboModule::invokeJavaMethod( | |
} else { | ||
TMPL::asyncMethodCallFail(moduleName, methodName); | ||
} | ||
throw; | ||
auto exception = std::current_exception(); | ||
auto throwable = facebook::jni::getJavaExceptionForCppException(exception); | ||
krystofwoldrich marked this conversation as resolved.
Show resolved
Hide resolved
krystofwoldrich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw convertThrowableToJSError(runtime, throwable); | ||
} | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't know that I can reach the methods like this, so the change to the fbjni interface is not necessary, but would make it cleaner.