@@ -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 , Type ? exceptionToIgnore = null )
12+ public static bool IgnoreExceptions ( Action action , ILogger ? logger = null , params Type [ ] ? exceptionsToIgnore )
1313 {
1414 try
1515 {
@@ -18,18 +18,30 @@ public static bool IgnoreExceptions(Action action, ILogger? logger = null, Type?
1818 }
1919 catch ( Exception ex )
2020 {
21- if ( exceptionToIgnore is null || exceptionToIgnore . IsAssignableFrom ( ex . GetType ( ) ) )
21+ bool shouldIgnore = exceptionsToIgnore is null || exceptionsToIgnore . Length == 0 ;
22+ if ( ! shouldIgnore )
2223 {
23- logger ? . LogInformation ( ex , ex . Message ) ;
24+ foreach ( var type in exceptionsToIgnore )
25+ {
26+ if ( type . IsAssignableFrom ( ex . GetType ( ) ) )
27+ {
28+ shouldIgnore = true ;
29+ break ;
30+ }
31+ }
32+ }
2433
34+ if ( shouldIgnore )
35+ {
36+ logger ? . LogInformation ( ex , ex . Message ) ;
2537 return false ;
2638 }
27- else
28- throw ;
39+
40+ throw ;
2941 }
3042 }
3143
32- public static async Task < bool > IgnoreExceptions ( Func < Task > action , ILogger ? logger = null , Type ? exceptionToIgnore = null )
44+ public static async Task < bool > IgnoreExceptions ( Func < Task > action , ILogger ? logger = null , params Type [ ] ? exceptionsToIgnore )
3345 {
3446 try
3547 {
@@ -39,52 +51,88 @@ public static async Task<bool> IgnoreExceptions(Func<Task> action, ILogger? logg
3951 }
4052 catch ( Exception ex )
4153 {
42- if ( exceptionToIgnore is null || exceptionToIgnore . IsAssignableFrom ( ex . GetType ( ) ) )
54+ bool shouldIgnore = exceptionsToIgnore is null || exceptionsToIgnore . Length == 0 ;
55+ if ( ! shouldIgnore )
4356 {
44- logger ? . LogInformation ( ex , ex . Message ) ;
57+ foreach ( var type in exceptionsToIgnore )
58+ {
59+ if ( type . IsAssignableFrom ( ex . GetType ( ) ) )
60+ {
61+ shouldIgnore = true ;
62+ break ;
63+ }
64+ }
65+ }
4566
67+ if ( shouldIgnore )
68+ {
69+ logger ? . LogInformation ( ex , ex . Message ) ;
4670 return false ;
4771 }
48- else
49- throw ;
72+
73+ throw ;
5074 }
5175 }
5276
53- public static T ? IgnoreExceptions < T > ( Func < T > action , ILogger ? logger = null , Type ? exceptionToIgnore = null )
77+ public static T ? IgnoreExceptions < T > ( Func < T > action , ILogger ? logger = null , params Type [ ] ? exceptionsToIgnore )
5478 {
5579 try
5680 {
5781 return action ( ) ;
5882 }
5983 catch ( Exception ex )
6084 {
61- if ( exceptionToIgnore is null || exceptionToIgnore . IsAssignableFrom ( ex . GetType ( ) ) )
85+ bool shouldIgnore = exceptionsToIgnore is null || exceptionsToIgnore . Length == 0 ;
86+ if ( ! shouldIgnore )
6287 {
63- logger ? . LogInformation ( ex , ex . Message ) ;
88+ foreach ( var type in exceptionsToIgnore )
89+ {
90+ if ( type . IsAssignableFrom ( ex . GetType ( ) ) )
91+ {
92+ shouldIgnore = true ;
93+ break ;
94+ }
95+ }
96+ }
6497
98+ if ( shouldIgnore )
99+ {
100+ logger ? . LogInformation ( ex , ex . Message ) ;
65101 return default ;
66102 }
67- else
68- throw ;
103+
104+ throw ;
69105 }
70106 }
71107
72- public static async Task < T ? > IgnoreExceptions < T > ( Func < Task < T > > action , ILogger ? logger = null , Type ? exceptionToIgnore = null )
108+ public static async Task < T ? > IgnoreExceptions < T > ( Func < Task < T > > action , ILogger ? logger = null , params Type [ ] ? exceptionsToIgnore )
73109 {
74110 try
75111 {
76112 return await action ( ) ;
77113 }
78114 catch ( Exception ex )
79115 {
80- if ( exceptionToIgnore is null || exceptionToIgnore . IsAssignableFrom ( ex . GetType ( ) ) )
116+ bool shouldIgnore = exceptionsToIgnore is null || exceptionsToIgnore . Length == 0 ;
117+ if ( ! shouldIgnore )
81118 {
82- logger ? . LogInformation ( ex , ex . Message ) ;
119+ foreach ( var type in exceptionsToIgnore )
120+ {
121+ if ( type . IsAssignableFrom ( ex . GetType ( ) ) )
122+ {
123+ shouldIgnore = true ;
124+ break ;
125+ }
126+ }
127+ }
83128
129+ if ( shouldIgnore )
130+ {
131+ logger ? . LogInformation ( ex , ex . Message ) ;
84132 return default ;
85133 }
86- else
87- throw ;
134+
135+ throw ;
88136 }
89137 }
90138
0 commit comments