-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathErrorHandler.cs
96 lines (85 loc) · 3.12 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Threading.Tasks;
using NUnit.Framework;
using Realms;
using Realms.Sync;
using Realms.Sync.Exceptions;
using Realms.Sync.Testing;
using Realms.Logging;
using System.Threading;
namespace Examples
{
public class ErrorHandler
{
App app;
User user;
PartitionSyncConfiguration config;
string myRealmAppId = Config.AppId;
[Test]
public async Task HandleErrors()
{
// :snippet-start: set-log-level
Logger.LogLevel = LogLevel.Debug;
// :snippet-end:
// :snippet-start: customize-logging-function
// :uncomment-start:
//using Realms.Logging;
//Logger.LogLevel = LogLevel.All;
// :uncomment-end:
// customize the logging function:
Logger.Default = Logger.Function(message =>
{
// Do something with the message
});
// :snippet-end:
var appConfig = new AppConfiguration(myRealmAppId)
{
DefaultRequestTimeout = TimeSpan.FromMilliseconds(1500)
};
app = App.Create(appConfig);
user = await app.LogInAsync(Credentials.Anonymous());
config = new PartitionSyncConfiguration("myPartition", user);
//:remove-start:
config.Schema = new[] { typeof(Models.User) };
//:remove-end:
var realm = await Realm.GetInstanceAsync(config);
// :snippet-start:handle-errors
config.OnSessionError = (session, sessionException) =>
{
switch (sessionException.ErrorCode)
{
// See https://www.mongodb.com/docs/realm-sdks/dotnet/latest/reference/Realms.Sync.Exceptions.ErrorCode.html
// for a list of all error codes
case ErrorCode.BadQuery:
break;
}
};
// :snippet-end:
TestingExtensions.SimulateError(realm.SyncSession, ErrorCode.BadQuery, "" +
"No permission to work with the Realm");
// 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();
//failing on build server. comment out to test.
//Assert.IsTrue(didTriggerErrorHandler);
}
public async Task UseCancellationToken()
{
var appConfig = new AppConfiguration(Config.FSAppId);
app = App.Create(appConfig);
user = await app.LogInAsync(Credentials.Anonymous());
var syncConfig = new FlexibleSyncConfiguration(user);
try
{
var cts = new CancellationTokenSource(TimeSpan.FromMinutes(2));
await Realm.GetInstanceAsync(syncConfig, cts.Token);
}
catch (OperationCanceledException)
{
Realm.GetInstance(syncConfig);
}
catch (Exception ex) { Console.WriteLine(ex.Message); }
}
}
}