Add the invalid value to the type error exception message

This commit is contained in:
Thomas Goyne 2017-09-14 16:01:59 -07:00
parent e564ad1de8
commit b8fd4fb861
3 changed files with 30 additions and 32 deletions

View File

@ -22,8 +22,6 @@
#include "js_realm_object.hpp" #include "js_realm_object.hpp"
#include "js_schema.hpp" #include "js_schema.hpp"
#include "util/format.hpp"
namespace realm { namespace realm {
class List; class List;
class Object; class Object;
@ -65,8 +63,9 @@ public:
ValueType value = Object::get_property(m_ctx, object, prop_name); ValueType value = Object::get_property(m_ctx, object, prop_name);
const auto& prop = m_object_schema.persisted_properties[prop_index]; const auto& prop = m_object_schema.persisted_properties[prop_index];
if (!Value::is_valid_for_property(m_ctx, value, prop)) { if (!Value::is_valid_for_property(m_ctx, value, prop)) {
throw TypeErrorException(util::format("%1.%2", m_object_schema.name, prop.name), throw TypeErrorException(m_object_schema.name, prop.name,
js_type_name_for_property_type(prop.type)); js_type_name_for_property_type(prop.type),
print(value));
} }
return value; return value;
} }
@ -121,7 +120,7 @@ public:
void will_change(realm::Object&, realm::Property const&) { } void will_change(realm::Object&, realm::Property const&) { }
void did_change() { } void did_change() { }
std::string print(ValueType const&) { return "not implemented"; } std::string print(ValueType const& v) { return Value::to_string(m_ctx, v); }
private: private:
ContextType m_ctx; ContextType m_ctx;

View File

@ -127,12 +127,13 @@ bool RealmObjectClass<T>::set_property(ContextType ctx, ObjectType object, const
return false; return false;
} }
NativeAccessor<T> accessor(ctx, realm_object->realm(), realm_object->get_object_schema());
if (!Value::is_valid_for_property(ctx, value, *prop)) { if (!Value::is_valid_for_property(ctx, value, *prop)) {
throw TypeErrorException(util::format("%1.%2", realm_object->get_object_schema().name, property_name), throw TypeErrorException(realm_object->get_object_schema().name, property_name,
js_type_name_for_property_type(prop->type)); js_type_name_for_property_type(prop->type),
accessor.print(value));
} }
NativeAccessor<T> accessor(ctx, realm_object->realm(), realm_object->get_object_schema());
realm_object->set_property_value(accessor, property_name, value, true); realm_object->set_property_value(accessor, property_name, value, true);
return true; return true;
} }

View File

@ -26,6 +26,7 @@
#include <vector> #include <vector>
#include <realm/binary_data.hpp> #include <realm/binary_data.hpp>
#include <realm/string_data.hpp>
#include <realm/util/to_string.hpp> #include <realm/util/to_string.hpp>
#if defined(__GNUC__) && !(defined(DEBUG) && DEBUG) #if defined(__GNUC__) && !(defined(DEBUG) && DEBUG)
@ -80,18 +81,16 @@ struct Context {
class TypeErrorException : public std::invalid_argument { class TypeErrorException : public std::invalid_argument {
public: public:
std::string const& prefix() const { return m_prefix; } TypeErrorException(StringData object_type, StringData property,
std::string const& type() const { return m_type; } std::string const& type, std::string const& value)
: std::invalid_argument(util::format("%1.%2 must be of type '%3', got (%4)",
object_type, property, type, value))
{}
TypeErrorException(std::string prefix, std::string type) : TypeErrorException(const char *name, std::string const& type, std::string const& value)
std::invalid_argument(prefix + " must be of type: " + type), : std::invalid_argument(util::format("%1 must be of type '%2', got (%3)",
m_prefix(std::move(prefix)), name ? name : "JS value", type, value))
m_type(std::move(type)) {}
{}
private:
std::string m_prefix;
std::string m_type;
}; };
template<typename T> template<typename T>
@ -138,8 +137,7 @@ struct Value {
#define VALIDATED(return_t, type) \ #define VALIDATED(return_t, type) \
static return_t validated_to_##type(ContextType ctx, const ValueType &value, const char *name = nullptr) { \ static return_t validated_to_##type(ContextType ctx, const ValueType &value, const char *name = nullptr) { \
if (!is_##type(ctx, value)) { \ if (!is_##type(ctx, value)) { \
std::string prefix = name ? std::string("'") + name + "'" : "JS value"; \ throw TypeErrorException(name, #type, to_string(ctx, value)); \
throw TypeErrorException(prefix, #type); \
} \ } \
return to_##type(ctx, value); \ return to_##type(ctx, value); \
} }