|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.Collections.Immutable; |
3 | 4 | using Microsoft.AspNetCore.Builder; |
4 | 5 | using Microsoft.AspNetCore.Diagnostics; |
5 | 6 | using Microsoft.AspNetCore.Http; |
@@ -45,14 +46,20 @@ internal static IApplicationBuilder UseExceptionHandlerMiddleware(this IApplicat |
45 | 46 |
|
46 | 47 | var options = httpContext.RequestServices.GetRequiredService<IOptions<ErrorHandlerOptions>>(); |
47 | 48 |
|
48 | | - IEnumerable<KeyValuePair<string, ErrorDetails>> errors; |
49 | | - (httpContext.Response.StatusCode, errors) = exception.MappingToStatusCode(options.Value); |
50 | | - |
51 | | - httpContext.ResetResponse(httpContext.Response.StatusCode); |
52 | | - problemDetails = problemDetailsFactory.Create(httpContext, errors); |
53 | | - |
| 49 | + try |
| 50 | + { |
| 51 | + IEnumerable<KeyValuePair<string, ErrorDetails>> errors; |
| 52 | + (httpContext.Response.StatusCode, errors) = exception.MappingToStatusCode(options.Value); |
| 53 | + httpContext.ResetResponse(httpContext.Response.StatusCode); |
| 54 | + problemDetails = problemDetailsFactory.Create(httpContext, errors); |
54 | 55 |
|
55 | | - logger.Error(exception, problemDetails.Instance, problemDetails.Status); |
| 56 | + logger.Error(exception, problemDetails.Instance, problemDetails.Status); |
| 57 | + } |
| 58 | + catch(Exception mappingException) |
| 59 | + { |
| 60 | + problemDetails = problemDetailsFactory.Create(httpContext, ImmutableDictionary<string, ErrorDetails>.Empty); |
| 61 | + logger.Error(mappingException, "Error mapping exception"); |
| 62 | + } |
56 | 63 | } |
57 | 64 |
|
58 | 65 | // Write error details in body response |
|
0 commit comments