diff --git a/src/js_realm.hpp b/src/js_realm.hpp index 7761a595..1f97173d 100644 --- a/src/js_realm.hpp +++ b/src/js_realm.hpp @@ -253,17 +253,30 @@ public: }; private: - static void translateSharedGroupOpenException() { + static void translateSharedGroupOpenException(ContextType ctx, realm::Realm::Config config) { try { throw; } catch (RealmFileException const& ex) { - switch (ex.kind()) { - case RealmFileException::Kind::IncompatibleSyncedRealm: { - throw std::runtime_error("IncompatibleSyncedRealm: "+ ex.path()); + switch (ex.kind()) { + case RealmFileException::Kind::IncompatibleSyncedRealm: { + // create an object which is going to be used as exception: + // { message: 'IncompatibleSyncedRealmException', configuration: { path: ... } } + + ObjectType configuration = Object::create_empty(ctx); + Object::set_property(ctx, configuration, "path", Value::from_string(ctx, ex.path())); + Object::set_property(ctx, configuration, "schema_mode", Value::from_string(ctx, "readOnly")); + if (!config.encryption_key.empty()) { + Object::set_property(ctx, configuration, "encryption_key", Value::from_binary(ctx, BinaryData(&config.encryption_key[0], 64))); + } + + ObjectType object = Object::create_empty(ctx); + Object::set_property(ctx, object, "message", Value::from_string(ctx, "IncompatibleSyncedRealmException")); + Object::set_property(ctx, object, "configuration", configuration); + throw Exception(ctx, object); + } default: throw; - } } } } @@ -503,7 +516,7 @@ SharedRealm RealmClass::create_shared_realm(ContextType ctx, realm::Realm::Co realm = realm::Realm::get_shared_realm(config); } catch (...) { - translateSharedGroupOpenException(); + translateSharedGroupOpenException(ctx, config); } GlobalContextType global_context = Context::get_global_context(ctx); @@ -719,7 +732,7 @@ void RealmClass::wait_for_download_completion(ContextType ctx, FunctionType, realm = realm::Realm::get_shared_realm(config); } catch (...) { - translateSharedGroupOpenException(); + translateSharedGroupOpenException(ctx, config); } if (auto sync_config = config.sync_config) diff --git a/src/js_types.hpp b/src/js_types.hpp index 4ce626c6..e1cf6459 100644 --- a/src/js_types.hpp +++ b/src/js_types.hpp @@ -318,31 +318,6 @@ struct Exception : public std::runtime_error { } }; -template -struct IncompatibleSyncedRealmException : public std::runtime_error { - using ContextType = typename T::Context; - using ObjectType = typename T::Object; - - std::string m_path; - std::vector m_encryption_key; - - IncompatibleSyncedRealmException(ContextType ctx, const std::string &path) - : std::runtime_error(std::string("Incompatible Synced Realm")), m_path(path) {} - IncompatibleSyncedRealmException(ContextType ctx, const std::string &path, const std::vector &key) - : std::runtime_error(std::string("Incompatible Synced Realm")), m_path(path), m_encryption_key(key) {} - - - ObjectType config(ContextType ctx) { - ObjectType configuration = ObjectType::create_empty(ctx); - ObjectType::set_property(ctx, configuration, "path", m_path); - ObjectType::set_property(ctx, configuration, "schema_mode", to_string(ctx, "readOnly")); - if (!m_encryption_key.empty()) { - ObjectType::set_property(ctx, configuration, "encryption_key", to_binary(ctx, m_encryption_key)); - } - return configuration; - } -}; - template struct ReturnValue { using ValueType = typename T::Value;