From e81d8589aede3529701df6b70b50d9ee85aa205a Mon Sep 17 00:00:00 2001 From: Kenneth Geisshirt Date: Mon, 6 Nov 2017 13:51:33 +0100 Subject: [PATCH] Post-release feedback on client reset (#1372) (#1459) * Post-release feedback on client reset (#1372) * Using error.name instead --- CHANGELOG.md | 6 ++++-- docs/realm.js | 2 +- docs/sync.js | 2 +- lib/index.d.ts | 5 +++-- src/js_sync.hpp | 10 +++++----- tests/js/session-tests.js | 8 ++++---- 6 files changed, 18 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6be4ee9d..bea4eb2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,10 +5,12 @@ X.Y.Z Release notes ### Enhancements * Better support for React Native 0.49 for iOS (#1431). +* Added property `name` to `error` in `Sync.error` callback. +* Sync error handler provides also a property called `name`; `code` is not changed. -### Bug fixea +### Bug fixed * Fixed missing Realm constructor in while debugging React Native apps (#1436). -* Remove argument in documentation of `Realm.Sync.Adapter.realmAtPath()`. +* Removed argument in documentation of `Realm.Sync.Adapter.realmAtPath()`. ### Internal * None. diff --git a/docs/realm.js b/docs/realm.js index 2ded8b08..5ae17650 100644 --- a/docs/realm.js +++ b/docs/realm.js @@ -309,7 +309,7 @@ Realm.defaultPath; * - `user` - A `User` object obtained by calling `Realm.Sync.User.login` * - `url` - A `string` which contains a valid Realm Sync url * - `error` - A callback function which is called in error situations. - * The `error` callback can take up to four optional arguments: `message`, `isFatal`, + * The `error` callback can take up to five optional arguments: `name`, `message`, `isFatal`, * `category`, and `code`. * - `validate_ssl` - Indicating if SSL certificates must be validated * - `ssl_trust_certificate_path` - A path where to find trusted SSL certificates diff --git a/docs/sync.js b/docs/sync.js index 6a3e8b8b..711d0046 100644 --- a/docs/sync.js +++ b/docs/sync.js @@ -70,7 +70,7 @@ class Sync { * { * const config = { sync: { user, url: 'realm://localhost:9080/~/myrealm' } }; * config.sync.error = (sender, error) => { - * if (error.code === 7) { // 7 -> client reset + * if (error.name === 'ClientReset') { * Realm.Sync.initiateClientReset(original_path); * // copy required objects from Realm at error.config.path * } diff --git a/lib/index.d.ts b/lib/index.d.ts index 8a72bfe2..ca94aae4 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -346,9 +346,10 @@ declare namespace Realm.Sync { } interface SyncError { + name: string; message: string; - isFatal: boolean - category?: string + isFatal: boolean; + category?: string; code: number; } diff --git a/src/js_sync.hpp b/src/js_sync.hpp index 1e6961c5..a8cc9001 100644 --- a/src/js_sync.hpp +++ b/src/js_sync.hpp @@ -228,22 +228,22 @@ public: void operator()(std::shared_ptr session, SyncError error) { HANDLESCOPE + std::string name = "Error"; auto error_object = Object::create_empty(m_ctx); - - auto error_code = error.error_code.value(); + if (error.is_client_reset_requested()) { - error_code = 7; // FIXME: define a proper constant - auto config_object = Object::create_empty(m_ctx); Object::set_property(m_ctx, config_object, "path", Value::from_string(m_ctx, error.user_info[SyncError::c_recovery_file_path_key])); Object::set_property(m_ctx, config_object, "readOnly", Value::from_boolean(m_ctx, true)); Object::set_property(m_ctx, error_object, "config", config_object); + name = "ClientReset"; } + Object::set_property(m_ctx, error_object, "name", Value::from_string(m_ctx, name)); Object::set_property(m_ctx, error_object, "message", Value::from_string(m_ctx, error.message)); Object::set_property(m_ctx, error_object, "isFatal", Value::from_boolean(m_ctx, error.is_fatal)); Object::set_property(m_ctx, error_object, "category", Value::from_string(m_ctx, error.error_code.category().name())); - Object::set_property(m_ctx, error_object, "code", Value::from_number(m_ctx, error_code)); + Object::set_property(m_ctx, error_object, "code", Value::from_number(m_ctx, error.error_code.value())); auto user_info = Object::create_empty(m_ctx); for (auto& kvp : error.user_info) { diff --git a/tests/js/session-tests.js b/tests/js/session-tests.js index 1d8d8d50..8957b886 100644 --- a/tests/js/session-tests.js +++ b/tests/js/session-tests.js @@ -742,13 +742,13 @@ module.exports = { const config = { sync: { user, url: 'realm://localhost:9080/~/myrealm' } }; config.sync.error = (sender, error) => { try { - TestCase.assertEqual(error.code, 7); // 7 -> client reset + TestCase.assertEqual(error.name, 'ClientReset'); TestCase.assertDefined(error.config); TestCase.assertNotEqual(error.config.path, ''); - const original_path = realm.path; + const path = realm.path; realm.close(); - Realm.Sync.initiateClientReset(original_path); - // copy required objects from Realm at error.config.path + Realm.Sync.initiateClientReset(path); + // open Realm with error.config, and copy required objects a Realm at `path` resolve(); } catch (e) {