forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionResult.cs
More file actions
268 lines (224 loc) · 9.88 KB
/
ExceptionResult.cs
File metadata and controls
268 lines (224 loc) · 9.88 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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.ModelBinding;
using System.Web.Http.Properties;
namespace System.Web.Http.Results
{
/// <summary>
/// Represents an action result that returns a <see cref="HttpStatusCode.InternalServerError"/> response and
/// performs content negotiation on an <see cref="HttpError"/> based on an <see cref="Exception"/>.
/// </summary>
public class ExceptionResult : IHttpActionResult
{
private readonly Exception _exception;
private readonly IDependencyProvider _dependencies;
/// <summary>Initializes a new instance of the <see cref="ExceptionResult"/> class.</summary>
/// <param name="exception">The exception to include in the error.</param>
/// <param name="includeErrorDetail">
/// <see langword="true"/> if the error should include exception messages; otherwise, <see langword="false"/>.
/// </param>
/// <param name="contentNegotiator">The content negotiator to handle content negotiation.</param>
/// <param name="request">The request message which led to this result.</param>
/// <param name="formatters">The formatters to use to negotiate and format the content.</param>
public ExceptionResult(Exception exception, bool includeErrorDetail, IContentNegotiator contentNegotiator,
HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
: this(exception, new DirectDependencyProvider(includeErrorDetail, contentNegotiator, request,
formatters))
{
}
/// <summary>Initializes a new instance of the <see cref="ExceptionResult"/> class.</summary>
/// <param name="exception">The exception to include in the error.</param>
/// <param name="controller">The controller from which to obtain the dependencies needed for execution.</param>
public ExceptionResult(Exception exception, ApiController controller)
: this(exception, new ApiControllerDependencyProvider(controller))
{
}
private ExceptionResult(Exception exception, IDependencyProvider dependencies)
{
if (exception == null)
{
throw new ArgumentNullException("exception");
}
Contract.Assert(dependencies != null);
_exception = exception;
_dependencies = dependencies;
}
/// <summary>Gets the exception to include in the error.</summary>
public Exception Exception
{
get { return _exception; }
}
/// <summary>Gets a value indicating whether the error should include exception messages.</summary>
public bool IncludeErrorDetail
{
get { return _dependencies.IncludeErrorDetail; }
}
/// <summary>Gets the content negotiator to handle content negotiation.</summary>
public IContentNegotiator ContentNegotiator
{
get { return _dependencies.ContentNegotiator; }
}
/// <summary>Gets the request message which led to this result.</summary>
public HttpRequestMessage Request
{
get { return _dependencies.Request; }
}
/// <summary>Gets the formatters to use to negotiate and format the content.</summary>
public IEnumerable<MediaTypeFormatter> Formatters
{
get { return _dependencies.Formatters; }
}
/// <inheritdoc />
public virtual Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
return Task.FromResult(Execute());
}
private HttpResponseMessage Execute()
{
HttpError error = new HttpError(_exception, _dependencies.IncludeErrorDetail);
return NegotiatedContentResult<HttpError>.Execute(HttpStatusCode.InternalServerError, error,
_dependencies.ContentNegotiator, _dependencies.Request, _dependencies.Formatters);
}
/// <summary>Defines a provider for dependencies that are not always directly available.</summary>
/// <remarks>
/// This abstraction supports the unit testing scenario of creating the result without creating a content
/// negotiator, request message, or formatters. (The ApiController provider implementation does lazy evaluation
/// to make that scenario work.)
/// </remarks>
internal interface IDependencyProvider
{
bool IncludeErrorDetail { get; }
IContentNegotiator ContentNegotiator { get; }
HttpRequestMessage Request { get; }
IEnumerable<MediaTypeFormatter> Formatters { get; }
}
internal sealed class DirectDependencyProvider : IDependencyProvider
{
private readonly bool _includeErrorDetail;
private readonly IContentNegotiator _contentNegotiator;
private readonly HttpRequestMessage _request;
private readonly IEnumerable<MediaTypeFormatter> _formatters;
public DirectDependencyProvider(bool includeErrorDetail, IContentNegotiator contentNegotiator,
HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
{
if (contentNegotiator == null)
{
throw new ArgumentNullException("contentNegotiator");
}
if (request == null)
{
throw new ArgumentNullException("request");
}
if (formatters == null)
{
throw new ArgumentNullException("formatters");
}
_includeErrorDetail = includeErrorDetail;
_contentNegotiator = contentNegotiator;
_request = request;
_formatters = formatters;
}
public bool IncludeErrorDetail
{
get { return _includeErrorDetail; }
}
public IContentNegotiator ContentNegotiator
{
get { return _contentNegotiator; }
}
public HttpRequestMessage Request
{
get { return _request; }
}
public IEnumerable<MediaTypeFormatter> Formatters
{
get { return _formatters; }
}
}
internal sealed class ApiControllerDependencyProvider : IDependencyProvider
{
private readonly ApiController _controller;
private IDependencyProvider _resolvedDependencies;
public ApiControllerDependencyProvider(ApiController controller)
{
if (controller == null)
{
throw new ArgumentNullException("controller");
}
_controller = controller;
}
public bool IncludeErrorDetail
{
get
{
EnsureResolved();
return _resolvedDependencies.IncludeErrorDetail;
}
}
public IContentNegotiator ContentNegotiator
{
get
{
EnsureResolved();
return _resolvedDependencies.ContentNegotiator;
}
}
public HttpRequestMessage Request
{
get
{
EnsureResolved();
return _resolvedDependencies.Request;
}
}
public IEnumerable<MediaTypeFormatter> Formatters
{
get
{
EnsureResolved();
return _resolvedDependencies.Formatters;
}
}
private void EnsureResolved()
{
if (_resolvedDependencies == null)
{
HttpRequestContext requestContext = _controller.RequestContext;
Contract.Assert(requestContext != null);
bool includeErrorDetail = requestContext.IncludeErrorDetail;
HttpConfiguration configuration = _controller.Configuration;
if (configuration == null)
{
throw new InvalidOperationException(
SRResources.HttpControllerContext_ConfigurationMustNotBeNull);
}
ServicesContainer services = configuration.Services;
Contract.Assert(services != null);
IContentNegotiator contentNegotiator = services.GetContentNegotiator();
if (contentNegotiator == null)
{
throw new InvalidOperationException(Error.Format(
SRResources.HttpRequestMessageExtensions_NoContentNegotiator, typeof(IContentNegotiator)));
}
HttpRequestMessage request = _controller.Request;
if (request == null)
{
throw new InvalidOperationException(SRResources.ApiController_RequestMustNotBeNull);
}
IEnumerable<MediaTypeFormatter> formatters = configuration.Formatters;
Contract.Assert(formatters != null);
_resolvedDependencies = new DirectDependencyProvider(includeErrorDetail, contentNegotiator, request,
formatters);
}
}
}
}
}