-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathExceptionHandlingPolicyMiddleware.cs
192 lines (159 loc) · 7.26 KB
/
ExceptionHandlingPolicyMiddleware.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Community.AspNetCore.ExceptionHandling.Handlers;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
namespace Community.AspNetCore.ExceptionHandling
{
public class ExceptionHandlingPolicyMiddleware : IMiddleware
{
public const int MaxRetryIterations = 10;
private readonly IOptions<ExceptionHandlingPolicyOptions> options;
public ExceptionHandlingPolicyMiddleware(IOptions<ExceptionHandlingPolicyOptions> options)
{
this.options = options ?? throw new ArgumentNullException(nameof(options));
}
private static async Task<HandlerResult> EnumerateExceptionMapping(
HttpContext context,
ExceptionHandlingPolicyOptions policyOptions,
Exception exception,
ILogger logger)
{
Type exceptionType = exception.GetType();
bool mappingExists = false;
HandlerResult handleResult = HandlerResult.ReThrow;
foreach (Type type in policyOptions.GetExceptionsInternal())
{
if (type.IsAssignableFrom(exceptionType))
{
mappingExists = true;
handleResult = await EnumerateHandlers(context, type, exception, policyOptions, logger);
if (handleResult != HandlerResult.NextChain)
{
break;
}
}
}
if (!mappingExists)
{
logger.LogWarning(Events.PolicyNotFound,
"Handlers mapping for exception type {exceptionType} not exists. Exception will be re-thrown. RequestId: {RequestId}",
exceptionType, context.TraceIdentifier);
return HandlerResult.ReThrow;
}
return handleResult;
}
private static async Task<HandlerResult> EnumerateHandlers(
HttpContext context,
Type exceptionType,
Exception exception,
ExceptionHandlingPolicyOptions policyOptions,
ILogger logger)
{
bool handlerExecuted = false;
HandlerResult handleResult = HandlerResult.ReThrow;
IEnumerable<Type> handlers = policyOptions.GetHandlersInternal(exceptionType);
foreach (Type handlerType in handlers)
{
try
{
IExceptionHandler handler =
context.RequestServices.GetService(handlerType) as IExceptionHandler;
if (handler == null)
{
handlerExecuted = false;
logger.LogError(Events.HandlerNotCreated,
"Handler type {handlerType} can't be created because it not registered in IServiceProvider. RequestId: {RequestId}",
handlerType, context.TraceIdentifier);
}
else
{
handleResult = await handler.Handle(context, exception);
handlerExecuted = true;
}
}
catch (Exception e)
{
logger.LogError(Events.HandlerError, e,
"Unhandled exception executing handler of type {handlerType} on exception of type {exceptionType}. RequestId: {RequestId}",
handlerType, exceptionType, context.TraceIdentifier);
return HandlerResult.ReThrow;
}
if (handleResult != HandlerResult.NextHandler)
{
break;
}
}
if (!handlerExecuted)
{
logger.LogWarning(Events.HandlersNotFound,
"Handlers collection for exception type {exceptionType} is empty. Exception will be re-thrown. RequestId: {RequestId}",
exceptionType, context.TraceIdentifier);
return HandlerResult.ReThrow;
}
return handleResult;
}
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
ILogger logger = context.RequestServices.GetService<ILogger<ExceptionHandlingPolicyMiddleware>>() ??
NullLoggerFactory.Instance.CreateLogger(Const.Category);
await InvokeWithRetryAsync(context, next, logger, 0);
}
private async Task InvokeWithRetryAsync(HttpContext context, RequestDelegate next, ILogger logger, int iteration)
{
try
{
await next(context);
}
catch (Exception exception)
{
ExceptionHandlingPolicyOptions policyOptions = this.options.Value;
var result = await EnumerateExceptionMapping(context, policyOptions, exception, logger);
if (result == HandlerResult.ReThrow)
{
throw;
}
if (result == HandlerResult.Retry)
{
// We can't do anything if the response has already started, just abort.
if (context.Response.HasStarted)
{
logger.LogWarning(Events.RetryForStartedResponce,
exception,
"Retry requested when responce already started. Exception will be re-thrown. RequestId: {RequestId}",
context.TraceIdentifier);
throw;
}
if (iteration > MaxRetryIterations)
{
logger.LogCritical(Events.RetryIterationExceedLimit,
exception,
"Retry iterations count exceed limit of {limit}. Possible issues with retry policy configuration. Exception will be re-thrown. RequestId: {RequestId}",
MaxRetryIterations,
context.TraceIdentifier);
throw;
}
logger.LogWarning(Events.Retry,
exception,
"Retry requested. Iteration {iteration} RequestId: {RequestId}",
iteration,
context.TraceIdentifier);
context.Response.Headers.Clear();
await InvokeWithRetryAsync(context, next, logger, iteration + 1);
}
if (result != HandlerResult.Handled)
{
logger.LogWarning(Events.UnhandledResult,
exception,
"After execution of all handlers exception was not marked as handled and will be re thrown. RequestId: {RequestId}",
context.TraceIdentifier);
throw;
}
}
}
}
}