@@ -22,17 +22,19 @@ public static partial class GuardClauseExtensions
2222 /// <param name="input"></param>
2323 /// <param name="parameterName"></param>
2424 /// <param name="message">Optional. Custom error message</param>
25- /// <param name="exception "></param>
25+ /// <param name="exceptionCreator "></param>
2626 /// <returns><paramref name="input" /> if the value is not null.</returns>
2727 /// <exception cref="Exception"></exception>
2828 public static T Null < T > ( this IGuardClause guardClause ,
2929 [ NotNull ] [ ValidatedNotNull ] T ? input ,
3030 [ CallerArgumentExpression ( "input" ) ] string ? parameterName = null ,
3131 string ? message = null ,
32- Exception ? exception = null )
32+ Func < Exception > ? exceptionCreator = null )
3333 {
3434 if ( input is null )
3535 {
36+ Exception ? exception = exceptionCreator ? . Invoke ( ) ;
37+
3638 if ( string . IsNullOrEmpty ( message ) )
3739 {
3840 throw exception ?? new ArgumentNullException ( parameterName ) ;
@@ -51,17 +53,19 @@ public static T Null<T>(this IGuardClause guardClause,
5153 /// <param name="input"></param>
5254 /// <param name="parameterName"></param>
5355 /// <param name="message">Optional. Custom error message</param>
54- /// <param name="exception "></param>
56+ /// <param name="exceptionCreator "></param>
5557 /// <returns><paramref name="input" /> if the value is not null.</returns>
5658 /// <exception cref="Exception"></exception>
5759 public static T Null < T > ( this IGuardClause guardClause ,
5860 [ NotNull ] [ ValidatedNotNull ] T ? input ,
5961 [ CallerArgumentExpression ( "input" ) ] string ? parameterName = null ,
6062 string ? message = null ,
61- Exception ? exception = null ) where T : struct
63+ Func < Exception > ? exceptionCreator = null ) where T : struct
6264 {
6365 if ( input is null )
6466 {
67+ Exception ? exception = exceptionCreator ? . Invoke ( ) ;
68+
6569 if ( string . IsNullOrEmpty ( message ) )
6670 {
6771 throw exception ?? new ArgumentNullException ( parameterName ) ;
@@ -80,7 +84,7 @@ public static T Null<T>(this IGuardClause guardClause,
8084 /// <param name="input"></param>
8185 /// <param name="parameterName"></param>
8286 /// <param name="message">Optional. Custom error message</param>
83- /// <param name="exception "></param>
87+ /// <param name="exceptionCreator "></param>
8488 /// <returns><paramref name="input" /> if the value is not an empty string or null.</returns>
8589 /// <exception cref="ArgumentNullException"></exception>
8690 /// <exception cref="ArgumentException"></exception>
@@ -89,12 +93,13 @@ public static string NullOrEmpty(this IGuardClause guardClause,
8993 [ NotNull ] [ ValidatedNotNull ] string ? input ,
9094 [ CallerArgumentExpression ( "input" ) ] string ? parameterName = null ,
9195 string ? message = null ,
92- Exception ? exception = null )
96+ Func < Exception > ? exceptionCreator = null )
9397 {
94- Guard . Against . Null ( input , parameterName , message , exception ) ;
98+ Guard . Against . Null ( input , parameterName , message , exceptionCreator ) ;
9599 if ( input == string . Empty )
96100 {
97- throw exception ?? new ArgumentException ( message ?? $ "Required input { parameterName } was empty.", parameterName ) ;
101+ throw exceptionCreator ? . Invoke ( ) ??
102+ new ArgumentException ( message ?? $ "Required input { parameterName } was empty.", parameterName ) ;
98103 }
99104
100105 return input ;
@@ -108,20 +113,22 @@ public static string NullOrEmpty(this IGuardClause guardClause,
108113 /// <param name="input"></param>
109114 /// <param name="parameterName"></param>
110115 /// <param name="message">Optional. Custom error message</param>
111- /// <param name="exception "></param>
116+ /// <param name="exceptionCreator "></param>
112117 /// <returns><paramref name="input" /> if the value is not an empty guid or null.</returns>
113118 /// <exception cref="ArgumentNullException"></exception>
114119 /// <exception cref="ArgumentException"></exception>
115120 /// <exception cref="Exception"></exception>
116121 public static Guid NullOrEmpty ( this IGuardClause guardClause ,
117122 [ NotNull ] [ ValidatedNotNull ] Guid ? input ,
118123 [ CallerArgumentExpression ( "input" ) ] string ? parameterName = null ,
119- string ? message = null , Exception ? exception = null )
124+ string ? message = null ,
125+ Func < Exception > ? exceptionCreator = null )
120126 {
121- Guard . Against . Null ( input , parameterName , message , exception ) ;
127+ Guard . Against . Null ( input , parameterName , message , exceptionCreator ) ;
122128 if ( input == Guid . Empty )
123129 {
124- throw exception ?? new ArgumentException ( message ?? $ "Required input { parameterName } was empty.", parameterName ) ;
130+ throw exceptionCreator ? . Invoke ( ) ??
131+ new ArgumentException ( message ?? $ "Required input { parameterName } was empty.", parameterName ) ;
125132 }
126133
127134 return input . Value ;
@@ -135,25 +142,27 @@ public static Guid NullOrEmpty(this IGuardClause guardClause,
135142 /// <param name="input"></param>
136143 /// <param name="parameterName"></param>
137144 /// <param name="message">Optional. Custom error message</param>
138- /// <param name="exception "></param>
145+ /// <param name="exceptionCreator "></param>
139146 /// <returns><paramref name="input" /> if the value is not an empty enumerable or null.</returns>
140147 /// <exception cref="ArgumentNullException"></exception>
141148 /// <exception cref="ArgumentException"></exception>
142149 /// <exception cref="Exception"></exception>
143150 public static IEnumerable < T > NullOrEmpty < T > ( this IGuardClause guardClause ,
144151 [ NotNull ] [ ValidatedNotNull ] IEnumerable < T > ? input ,
145152 [ CallerArgumentExpression ( "input" ) ] string ? parameterName = null ,
146- string ? message = null , Exception ? exception = null )
153+ string ? message = null ,
154+ Func < Exception > ? exceptionCreator = null )
147155 {
148- Guard . Against . Null ( input , parameterName , message , exception ) ;
156+ Guard . Against . Null ( input , parameterName , message , exceptionCreator : exceptionCreator ) ;
149157
150158 if ( input is Array and { Length : 0 } //Try checking first with pattern matching because it's faster than TryGetNonEnumeratedCount on Array
151159#if NET6_0_OR_GREATER
152160 || ( input . TryGetNonEnumeratedCount ( out var count ) && count == 0 )
153161#endif
154162 || ! input . Any ( ) )
155163 {
156- throw exception ?? new ArgumentException ( message ?? $ "Required input { parameterName } was empty.", parameterName ) ;
164+ throw exceptionCreator ? . Invoke ( ) ??
165+ new ArgumentException ( message ?? $ "Required input { parameterName } was empty.", parameterName ) ;
157166 }
158167
159168 return input ;
@@ -167,20 +176,22 @@ public static IEnumerable<T> NullOrEmpty<T>(this IGuardClause guardClause,
167176 /// <param name="input"></param>
168177 /// <param name="parameterName"></param>
169178 /// <param name="message">Optional. Custom error message</param>
170- /// <param name="exception "></param>
179+ /// <param name="exceptionCreator "></param>
171180 /// <returns><paramref name="input" /> if the value is not an empty or whitespace string.</returns>
172181 /// <exception cref="ArgumentNullException"></exception>
173182 /// <exception cref="ArgumentException"></exception>
174183 /// <exception cref="Exception"></exception>
175184 public static string NullOrWhiteSpace ( this IGuardClause guardClause ,
176185 [ NotNull ] [ ValidatedNotNull ] string ? input ,
177186 [ CallerArgumentExpression ( "input" ) ] string ? parameterName = null ,
178- string ? message = null , Exception ? exception = null )
187+ string ? message = null ,
188+ Func < Exception > ? exceptionCreator = null )
179189 {
180- Guard . Against . NullOrEmpty ( input , parameterName , message , exception ) ;
190+ Guard . Against . NullOrEmpty ( input , parameterName , message , exceptionCreator ) ;
181191 if ( String . IsNullOrWhiteSpace ( input ) )
182192 {
183- throw exception ?? new ArgumentException ( message ?? $ "Required input { parameterName } was empty.", parameterName ) ;
193+ throw exceptionCreator ? . Invoke ( ) ??
194+ new ArgumentException ( message ?? $ "Required input { parameterName } was empty.", parameterName ) ;
184195 }
185196
186197 return input ;
@@ -193,19 +204,20 @@ public static string NullOrWhiteSpace(this IGuardClause guardClause,
193204 /// <param name="input"></param>
194205 /// <param name="parameterName"></param>
195206 /// <param name="message">Optional. Custom error message</param>
196- /// <param name="exception "></param>
207+ /// <param name="exceptionCreator "></param>
197208 /// <returns><paramref name="input" /> if the value is not default for that type.</returns>
198209 /// <exception cref="ArgumentException"></exception>
199210 /// <exception cref="Exception"></exception>
200211 public static T Default < T > ( this IGuardClause guardClause ,
201212 [ AllowNull , NotNull ] T input ,
202213 [ CallerArgumentExpression ( "input" ) ] string ? parameterName = null ,
203214 string ? message = null ,
204- Exception ? exception = null )
215+ Func < Exception > ? exceptionCreator = null )
205216 {
206217 if ( EqualityComparer < T > . Default . Equals ( input , default ( T ) ! ) || input is null )
207218 {
208- throw exception ?? new ArgumentException ( message ?? $ "Parameter [{ parameterName } ] is default value for type { typeof ( T ) . Name } ", parameterName ) ;
219+ throw exceptionCreator ? . Invoke ( ) ??
220+ new ArgumentException ( message ?? $ "Parameter [{ parameterName } ] is default value for type { typeof ( T ) . Name } ", parameterName ) ;
209221 }
210222
211223 return input ;
@@ -221,7 +233,7 @@ public static T Default<T>(this IGuardClause guardClause,
221233 /// <param name="parameterName"></param>
222234 /// <param name="predicate"></param>
223235 /// <param name="message">Optional. Custom error message</param>
224- /// <param name="exception "></param>
236+ /// <param name="exceptionCreator "></param>
225237 /// <typeparam name="T"></typeparam>
226238 /// <returns></returns>
227239 /// <exception cref="ArgumentException"></exception>
@@ -232,10 +244,10 @@ public static T NullOrInvalidInput<T>(this IGuardClause guardClause,
232244 string parameterName ,
233245 Func < T , bool > predicate ,
234246 string ? message = null ,
235- Exception ? exception = null )
247+ Func < Exception > ? exceptionCreator = null )
236248 {
237- Guard . Against . Null ( input , parameterName , message , exception ) ;
249+ Guard . Against . Null ( input , parameterName , message , exceptionCreator : exceptionCreator ) ;
238250
239- return Guard . Against . InvalidInput ( input , parameterName , predicate , message , exception ) ;
251+ return Guard . Against . InvalidInput ( input , parameterName , predicate , message , exceptionCreator ? . Invoke ( ) ) ;
240252 }
241253}
0 commit comments