Skip to content

Commit

Permalink
Revert D37055069: Add key to prop conversion errors
Browse files Browse the repository at this point in the history
Differential Revision:
D37055069 (80d626a)

Original commit changeset: 8ce121a9b187

Original Phabricator Diff: D37055069 (80d626a)

fbshipit-source-id: b364e29f6c2d75c19392f705910d753a328e798d
  • Loading branch information
Ravi Shah authored and facebook-github-bot committed Aug 22, 2022
1 parent 5f0fac5 commit f5f6896
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 27 deletions.
24 changes: 14 additions & 10 deletions ReactCommon/react/renderer/core/RawValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class RawValue {
* Casts the value to a specified type.
*/
template <typename T>
explicit operator T() const {
explicit operator T() const noexcept {
return castValue(dynamic_, (T *)nullptr);
}

Expand Down Expand Up @@ -212,36 +212,40 @@ class RawValue {
return RawValue(dynamic);
}

static bool castValue(const folly::dynamic &dynamic, bool *type) {
static bool castValue(const folly::dynamic &dynamic, bool *type) noexcept {
return dynamic.getBool();
}

static int castValue(const folly::dynamic &dynamic, int *type) {
static int castValue(const folly::dynamic &dynamic, int *type) noexcept {
return static_cast<int>(dynamic.asInt());
}

static int64_t castValue(const folly::dynamic &dynamic, int64_t *type) {
static int64_t castValue(
const folly::dynamic &dynamic,
int64_t *type) noexcept {
return dynamic.asInt();
}

static float castValue(const folly::dynamic &dynamic, float *type) {
static float castValue(const folly::dynamic &dynamic, float *type) noexcept {
return static_cast<float>(dynamic.asDouble());
}

static double castValue(const folly::dynamic &dynamic, double *type) {
static double castValue(
const folly::dynamic &dynamic,
double *type) noexcept {
return dynamic.asDouble();
}

static std::string castValue(
const folly::dynamic &dynamic,
std::string *type) {
std::string *type) noexcept {
return dynamic.getString();
}

template <typename T>
static std::vector<T> castValue(
const folly::dynamic &dynamic,
std::vector<T> *type) {
std::vector<T> *type) noexcept {
react_native_assert(dynamic.isArray());
auto result = std::vector<T>{};
result.reserve(dynamic.size());
Expand All @@ -254,7 +258,7 @@ class RawValue {
template <typename T>
static std::vector<std::vector<T>> castValue(
const folly::dynamic &dynamic,
std::vector<std::vector<T>> *type) {
std::vector<std::vector<T>> *type) noexcept {
react_native_assert(dynamic.isArray());
auto result = std::vector<std::vector<T>>{};
result.reserve(dynamic.size());
Expand All @@ -267,7 +271,7 @@ class RawValue {
template <typename T>
static butter::map<std::string, T> castValue(
const folly::dynamic &dynamic,
butter::map<std::string, T> *type) {
butter::map<std::string, T> *type) noexcept {
react_native_assert(dynamic.isObject());
auto result = butter::map<std::string, T>{};
for (const auto &item : dynamic.items()) {
Expand Down
52 changes: 35 additions & 17 deletions ReactCommon/react/renderer/core/propsConversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#include <optional>

#include <folly/Likely.h>
#include <folly/dynamic.h>
#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/core/RawProps.h>
#include <react/renderer/core/RawPropsKey.h>
#include <react/renderer/graphics/Color.h>
#include <react/renderer/graphics/Geometry.h>
#include <react/renderer/graphics/conversions.h>
Expand Down Expand Up @@ -43,18 +43,18 @@ template <typename T>
void fromRawValue(
const PropsParserContext &context,
RawValue const &rawValue,
T &result) {
result = (T)rawValue;
std::optional<T> &result) {
T res{};
fromRawValue(context, rawValue, res);
result = std::optional<T>(res);
}

template <typename T>
void fromRawValue(
const PropsParserContext &context,
RawValue const &rawValue,
std::optional<T> &result) {
T resultValue;
fromRawValue(context, rawValue, resultValue);
result = std::optional<T>{std::move(resultValue)};
T &result) {
result = (T)rawValue;
}

template <typename T>
Expand Down Expand Up @@ -119,6 +119,7 @@ T convertRawProp(
char const *namePrefix = nullptr,
char const *nameSuffix = nullptr) {
const auto *rawValue = rawProps.at(name, namePrefix, nameSuffix);

if (LIKELY(rawValue == nullptr)) {
return sourceValue;
}
Expand All @@ -129,18 +130,35 @@ T convertRawProp(
return defaultValue;
}

try {
T result;
fromRawValue(context, *rawValue, result);
return result;
} catch (const std::exception &e) {
// In case of errors, log the error and fall back to the default
RawPropsKey key{namePrefix, name, nameSuffix};
// TODO: report this using ErrorUtils so it's more visible to the user
LOG(ERROR) << "Error while converting prop '"
<< static_cast<std::string>(key) << "': " << e.what();
T result;
fromRawValue(context, *rawValue, result);
return result;
}

template <typename T>
static std::optional<T> convertRawProp(
const PropsParserContext &context,
RawProps const &rawProps,
char const *name,
std::optional<T> const &sourceValue,
std::optional<T> const &defaultValue,
char const *namePrefix = nullptr,
char const *nameSuffix = nullptr) {
const auto *rawValue = rawProps.at(name, namePrefix, nameSuffix);

if (LIKELY(rawValue == nullptr)) {
return sourceValue;
}

// Special case: `null` always means `the prop was removed, use default
// value`.
if (UNLIKELY(!rawValue->hasValue())) {
return defaultValue;
}

T result;
fromRawValue(context, *rawValue, result);
return std::optional<T>{result};
}

} // namespace react
Expand Down

0 comments on commit f5f6896

Please sign in to comment.