From 1ae58780bf4b1e395dd0aa2790d9dfd557ad287c Mon Sep 17 00:00:00 2001 From: blagoev Date: Fri, 12 May 2017 01:40:15 +0300 Subject: [PATCH] Pass the error code and message to user code --- src/js_realm.hpp | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/src/js_realm.hpp b/src/js_realm.hpp index a807ff6f..10930dbd 100644 --- a/src/js_realm.hpp +++ b/src/js_realm.hpp @@ -165,7 +165,7 @@ public: using ObjectDefaultsMap = typename Schema::ObjectDefaultsMap; using ConstructorMap = typename Schema::ConstructorMap; - using WaitHandler = void(); + using WaitHandler = void(std::error_code); static FunctionType create_constructor(ContextType); @@ -567,9 +567,22 @@ void RealmClass::wait_for_download_completion(ContextType ctx, FunctionType, Protected protected_this(ctx, this_object); Protected protected_ctx(Context::get_global_context(ctx)); - EventLoopDispatcher wait_handler([=]() { + EventLoopDispatcher wait_handler([=](std::error_code error_code) { HANDLESCOPE - Function::callback(protected_ctx, protected_callback, protected_this, 0, nullptr); + if (error_code == std::error_code{}) { + //success + Function::callback(protected_ctx, protected_callback, protected_this, 0, nullptr); + } + else { + //fail + ObjectType object = Object::create_empty(protected_ctx); + Object::set_property(protected_ctx, object, "message", Value::from_string(protected_ctx, error_code.message())); + Object::set_property(protected_ctx, object, "errorCode", Value::from_number(protected_ctx, error_code.value())); + + ValueType callback_arguments[1]; + callback_arguments[0] = object; + Function::callback(protected_ctx, protected_callback, protected_this, 1, callback_arguments); + } }); std::function waitFunc = std::move(wait_handler); @@ -579,13 +592,24 @@ void RealmClass::wait_for_download_completion(ContextType ctx, FunctionType, std::shared_ptr user = sync_config->user; if (user && user->state() != SyncUser::State::Error) { - auto session = user->session_for_on_disk_path(config.path); - session->wait_for_download_completion([=](std::error_code error_code) { - realm->config(); - waitFunc(); - }); - return; + if (auto session = user->session_for_on_disk_path(config.path)) + { + session->wait_for_download_completion([=](std::error_code error_code) { + realm->config(); //capture and keep realm instance for till here + waitFunc(error_code); + }); + return; + } } + + ObjectType object = Object::create_empty(protected_ctx); + Object::set_property(protected_ctx, object, "message", Value::from_string(protected_ctx, "Cannot asynchronously open synced Realm, because the associated session previously experienced a fatal error")); + Object::set_property(protected_ctx, object, "errorCode", Value::from_number(protected_ctx, 1)); + + ValueType callback_arguments[1]; + callback_arguments[0] = object; + Function::call(protected_ctx, protected_callback, protected_this, 1, callback_arguments); + return; } }