@@ -9,7 +9,7 @@ namespace Files.Shared.Extensions
9
9
{
10
10
public static class SafetyExtensions
11
11
{
12
- public static bool IgnoreExceptions ( Action action , ILogger ? logger = null )
12
+ public static bool IgnoreExceptions ( Action action , ILogger ? logger = null , Type ? exceptionToIgnore = null )
13
13
{
14
14
try
15
15
{
@@ -18,13 +18,18 @@ public static bool IgnoreExceptions(Action action, ILogger? logger = null)
18
18
}
19
19
catch ( Exception ex )
20
20
{
21
- logger ? . LogInformation ( ex , ex . Message ) ;
21
+ if ( exceptionToIgnore is null || exceptionToIgnore . IsAssignableFrom ( ex . GetType ( ) ) )
22
+ {
23
+ logger ? . LogInformation ( ex , ex . Message ) ;
22
24
23
- return false ;
25
+ return false ;
26
+ }
27
+ else
28
+ throw ;
24
29
}
25
30
}
26
31
27
- public static async Task < bool > IgnoreExceptions ( Func < Task > action , ILogger ? logger = null )
32
+ public static async Task < bool > IgnoreExceptions ( Func < Task > action , ILogger ? logger = null , Type ? exceptionToIgnore = null )
28
33
{
29
34
try
30
35
{
@@ -34,37 +39,52 @@ public static async Task<bool> IgnoreExceptions(Func<Task> action, ILogger? logg
34
39
}
35
40
catch ( Exception ex )
36
41
{
37
- logger ? . LogInformation ( ex , ex . Message ) ;
42
+ if ( exceptionToIgnore is null || exceptionToIgnore . IsAssignableFrom ( ex . GetType ( ) ) )
43
+ {
44
+ logger ? . LogInformation ( ex , ex . Message ) ;
38
45
39
- return false ;
46
+ return false ;
47
+ }
48
+ else
49
+ throw ;
40
50
}
41
51
}
42
52
43
- public static T ? IgnoreExceptions < T > ( Func < T > action , ILogger ? logger = null )
53
+ public static T ? IgnoreExceptions < T > ( Func < T > action , ILogger ? logger = null , Type ? exceptionToIgnore = null )
44
54
{
45
55
try
46
56
{
47
57
return action ( ) ;
48
58
}
49
59
catch ( Exception ex )
50
60
{
51
- logger ? . LogInformation ( ex , ex . Message ) ;
61
+ if ( exceptionToIgnore is null || exceptionToIgnore . IsAssignableFrom ( ex . GetType ( ) ) )
62
+ {
63
+ logger ? . LogInformation ( ex , ex . Message ) ;
52
64
53
- return default ;
65
+ return default ;
66
+ }
67
+ else
68
+ throw ;
54
69
}
55
70
}
56
71
57
- public static async Task < T ? > IgnoreExceptions < T > ( Func < Task < T > > action , ILogger ? logger = null )
72
+ public static async Task < T ? > IgnoreExceptions < T > ( Func < Task < T > > action , ILogger ? logger = null , Type ? exceptionToIgnore = null )
58
73
{
59
74
try
60
75
{
61
76
return await action ( ) ;
62
77
}
63
78
catch ( Exception ex )
64
79
{
65
- logger ? . LogInformation ( ex , ex . Message ) ;
80
+ if ( exceptionToIgnore is null || exceptionToIgnore . IsAssignableFrom ( ex . GetType ( ) ) )
81
+ {
82
+ logger ? . LogInformation ( ex , ex . Message ) ;
66
83
67
- return default ;
84
+ return default ;
85
+ }
86
+ else
87
+ throw ;
68
88
}
69
89
}
70
90
0 commit comments