Skip to content

Commit

Permalink
feat: Add try/catch block for prop parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Jan 16, 2025
1 parent 6daa311 commit 1e962bd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
12 changes: 8 additions & 4 deletions packages/nitrogen/src/views/CppHybridViewComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,14 @@ namespace ${namespace} {
propInitializers.push(
`
${escapeCppName(prop.name)}([&]() -> CachedProp<${type}> {
const react::RawValue* rawValue = rawProps.at("${prop.name}", nullptr, nullptr);
if (rawValue == nullptr) return {};
const auto& [runtime, value] = (std::pair<jsi::Runtime*, const jsi::Value&>)*rawValue;
return CachedProp<${type}>::fromRawValue(*runtime, value, sourceProps.${escapeCppName(prop.name)});
try {
const react::RawValue* rawValue = rawProps.at("${prop.name}", nullptr, nullptr);
if (rawValue == nullptr) return {};
const auto& [runtime, value] = (std::pair<jsi::Runtime*, const jsi::Value&>)*rawValue;
return CachedProp<${type}>::fromRawValue(*runtime, value, sourceProps.${escapeCppName(prop.name)});
} catch (const std::exception& exc) {
throw std::runtime_error(std::string("${spec.name}.${prop.name}: ") + exc.what());
}
}())`.trim()
)
propCopyInitializers.push(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,24 @@ namespace margelo::nitro::image::views {
const react::RawProps& rawProps):
react::ViewProps(context, sourceProps, rawProps, filterObjectKeys),
someProp([&]() -> CachedProp<bool> {
const react::RawValue* rawValue = rawProps.at("someProp", nullptr, nullptr);
if (rawValue == nullptr) return {};
const auto& [runtime, value] = (std::pair<jsi::Runtime*, const jsi::Value&>)*rawValue;
return CachedProp<bool>::fromRawValue(*runtime, value, sourceProps.someProp);
try {
const react::RawValue* rawValue = rawProps.at("someProp", nullptr, nullptr);
if (rawValue == nullptr) return {};
const auto& [runtime, value] = (std::pair<jsi::Runtime*, const jsi::Value&>)*rawValue;
return CachedProp<bool>::fromRawValue(*runtime, value, sourceProps.someProp);
} catch (const std::exception& exc) {
throw std::runtime_error(std::string("TestView.someProp: ") + exc.what());
}
}()),
someCallback([&]() -> CachedProp<std::function<void(double /* someParam */)>> {
const react::RawValue* rawValue = rawProps.at("someCallback", nullptr, nullptr);
if (rawValue == nullptr) return {};
const auto& [runtime, value] = (std::pair<jsi::Runtime*, const jsi::Value&>)*rawValue;
return CachedProp<std::function<void(double /* someParam */)>>::fromRawValue(*runtime, value, sourceProps.someCallback);
try {
const react::RawValue* rawValue = rawProps.at("someCallback", nullptr, nullptr);
if (rawValue == nullptr) return {};
const auto& [runtime, value] = (std::pair<jsi::Runtime*, const jsi::Value&>)*rawValue;
return CachedProp<std::function<void(double /* someParam */)>>::fromRawValue(*runtime, value, sourceProps.someCallback);
} catch (const std::exception& exc) {
throw std::runtime_error(std::string("TestView.someCallback: ") + exc.what());
}
}()) { }

HybridTestViewProps::HybridTestViewProps(const HybridTestViewProps& other):
Expand Down

0 comments on commit 1e962bd

Please sign in to comment.