From 1e962bd1d76975adf96c793bd9fcf76e2fea8492 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Thu, 16 Jan 2025 01:41:44 +0100 Subject: [PATCH] feat: Add `try`/`catch` block for prop parsing --- .../src/views/CppHybridViewComponent.ts | 12 ++++++---- .../c++/views/HybridTestViewComponent.cpp | 24 ++++++++++++------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/packages/nitrogen/src/views/CppHybridViewComponent.ts b/packages/nitrogen/src/views/CppHybridViewComponent.ts index b418cbf2..2aca8b3e 100644 --- a/packages/nitrogen/src/views/CppHybridViewComponent.ts +++ b/packages/nitrogen/src/views/CppHybridViewComponent.ts @@ -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)*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)*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( diff --git a/packages/react-native-nitro-image/nitrogen/generated/shared/c++/views/HybridTestViewComponent.cpp b/packages/react-native-nitro-image/nitrogen/generated/shared/c++/views/HybridTestViewComponent.cpp index 2fd0f387..7cd726ae 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/shared/c++/views/HybridTestViewComponent.cpp +++ b/packages/react-native-nitro-image/nitrogen/generated/shared/c++/views/HybridTestViewComponent.cpp @@ -17,16 +17,24 @@ namespace margelo::nitro::image::views { const react::RawProps& rawProps): react::ViewProps(context, sourceProps, rawProps, filterObjectKeys), someProp([&]() -> CachedProp { - const react::RawValue* rawValue = rawProps.at("someProp", nullptr, nullptr); - if (rawValue == nullptr) return {}; - const auto& [runtime, value] = (std::pair)*rawValue; - return CachedProp::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)*rawValue; + return CachedProp::fromRawValue(*runtime, value, sourceProps.someProp); + } catch (const std::exception& exc) { + throw std::runtime_error(std::string("TestView.someProp: ") + exc.what()); + } }()), someCallback([&]() -> CachedProp> { - const react::RawValue* rawValue = rawProps.at("someCallback", nullptr, nullptr); - if (rawValue == nullptr) return {}; - const auto& [runtime, value] = (std::pair)*rawValue; - return CachedProp>::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)*rawValue; + return CachedProp>::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):