@@ -11,35 +11,34 @@ namespace Commmunity.AspNetCore.ExceptionHandling
1111{
1212 public static class PolicyBuilderExtensions
1313 {
14- public static ExceptionMapping < TException > For < TException > (
14+ public static IExceptionMapping < TException > For < TException > (
1515 this IExceptionPolicyBuilder builder , int index = - 1 ) where TException : Exception
1616 {
1717 builder . Options . EnsureException ( typeof ( TException ) , index ) ;
1818 return new ExceptionMapping < TException > ( builder ) ;
1919 }
2020
21- public static ExceptionMapping < TException > EnsureHandler < TException , THandler > (
22- this ExceptionMapping < TException > builder , int index = - 1 )
21+ public static void EnsureHandler < TException , THandler > (
22+ this IExceptionPolicyBuilder builder , int index = - 1 )
2323 where THandler : class , IExceptionHandler
2424 where TException : Exception
2525
2626 {
2727 builder . Options . Value . EnsureHandler ( typeof ( TException ) , typeof ( THandler ) , index ) ;
2828 builder . Services . TryAddSingleton < THandler > ( ) ;
29- return builder ;
3029 }
3130
32- public static ExceptionMapping < TException > RemoveHandler < TException , THandler > (
33- this ExceptionMapping < TException > builder )
31+ public static IExceptionMapping < TException > RemoveHandler < TException , THandler > (
32+ this IExceptionMapping < TException > builder )
3433 where THandler : IExceptionHandler
3534 where TException : Exception
3635 {
3736 builder . Options . Value . RemoveHandler ( typeof ( TException ) , typeof ( THandler ) ) ;
3837 return builder ;
3938 }
4039
41- public static ExceptionMapping < TException > Clear < TException > (
42- this ExceptionMapping < TException > builder )
40+ public static IExceptionMapping < TException > Clear < TException > (
41+ this IExceptionMapping < TException > builder )
4342 where TException : Exception
4443 {
4544 builder . Options . Value . ClearHandlers ( typeof ( TException ) ) ;
@@ -48,39 +47,44 @@ public static ExceptionMapping<TException> Clear<TException>(
4847
4948 // rethrow
5049 public static IExceptionPolicyBuilder AddRethrow < TException > (
51- this ExceptionMapping < TException > builder , int index = - 1 )
50+ this IExceptionMapping < TException > builder , int index = - 1 )
5251 where TException : Exception
53- {
54- return builder . EnsureHandler < TException , ReThrowExceptionHandler > ( index ) ;
52+ {
53+ builder . EnsureHandler < TException , ReThrowExceptionHandler > ( index ) ;
54+ return builder ;
5555 }
5656
5757 // Log
58- public static ExceptionMapping < TException > AddLog < TException > (
59- this ExceptionMapping < TException > builder , Action < LogHandlerOptions < TException > > settings = null , int index = - 1 )
58+ public static IExceptionMapping < TException > AddLog < TException > (
59+ this IExceptionMapping < TException > builder , Action < LogHandlerOptions < TException > > settings = null , int index = - 1 )
6060 where TException : Exception
6161 {
6262 LogHandlerOptions < TException > options = new LogHandlerOptions < TException > ( ) ;
6363 settings ? . Invoke ( options ) ;
6464 builder . Services . TryAddSingleton ( options ) ;
65- return builder . EnsureHandler < TException , LogExceptionHandler < TException > > ( index ) ;
65+ builder . EnsureHandler < TException , LogExceptionHandler < TException > > ( index ) ;
66+
67+ return builder ;
6668 }
6769
6870 // Set status code
69- public static ExceptionMapping < TException > AddStatusCode < TException > (
70- this ExceptionMapping < TException > builder , Func < TException , int > settings = null , int index = - 1 )
71+ public static IResponseHandlers < TException > AddNewResponse < TException > (
72+ this IExceptionMapping < TException > builder , Func < TException , int > statusCodeFactory = null , int index = - 1 )
7173 where TException : Exception
7274 {
73- if ( settings != null )
75+ if ( statusCodeFactory != null )
7476 {
75- builder . Services . Configure < SetStatusCodeOptions < TException > > ( codeOptions =>
76- codeOptions . StatusCodeFactory = settings ) ;
77+ builder . Services . Configure < NewResponseOptions < TException > > ( codeOptions =>
78+ codeOptions . StatusCodeFactory = statusCodeFactory ) ;
7779 }
78-
79- return builder . EnsureHandler < TException , SetStatusCodeHandler < TException > > ( index ) ;
80+
81+ builder . EnsureHandler < TException , NewResponseHandler < TException > > ( index ) ;
82+
83+ return builder as IResponseHandlers < TException > ;
8084 }
8185
82- public static ExceptionMapping < TException > AddHeaders < TException > (
83- this ExceptionMapping < TException > builder , Action < IHeaderDictionary , TException > settings = null , int index = - 1 )
86+ public static IResponseHandlers < TException > WithHeaders < TException > (
87+ this IResponseHandlers < TException > builder , Action < IHeaderDictionary , TException > settings = null , int index = - 1 )
8488 where TException : Exception
8589 {
8690 if ( settings != null )
@@ -89,7 +93,9 @@ public static ExceptionMapping<TException> AddHeaders<TException>(
8993 codeOptions . SetHeadersAction = settings ) ;
9094 }
9195
92- return builder . EnsureHandler < TException , SetHeadersHandler < TException > > ( index ) ;
96+ builder . EnsureHandler < TException , SetHeadersHandler < TException > > ( index ) ;
97+
98+ return builder ;
9399 }
94100 }
95101}
0 commit comments