-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathErrorHandler.cs
70 lines (65 loc) · 2.41 KB
/
ErrorHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Threading.Tasks;
using NUnit.Framework;
using Realms;
using Realms.Sync;
using Realms.Sync.Exceptions;
using Realms.Sync.Testing;
namespace Examples
{
public class ErrorHandler
{
App app;
Realms.Sync.User user;
SyncConfiguration config;
bool didTriggerErrorHandler;
string myRealmAppId = Config.appid;
[Test]
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);
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);
}
}
}