99namespace Dibix . Http . Host
1010{
1111 // Map custom HTTP status codes to response
12- internal sealed class HttpRequestExecutionExceptionHandler : IExceptionHandler
12+ internal class HttpRequestExecutionExceptionHandler : IExceptionHandler
1313 {
1414 private readonly IProblemDetailsService _problemDetailsService ;
1515
@@ -18,8 +18,25 @@ public HttpRequestExecutionExceptionHandler(IProblemDetailsService problemDetail
1818 _problemDetailsService = problemDetailsService ;
1919 }
2020
21- public ValueTask < bool > TryHandleAsync ( HttpContext httpContext , Exception exception , CancellationToken cancellationToken ) => TryHandleWithProblemDetails ( httpContext , exception ) ;
22-
21+ public virtual ValueTask < bool > TryHandleAsync ( HttpContext httpContext , Exception exception , CancellationToken cancellationToken ) => TryHandleWithProblemDetails ( httpContext , exception ) ;
22+
23+ protected static bool HandleHttpRequestExecutionException ( HttpContext httpContext , HttpRequestExecutionException httpRequestExecutionException )
24+ {
25+ httpContext . Response . StatusCode = ( int ) httpRequestExecutionException . StatusCode ;
26+
27+ // For compatibility reasons
28+ // TODO: Remove, once problem details are stabilized
29+ httpRequestExecutionException . AppendToResponse ( httpContext . Response ) ;
30+
31+ // Note: The ExceptionHandlerMiddleware will log the exception even if the handler returns true
32+ // See: https://github.com/dotnet/aspnetcore/issues/54554
33+ // If client exceptions should not be logged, we can do the following:
34+ // - Implement a custom IExceptionHandler
35+ // - Use LogError with a custom log category (i.E. the name of the handler, something like 'HttpStatusCodeClientExceptionHandler')
36+ // - Set this category's log level to 'Critical' by default, to avoid logging client exceptions
37+ return true ;
38+ }
39+
2340 // TODO: Instead of writing problem details manually, use the StatusCodeSelector introduced in .NET 9
2441 // See: https://www.milanjovanovic.tech/blog/problem-details-for-aspnetcore-apis#handling-specific-exceptions-status-codes
2542 private async ValueTask < bool > TryHandleWithProblemDetails ( HttpContext httpContext , Exception exception )
@@ -41,19 +58,7 @@ private static bool TryHandle(HttpContext httpContext, Exception exception)
4158 if ( exception is not HttpRequestExecutionException httpRequestExecutionException )
4259 return false ;
4360
44- httpContext . Response . StatusCode = ( int ) httpRequestExecutionException . StatusCode ;
45-
46- // For compatibility reasons
47- // TODO: Remove, once problem details are stabilized
48- httpRequestExecutionException . AppendToResponse ( httpContext . Response ) ;
49-
50- // Note: The ExceptionHandlerMiddleware will log the exception even if the handler returns true
51- // See: https://github.com/dotnet/aspnetcore/issues/54554
52- // If client exceptions should not be logged, we can do the following:
53- // - Implement a custom IExceptionHandler
54- // - Use LogError with a custom log category (i.E. the name of the handler, something like 'HttpStatusCodeClientExceptionHandler')
55- // - Set this category's log level to 'Critical' by default, to avoid logging client exceptions
56- return true ;
61+ return HandleHttpRequestExecutionException ( httpContext , httpRequestExecutionException ) ;
5762 }
5863 }
5964}
0 commit comments