-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTryCatchResult.cs
More file actions
54 lines (52 loc) · 1.31 KB
/
TryCatchResult.cs
File metadata and controls
54 lines (52 loc) · 1.31 KB
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
using System;
using System.Threading.Tasks;
using UnityEngine;
public static class TryCatchResult
{
public static Result<T> Try<T>(Func<T> action)
{
try
{
return Result<T>.Success(action());
}
catch (Exception e)
{
return Result<T>.Failure(new ErrorDetails(e.GetType().Name, e.Message));
}
}
public static Result<Success> Try(Action action)
{
try
{
action();
return Result<Success>.Success(new Success());
}
catch (Exception e)
{
return Result<Success>.Failure(new ErrorDetails(e.GetType().Name, e.Message));
}
}
public static async Awaitable<Result<T>> ToResult<T>(this Task<T> task)
{
try
{
return Result<T>.Success(await task);
}
catch (Exception e)
{
return Result<T>.Failure(new ErrorDetails(e.GetType().Name, e.Message));
}
}
public static async Awaitable<Result<Success>> ToResult(this Task task)
{
try
{
await task;
return Result<Success>.Success(new Success());
}
catch (Exception e)
{
return Result<Success>.Failure(new ErrorDetails(e.GetType().Name, e.Message));
}
}
}