@@ -9,7 +9,7 @@ namespace Files.Shared.Extensions
99{
1010 public static class SafetyExtensions
1111 {
12- public static bool IgnoreExceptions ( Action action , ILogger ? logger = null )
12+ public static bool IgnoreExceptions ( Action action , ILogger ? logger = null , Type ? exceptionToIgnore = null )
1313 {
1414 try
1515 {
@@ -18,13 +18,18 @@ public static bool IgnoreExceptions(Action action, ILogger? logger = null)
1818 }
1919 catch ( Exception ex )
2020 {
21- logger ? . LogInformation ( ex , ex . Message ) ;
21+ if ( exceptionToIgnore is null || exceptionToIgnore . IsAssignableFrom ( ex . GetType ( ) ) )
22+ {
23+ logger ? . LogInformation ( ex , ex . Message ) ;
2224
23- return false ;
25+ return false ;
26+ }
27+ else
28+ throw ;
2429 }
2530 }
2631
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 )
2833 {
2934 try
3035 {
@@ -34,37 +39,52 @@ public static async Task<bool> IgnoreExceptions(Func<Task> action, ILogger? logg
3439 }
3540 catch ( Exception ex )
3641 {
37- logger ? . LogInformation ( ex , ex . Message ) ;
42+ if ( exceptionToIgnore is null || exceptionToIgnore . IsAssignableFrom ( ex . GetType ( ) ) )
43+ {
44+ logger ? . LogInformation ( ex , ex . Message ) ;
3845
39- return false ;
46+ return false ;
47+ }
48+ else
49+ throw ;
4050 }
4151 }
4252
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 )
4454 {
4555 try
4656 {
4757 return action ( ) ;
4858 }
4959 catch ( Exception ex )
5060 {
51- logger ? . LogInformation ( ex , ex . Message ) ;
61+ if ( exceptionToIgnore is null || exceptionToIgnore . IsAssignableFrom ( ex . GetType ( ) ) )
62+ {
63+ logger ? . LogInformation ( ex , ex . Message ) ;
5264
53- return default ;
65+ return default ;
66+ }
67+ else
68+ throw ;
5469 }
5570 }
5671
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 )
5873 {
5974 try
6075 {
6176 return await action ( ) ;
6277 }
6378 catch ( Exception ex )
6479 {
65- logger ? . LogInformation ( ex , ex . Message ) ;
80+ if ( exceptionToIgnore is null || exceptionToIgnore . IsAssignableFrom ( ex . GetType ( ) ) )
81+ {
82+ logger ? . LogInformation ( ex , ex . Message ) ;
6683
67- return default ;
84+ return default ;
85+ }
86+ else
87+ throw ;
6888 }
6989 }
7090
0 commit comments