Пример #1
0
        public async Task handleErrors()
        {
            // :code-block-start: set-log-level
            var appConfig = new AppConfiguration(myRealmAppId)
            {
                //LogLevel = LogLevel.Debug,
                // :hide-start:
                DefaultRequestTimeout = TimeSpan.FromMilliseconds(1500)
                                        // :hide-end:
            };

            // :code-block-end:
            app  = App.Create(appConfig);
            user = await app.LogInAsync(Credentials.Anonymous());

            config = new SyncConfiguration("myPartition", user);
            //:hide-start:
            config.ObjectClasses = new[]
            {
                typeof(dotnet.User)
            };
            //:hide-end:
            var realm = await Realm.GetInstanceAsync(config);

            // :code-block-start: handle-errors
            Session.Error += (session, errorArgs) =>
            {
                var sessionException = (SessionException)errorArgs.Exception;
                switch (sessionException.ErrorCode)
                {
                case ErrorCode.AccessTokenExpired:
                case ErrorCode.BadUserAuthentication:
                    // Ask user for credentials
                    break;

                case ErrorCode.PermissionDenied:
                    // Tell the user they don't have permissions to work with that Realm
                    // :hide-start:
                    didTriggerErrorHandler = true;
                    // :hide-end:
                    break;

                case ErrorCode.Unknown:
                    // Likely the app version is too old, prompt for update
                    break;
                    // ...
                }
            };
            // :code-block-end:
            TestingExtensions.SimulateError(realm.GetSession(),
                                            ErrorCode.PermissionDenied, "No permission to work with the Realm", false);

            // Close the Realm before doing the reset as it'll need
            // to be deleted and all objects obtained from it will be
            // invalidated.
            realm.Dispose();

            Assert.IsTrue(didTriggerErrorHandler);
        }
Пример #2
0
        public async Task resetsTheClient()
        {
            app = App.Create(myRealmAppId);

            user = app.LogInAsync(Credentials.EmailPassword("*****@*****.**", "foobar")).Result;

            config = new SyncConfiguration("myPart", user);
            //:hide-start:
            config.ObjectClasses = new[]
            {
                typeof(dotnet.User)
            };
            //:hide-end:
            var realm = await Realm.GetInstanceAsync(config);

            // :code-block-start: handle
            Session.Error += (sender, err) =>
            {
                if (err.Exception is ClientResetException clientResetEx)
                {
                    var session = (Session)sender;
                    Console.WriteLine("Client Reset requested for " +
                                      session.Path + "due to " + clientResetEx.Message);

                    // Prompt user to perform client reset immediately. If they don't do it,
                    // they won't receive any data from the server until they restart the app
                    // and all changes they make will be discarded when the app restarts.
                    var didUserConfirmReset = true;
                    if (didUserConfirmReset)
                    {
                        // Close the Realm before doing the reset as it'll need
                        // to be deleted and all objects obtained from it will be
                        // invalidated.
                        realm.Dispose();
                        var didReset = clientResetEx.InitiateClientReset();
                        if (didReset)
                        {
                            // Navigate the user back to the main page or reopen the
                            // the Realm and reinitialize the current page.
                        }
                        else
                        {
                            // Reset failed - notify user that they'll need to restart the app.
                        }
                        // :hide-start:
                        Assert.IsTrue(didReset);
                        // :hide-end:
                    }
                }
            };
            // :code-block-end:
            TestingExtensions.SimulateError(realm.GetSession(),
                                            ErrorCode.DivergingHistories, "diverging histories!", false);
        }