-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathProblemDetailsOptions.cs
More file actions
379 lines (331 loc) · 16.8 KB
/
ProblemDetailsOptions.cs
File metadata and controls
379 lines (331 loc) · 16.8 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Net.Http.Headers;
using MvcProblemDetails = Microsoft.AspNetCore.Mvc.ProblemDetails;
namespace Hellang.Middleware.ProblemDetails
{
public class ProblemDetailsOptions
{
public const string DefaultExceptionDetailsPropertyName = "exceptionDetails";
public const string DefaultTraceIdPropertyName = "traceId";
public ProblemDetailsOptions()
{
SourceCodeLineCount = 6;
Mappers = new List<ExceptionMapper>();
RethrowPolicies = new List<Func<HttpContext, Exception, bool>>();
ContentTypes = new MediaTypeCollection();
TraceIdPropertyName = DefaultTraceIdPropertyName;
ExceptionDetailsPropertyName = DefaultExceptionDetailsPropertyName;
ValidationProblemStatusCode = StatusCodes.Status422UnprocessableEntity;
AllowedHeaderNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
HeaderNames.AccessControlAllowCredentials,
HeaderNames.AccessControlAllowHeaders,
HeaderNames.AccessControlAllowMethods,
HeaderNames.AccessControlAllowOrigin,
HeaderNames.AccessControlExposeHeaders,
HeaderNames.AccessControlMaxAge,
HeaderNames.StrictTransportSecurity,
HeaderNames.WWWAuthenticate,
};
}
/// <summary>
/// Gets or sets the number of source code lines to include for context in exception details.
/// The default is <c>6</c>.
/// </summary>
public int SourceCodeLineCount { get; set; }
/// <summary>
/// The <see cref="IFileProvider"/> for getting file information when reading stack trace information.
/// </summary>
public IFileProvider FileProvider { get; set; } = null!;
/// <summary>
/// Gets or sets the function for getting a <c>traceId</c> to include in the problem response.
/// The default gets the ID from <see cref="Activity.Current"/> with a
/// fallback to <see cref="HttpContext.TraceIdentifier"/>.
/// </summary>
public Func<HttpContext, string?> GetTraceId { get; set; } = null!;
/// <summary>
/// Gets or sets the predicate used for determining whether exception details (stack trace etc.)
/// should be included in the problem details response.
/// The default returns <c>true</c> when <see cref="IHostEnvironment.EnvironmentName"/> is "Development".
/// </summary>
public Func<HttpContext, Exception, bool> IncludeExceptionDetails { get; set; } = null!;
/// <summary>
/// Configure which properties to include in the problem details response.
/// By default all fields are excluded.
/// This can be used as an alternative to <see cref="IncludeExceptionDetails"/> when specific fields are needed.
/// </summary>
public Func<HttpContext, Exception, IncludeProblemDetailProps>? IncludePropsFilter { get; set; } = null!;
/// <summary>
/// The property name to use for traceId
/// This defaults to <see cref="DefaultTraceIdPropertyName"/> (<c>traceId</c>).
/// </summary>
public string TraceIdPropertyName { get; set; }
/// <summary>
/// The property name to use for exception details.
/// This defaults to <see cref="DefaultExceptionDetailsPropertyName"/> (<c>errors</c>).
/// </summary>
public string ExceptionDetailsPropertyName { get; set; }
/// <summary>
/// Gets or sets the predicate used for determining whether a request/response should be considered
/// a problem or not. The default returns <c>true</c> if the following is true:
/// <list type="bullet">
/// <item>
/// <description>The status code is between 400 and 600.</description>
/// </item>
/// <item>
/// <description>The <c>Content-Length</c> header is empty.</description>
/// </item>
/// <item>
/// <description>The <c>Content-Type</c> header is empty.</description>
/// </item>
/// </list>
/// </summary>
public Func<HttpContext, bool> IsProblem { get; set; } = null!;
/// <summary>
/// Gets or sets the function for mapping response status codes to problem details instances.
/// The default will just create a <see cref="StatusCodeProblemDetails"/> using the response
/// status code of the current <see cref="HttpContext"/>.
/// </summary>
public Func<HttpContext, MvcProblemDetails> MapStatusCode { get; set; } = null!;
/// <summary>
/// Gets or sets a callback used to transform a problem details instance right before
/// it is written to the response.
/// </summary>
public Action<HttpContext, MvcProblemDetails>? OnBeforeWriteDetails { get; set; }
/// <summary>
/// Gets or sets a predicate used for determining whether an exception should be logged as unhandled.
/// The default returns <c>true</c> if the response status code doesn't have a value, or the
/// value is <see cref="StatusCodes.Status500InternalServerError"/> or higher.
/// </summary>
public Func<HttpContext, Exception, MvcProblemDetails, bool> ShouldLogUnhandledException { get; set; } = null!;
/// <summary>
/// Gets or sets an action to populate response cache headers to prevent caching problem details responses.
/// </summary>
public Action<HttpContext, HeaderDictionary> AppendCacheHeaders { get; set; } = null!;
/// <summary>
/// Gets the set of headers that shouldn't be cleared when producing a problem details response.
/// This includes CORS, HSTS and authentication challenge headers by default.
/// </summary>
public HashSet<string> AllowedHeaderNames { get; }
/// <summary>
/// Gets the supported <c>Content-Type</c> values for use in content negotiation.
/// The default values are <c>application/problem+json</c> and <c>application/problem+xml</c>.
/// </summary>
public MediaTypeCollection ContentTypes { get; }
/// <summary>
/// Gets or sets the status code used for validation errors when using the MVC conventions.
/// </summary>
public int ValidationProblemStatusCode { get; set; }
private List<ExceptionMapper> Mappers { get; }
private List<Func<HttpContext, Exception, bool>> RethrowPolicies { get; }
/// <summary>
/// Maps the specified exception type <typeparamref name="TException"/> to the specified
/// status code <paramref name="statusCode"/>. This also includes default values for
/// <see cref="MvcProblemDetails.Type"/> and <see cref="MvcProblemDetails.Title"/>.
/// </summary>
/// <param name="statusCode">The status code to return for the specified exception.</param>
/// <typeparam name="TException">The exception type to map to the specified status code.</typeparam>
public void MapToStatusCode<TException>(int statusCode) where TException : Exception
{
Map<TException>((_, _) => StatusCodeProblemDetails.Create(statusCode));
}
/// <summary>
/// Configures the middleware to ignore any exception of the specified exception type <typeparamref name="TException"/>.
/// This will cause the exception to be rethrown to be handled upstream.
/// </summary>
/// <typeparam name="TException">The exception type to ignore.</typeparam>
public void Ignore<TException>() where TException : Exception
{
Map<TException>((_, _) => null);
}
/// <summary>
/// Configures the middleware to ignore exceptions of the specified exception type <typeparamref name="TException"/> that match the specified <paramref name="predicate"/>.
/// This will cause the exception to be rethrown to be handled upstream.
/// </summary>
/// <param name="predicate">The predicate to check whether the exception should be ignored or not.</param>
/// <typeparam name="TException">The exception type to ignore.</typeparam>
public void Ignore<TException>(Func<HttpContext, TException, bool> predicate) where TException : Exception
{
Map(predicate, (_, _) => null);
}
/// <summary>
/// Maps the specified exception type <typeparamref name="TException"/> to a <see cref="MvcProblemDetails"/> instance
/// using the specified <paramref name="mapping"/> function.
/// </summary>
/// <remarks>
/// Mappers are called in the order they're registered.
/// Returning <c>null</c> from the mapper will signify that you can't or don't want to map the exception to <see cref="MvcProblemDetails"/>.
/// This will cause the exception to be rethrown.
/// </remarks>
/// <param name="mapping">The mapping function for creating a problem details instance.</param>
/// <typeparam name="TException">The exception type to map using the specified mapping function.</typeparam>
public void Map<TException>(Func<TException, MvcProblemDetails?> mapping) where TException : Exception
{
Map<TException>((_, ex) => mapping(ex));
}
/// <summary>
/// Maps the specified exception type <typeparamref name="TException"/> to a <see cref="MvcProblemDetails"/> instance
/// using the specified <paramref name="mapping"/> function.
/// </summary>
/// <remarks>
/// Mappers are called in the order they're registered.
/// Returning <c>null</c> from the mapper will signify that you can't or don't want to map the exception to <see cref="MvcProblemDetails"/>.
/// This will cause the exception to be rethrown.
/// </remarks>
/// <param name="mapping">The mapping function for creating a problem details instance.</param>
/// <typeparam name="TException">The exception type to map using the specified mapping function.</typeparam>
public void Map<TException>(Func<HttpContext, TException, MvcProblemDetails?> mapping) where TException : Exception
{
Map((_, _) => true, mapping);
}
/// <summary>
/// Maps the specified exception type <typeparamref name="TException"/> to a <see cref="MvcProblemDetails"/> instance
/// using the specified <paramref name="mapping"/> function.
/// </summary>
/// <remarks>
/// Mappers are called in the order they're registered.
/// Returning <c>null</c> from the mapper will signify that you can't or don't want to map the exception to <see cref="MvcProblemDetails"/>.
/// This will cause the exception to be rethrown.
/// </remarks>
/// <param name="predicate">This Map will skip this exception if the predicate returns false.</param>
/// <param name="mapping">The mapping function for creating a problem details instance.</param>
/// <typeparam name="TException">The exception type to map using the specified mapping function.</typeparam>
public void Map<TException>(
Func<HttpContext, TException, bool> predicate,
Func<HttpContext, TException, MvcProblemDetails?> mapping)
where TException : Exception
{
Mappers.Add(new ExceptionMapper(
typeof(TException),
(ctx, ex) => mapping(ctx, (TException)ex),
(ctx, ex) => predicate(ctx, (TException)ex)));
}
/// <summary>
/// Marks the specified exception type <typeparamref name="TException"/> for re-throwing.
/// This is useful if you have other upstream middleware that wants to handle the exception.
/// </summary>
/// <typeparam name="TException">The type of exception to re-throw.</typeparam>
public void Rethrow<TException>() where TException : Exception
{
Rethrow<TException>((_, _) => true);
}
/// <summary>
/// Marks the specified exception type <typeparamref name="TException"/> for re-throwing, using
/// the specified <paramref name="predicate"/>.
/// </summary>
/// <param name="predicate">The predicate to determine whether an exception should be re-thrown.</param>
/// <typeparam name="TException">The type of exception to re-throw.</typeparam>
public void Rethrow<TException>(Func<HttpContext, TException, bool> predicate) where TException : Exception
{
Rethrow((ctx, ex) => ex is TException exception && predicate(ctx, exception));
}
/// <summary>
/// Configures the middleware to re-throw all exceptions. This can be useful if you
/// have upstream middleware that needs to do additional handling of exceptions.
/// </summary>
public void RethrowAll()
{
RethrowPolicies.Clear(); // There's no point in keeping multiple policies
Rethrow((_, _) => true); // when this one always returns true :)
}
/// <summary>
/// Configures the middleware to re-throw exceptions, based on the specified <paramref name="predicate"/>.
/// </summary>
/// <param name="predicate">The predicate to determine whether an exception should be re-thrown.</param>
public void Rethrow(Func<HttpContext, Exception, bool> predicate)
{
RethrowPolicies.Add(predicate);
}
internal bool ShouldRethrowException(HttpContext httpContext, Exception exception)
{
foreach (var policy in RethrowPolicies)
{
if (policy(httpContext, exception))
{
return true;
}
}
return false;
}
internal void CallBeforeWriteHook(HttpContext context, MvcProblemDetails details)
{
AddTraceId(context, details);
OnBeforeWriteDetails?.Invoke(context, details);
#if NETCOREAPP3_1
// Workaround for https://github.com/dotnet/aspnetcore/pull/17565.
context.Response.StatusCode = details.Status ?? context.Response.StatusCode;
#endif
}
private void AddTraceId(HttpContext context, MvcProblemDetails details)
{
var key = TraceIdPropertyName;
if (details.Extensions.ContainsKey(key))
{
return;
}
var traceId = GetTraceId.Invoke(context);
if (!string.IsNullOrEmpty(traceId))
{
details.Extensions[key] = traceId;
}
}
internal bool TryMapProblemDetails(HttpContext context, Exception? exception, out MvcProblemDetails? problem)
{
if (exception is null)
{
problem = default;
return false;
}
foreach (var mapper in Mappers)
{
if (mapper.TryMap(context, exception, out problem))
{
return true;
}
}
problem = default;
return false;
}
private sealed class ExceptionMapper
{
public ExceptionMapper(Type type, Func<HttpContext, Exception, MvcProblemDetails?> mapping, Func<HttpContext, Exception, bool> predicate)
{
Type = type ?? throw new ArgumentNullException(nameof(type));
Mapping = mapping ?? throw new ArgumentNullException(nameof(mapping));
Predicate = predicate ?? throw new ArgumentNullException(nameof(predicate));
}
private Type Type { get; }
private Func<HttpContext, Exception, MvcProblemDetails?> Mapping { get; }
private Func<HttpContext, Exception, bool> Predicate { get; }
public bool ShouldMap(HttpContext context, Exception exception)
{
return Type.IsInstanceOfType(exception) && Predicate(context, exception);
}
public bool TryMap(HttpContext context, Exception exception, out MvcProblemDetails? problem)
{
if (ShouldMap(context, exception))
{
try
{
problem = Mapping(context, exception);
return true;
}
catch
{
problem = default;
return false;
}
}
problem = default;
return false;
}
}
}
}