-
Notifications
You must be signed in to change notification settings - Fork 0
/
Try.cs
73 lines (67 loc) · 1.57 KB
/
Try.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
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace InputMaster
{
public static class Try
{
private static bool _fatalExceptionHandled;
/// <summary>
/// Execute the action and capture any exception thrown during the execution.
/// </summary>
/// <param name="action"></param>
public static async Task Execute(Action action)
{
try
{
action();
}
catch (Exception ex)
{
await HandleFatalException(ex);
}
}
public static async Task ExecuteAsync(Func<Task> action)
{
try
{
await action();
}
catch (Exception ex)
{
await HandleFatalException(ex);
}
}
public static async Task HandleFatalException(Exception exception)
{
if (_fatalExceptionHandled)
return;
_fatalExceptionHandled = true;
try
{
Env.Notifier?.LogError(exception.ToString());
}
catch (Exception)
{
// Nothing can be done here as there has already been thrown a fatal exception, and logging does not seem to work.
}
try
{
await Helper.ShowSelectableTextAsync("Fatal error", exception.ToString());
}
catch (Exception)
{
// We are not interested in this exception.
}
Application.Exit();
}
public static Action Wrap(Action action)
{
return async () => { await Execute(action); };
}
public static Func<Task> Wrap(Func<Task> action)
{
return () => { return ExecuteAsync(action); };
}
}
}