-
Notifications
You must be signed in to change notification settings - Fork 3
/
HttpExceptionDescriptorResponseFormatter.cs
114 lines (107 loc) · 5.8 KB
/
HttpExceptionDescriptorResponseFormatter.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
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Cuemon.Configuration;
using Cuemon.Net.Http;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.Extensions.Options;
namespace Cuemon.AspNetCore.Diagnostics
{
/// <summary>
/// Provides a generic way to support content negotiation for exceptions in the application.
/// </summary>
/// <typeparam name="TOptions">The type of the options to configure.</typeparam>
/// <seealso cref="Configurable{TOptions}" />
/// <seealso cref="IHttpExceptionDescriptorResponseFormatter" />
/// <seealso cref="ExceptionHandlerMiddleware"/>
public class HttpExceptionDescriptorResponseFormatter<TOptions> : Configurable<TOptions>, IHttpExceptionDescriptorResponseFormatter
where TOptions : class, IContentNegotiation, IParameterObject, new()
{
private ICollection<HttpExceptionDescriptorResponseHandler> _exceptionDescriptorHandlers = new List<HttpExceptionDescriptorResponseHandler>();
/// <summary>
/// Initializes a new instance of the <see cref="HttpExceptionDescriptorResponseFormatter{TOptions}"/> class.
/// </summary>
/// <param name="setup">The <see cref="Action{TOptions}"/> which need to be configured.</param>
/// <exception cref="ArgumentException">
/// <paramref name="setup"/> could not be configured to a valid state.
/// </exception>
public HttpExceptionDescriptorResponseFormatter(Action<TOptions> setup) : this(Validator.CheckParameter(() =>
{
Validator.ThrowIfInvalidConfigurator(setup, out var options);
return options;
}))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpExceptionDescriptorResponseFormatter{TOptions}"/> class.
/// </summary>
/// <param name="options">The <see cref="IOptions{TOptions}"/> which need to be configured.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="options"/> cannot be null.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="options"/> are not in a valid state.
/// </exception>
public HttpExceptionDescriptorResponseFormatter(IOptions<TOptions> options) : this(options?.Value)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpExceptionDescriptorResponseFormatter{TOptions}"/> class.
/// </summary>
/// <param name="options">The configured options of this instance.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="options"/> cannot be null.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="options"/> are not in a valid state.
/// </exception>
public HttpExceptionDescriptorResponseFormatter(TOptions options) : base(options)
{
}
/// <summary>
/// Gets the collection of populated <see cref="HttpExceptionDescriptorResponseHandler"/> instances.
/// </summary>
/// <value>The collection of populated <see cref="HttpExceptionDescriptorResponseHandler"/> instances.</value>
public ICollection<HttpExceptionDescriptorResponseHandler> ExceptionDescriptorHandlers => _exceptionDescriptorHandlers;
/// <summary>
/// Adjusts the <see cref="Options"/> to your liking.
/// </summary>
/// <param name="setup">The <see cref="Action{TOptions}"/> which need to be configured.</param>
/// <returns>A reference to this instance so that additional calls can be chained.</returns>
/// <exception cref="ArgumentException">
/// <paramref name="setup"/> could not be configured to a valid state.
/// </exception>
public HttpExceptionDescriptorResponseFormatter<TOptions> Adjust(Action<TOptions> setup)
{
Validator.ThrowIfInvalidConfigurator(setup, out _);
setup.Invoke(Options);
return this;
}
/// <summary>
/// Populates the underlying <see cref="IContentNegotiation.SupportedMediaTypes"/> to the <see cref="ExceptionDescriptorHandlers"/> using the specified <paramref name="contentFactory"/>.
/// </summary>
/// <param name="contentFactory">The function delegate that will create an instance of <see cref="HttpContent"/> with input from the specified <see cref="HttpExceptionDescriptor"/> and <see cref="MediaTypeHeaderValue"/>.</param>
/// <param name="exceptionDescriptorHandlers">The optional collection of <see cref="HttpExceptionDescriptorResponseHandler"/> instances to populate to.</param>
/// <returns>A reference to this instance so that additional calls can be chained.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="contentFactory"/> cannot be null.
/// </exception>
public HttpExceptionDescriptorResponseFormatter<TOptions> Populate(Func<HttpExceptionDescriptor, MediaTypeHeaderValue, HttpContent> contentFactory, ICollection<HttpExceptionDescriptorResponseHandler> exceptionDescriptorHandlers = null)
{
Validator.ThrowIfNull(contentFactory);
if (exceptionDescriptorHandlers != null) { _exceptionDescriptorHandlers = exceptionDescriptorHandlers; }
foreach (var mediaType in Options.SupportedMediaTypes)
{
Decorator.Enclose(_exceptionDescriptorHandlers).AddResponseHandler(o =>
{
o.ContentType = mediaType;
o.ContentFactory = descriptor => contentFactory(descriptor, mediaType);
o.StatusCodeFactory = ed => (HttpStatusCode)ed.StatusCode;
});
}
return this;
}
}
}