fix: Log Stacktrace on Frame Processor Error (#731)

* fix: Log JS Stack on Error

* Android

* Format Stacktrace better

* Update FrameProcessorUtils.mm

* Allow unapproved C++11 headers

* Use `.c_str()`
This commit is contained in:
Marc Rousavy 2022-01-10 16:37:47 +01:00 committed by GitHub
parent 233094d18e
commit 0904767cf2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 7 deletions

View File

@ -31,4 +31,5 @@ jobs:
,-build/namespaces\ ,-build/namespaces\
,-whitespace/comments\ ,-whitespace/comments\
,-build/include_order\ ,-build/include_order\
,-build/c++11\
" "

View File

@ -6,9 +6,11 @@
#include <jni.h> #include <jni.h>
#include <fbjni/fbjni.h> #include <fbjni/fbjni.h>
#include <jsi/jsi.h>
#include <memory> #include <memory>
#include <string> #include <string>
#include <regex>
namespace vision { namespace vision {
@ -36,9 +38,12 @@ void CameraView::frameProcessorCallback(const alias_ref<JImageProxy::javaobject>
try { try {
frameProcessor_(frame); frameProcessor_(frame);
} catch (const std::exception& exception) { } catch (const jsi::JSError& error) {
// TODO: jsi::JSErrors cannot be caught on Hermes. They crash the entire app. // TODO: jsi::JSErrors cannot be caught on Hermes. They crash the entire app.
__android_log_print(ANDROID_LOG_ERROR, TAG, "Frame Processor threw an error! %s", exception.what()); auto stack = std::regex_replace(error.getStack(), std::regex("\n"), "\n ");
__android_log_print(ANDROID_LOG_ERROR, TAG, "Frame Processor threw an error! %s\nIn: %s", error.getMessage().c_str(), stack.c_str());
} catch (const std::exception& exception) {
__android_log_print(ANDROID_LOG_ERROR, TAG, "Frame Processor threw a C++ error! %s", exception.what());
} }
} }

View File

@ -9,6 +9,7 @@
#import "FrameProcessorUtils.h" #import "FrameProcessorUtils.h"
#import <chrono> #import <chrono>
#import <memory> #import <memory>
#import <regex>
#import "FrameHostObject.h" #import "FrameHostObject.h"
#import "Frame.h" #import "Frame.h"
@ -18,7 +19,7 @@
#import "JSConsoleHelper.h" #import "JSConsoleHelper.h"
#import <ReactCommon/RCTTurboModule.h> #import <ReactCommon/RCTTurboModule.h>
FrameProcessorCallback convertJSIFunctionToFrameProcessorCallback(jsi::Runtime &runtime, const jsi::Function &value) { FrameProcessorCallback convertJSIFunctionToFrameProcessorCallback(jsi::Runtime& runtime, const jsi::Function& value) {
__block auto cb = value.getFunction(runtime); __block auto cb = value.getFunction(runtime);
return ^(Frame* frame) { return ^(Frame* frame) {
@ -27,15 +28,17 @@ FrameProcessorCallback convertJSIFunctionToFrameProcessorCallback(jsi::Runtime &
try { try {
cb.callWithThis(runtime, cb, jsi::Object::createFromHostObject(runtime, frameHostObject)); cb.callWithThis(runtime, cb, jsi::Object::createFromHostObject(runtime, frameHostObject));
} catch (jsi::JSError& jsError) { } catch (jsi::JSError& jsError) {
auto message = jsError.getMessage(); auto stack = std::regex_replace(jsError.getStack(), std::regex("\n"), "\n ");
auto message = [NSString stringWithFormat:@"Frame Processor threw an error: %s\nIn: %s", jsError.getMessage().c_str(), stack.c_str()];
RCTBridge* bridge = [RCTBridge currentBridge]; RCTBridge* bridge = [RCTBridge currentBridge];
if (bridge != nil) { if (bridge != nil) {
bridge.jsCallInvoker->invokeAsync([bridge, message]() { bridge.jsCallInvoker->invokeAsync([bridge, message]() {
auto logFn = [JSConsoleHelper getLogFunctionForBridge:bridge]; auto logFn = [JSConsoleHelper getLogFunctionForBridge:bridge];
logFn(RCTLogLevelError, [NSString stringWithFormat:@"Frame Processor threw an error: %s", message.c_str()]); logFn(RCTLogLevelError, message);
}); });
} else { } else {
NSLog(@"Frame Processor threw an error: %s", message.c_str()); NSLog(@"%@", message);
} }
} }

View File

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
if which cpplint >/dev/null; then if which cpplint >/dev/null; then
cpplint --linelength=230 --filter=-legal/copyright,-readability/todo,-build/namespaces,-whitespace/comments,-build/include_order --quiet --recursive --exclude "android/src/main/cpp/reanimated-headers" cpp android/src/main/cpp cpplint --linelength=230 --filter=-legal/copyright,-readability/todo,-build/namespaces,-whitespace/comments,-build/include_order,-build/c++11 --quiet --recursive --exclude "android/src/main/cpp/reanimated-headers" cpp android/src/main/cpp
else else
echo "warning: cpplint not installed, download from https://github.com/cpplint/cpplint" echo "warning: cpplint not installed, download from https://github.com/cpplint/cpplint"
fi fi