Skip to content

Commit

Permalink
Handling JSError on iOS (#2153)
Browse files Browse the repository at this point in the history
## Description

Now on iOS is possible to catch JSError thrown from the worklet.

## Example
```js
const a = () => {
  'worklet';
  throw new Error('exception_message')
}

const style = useAnimatedStyle(() => {
  'worklet'
  if(_WORKLET) {
    try {
      a();
    } 
    catch(e) {
      console.log("SUCCESS!", e.message)
    }
  }

  return {
    width: withTiming(sv.value),
  };
});
```
  • Loading branch information
piaskowyk committed Jun 25, 2021
1 parent 17b8f66 commit e0b9712
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Common/cpp/SharedItems/ShareableValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "RemoteObject.h"
#include "FrozenObject.h"
#include "RuntimeDecorator.h"
#include <cxxabi.h>

namespace reanimated {

Expand Down Expand Up @@ -352,6 +353,9 @@ jsi::Value ShareableValue::toJSValue(jsi::Runtime &rt) {
} catch(jsi::JSError &e) {
throw e;
} catch(...) {
if(demangleExceptionName(abi::__cxa_current_exception_type()->name()) == "facebook::jsi::JSError") {
throw jsi::JSError(rt, "Javascript worklet error");
}
// TODO find out a way to get the error's message on hermes
jsi::Value location = jsThis->getProperty(rt, "__location");
std::string str = "Javascript worklet error";
Expand Down Expand Up @@ -407,6 +411,9 @@ jsi::Value ShareableValue::toJSValue(jsi::Runtime &rt) {
runtimeManager->errorHandler->setError(str);
runtimeManager->errorHandler->raise();
} catch(...) {
if(demangleExceptionName(abi::__cxa_current_exception_type()->name()) == "facebook::jsi::JSError") {
throw jsi::JSError(rt, "Javascript worklet error");
}
// TODO find out a way to get the error's message on hermes
jsi::Value location = jsThis.getProperty(rt, "__location");
std::string str = "Javascript worklet error";
Expand All @@ -433,4 +440,15 @@ jsi::Value ShareableValue::toJSValue(jsi::Runtime &rt) {
throw "convert error";
}

std::string ShareableValue::demangleExceptionName(std::string toDemangle) {
int status = 0;
char * buff = __cxxabiv1::__cxa_demangle(toDemangle.c_str(), nullptr, nullptr, &status);

This comment has been minimized.

Copy link
@mrousavy

mrousavy Jun 25, 2021

Contributor

wtf nice 😅

This comment has been minimized.

Copy link
@piaskowyk

piaskowyk Jun 25, 2021

Author Member

I agree 🤣 Strange but this is a consequence of a duplicated JSError on Hermes and ReactNative - I mentioned this last time.

if (!buff) {
return toDemangle;
}
std::string demangled = buff;
std::free(buff);
return demangled;
}

}
1 change: 1 addition & 0 deletions Common/cpp/headers/SharedItems/ShareableValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ friend void extractMutables(jsi::Runtime &rt,
jsi::Object createHost(jsi::Runtime &rt, std::shared_ptr<jsi::HostObject> host);
void adapt(jsi::Runtime &rt, const jsi::Value &value, ValueType objectType);
void adaptCache(jsi::Runtime &rt, const jsi::Value &value);
std::string demangleExceptionName(std::string toDemangle);

public:
ValueType type = ValueType::UndefinedType;
Expand Down

0 comments on commit e0b9712

Please sign in to comment.