forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpRouteExceptionHandler.cs
More file actions
126 lines (109 loc) · 5.06 KB
/
HttpRouteExceptionHandler.cs
File metadata and controls
126 lines (109 loc) · 5.06 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
// 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.Diagnostics.Contracts;
using System.Net.Http;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.ExceptionHandling;
namespace System.Web.Http.WebHost.Routing
{
/// <summary>Represents a handler that asynchronously handles an unhandled exception from routing.</summary>
internal class HttpRouteExceptionHandler : HttpTaskAsyncHandler
{
private readonly ExceptionDispatchInfo _exceptionInfo;
private readonly IExceptionLogger _exceptionLogger;
private readonly IExceptionHandler _exceptionHandler;
public HttpRouteExceptionHandler(ExceptionDispatchInfo exceptionInfo)
: this(exceptionInfo, ExceptionServices.GetLogger(GlobalConfiguration.Configuration),
ExceptionServices.GetHandler(GlobalConfiguration.Configuration))
{
}
internal HttpRouteExceptionHandler(ExceptionDispatchInfo exceptionInfo,
IExceptionLogger exceptionLogger, IExceptionHandler exceptionHandler)
{
Contract.Assert(exceptionInfo != null);
Contract.Assert(exceptionLogger != null);
Contract.Assert(exceptionHandler != null);
_exceptionInfo = exceptionInfo;
_exceptionLogger = exceptionLogger;
_exceptionHandler = exceptionHandler;
}
internal ExceptionDispatchInfo ExceptionInfo
{
get { return _exceptionInfo; }
}
internal IExceptionLogger ExceptionLogger
{
get { return _exceptionLogger; }
}
internal IExceptionHandler ExceptionHandler
{
get { return _exceptionHandler; }
}
public override Task ProcessRequestAsync(HttpContext context)
{
return ProcessRequestAsync(new HttpContextWrapper(context));
}
internal async Task ProcessRequestAsync(HttpContextBase context)
{
Exception exception = _exceptionInfo.SourceException;
Contract.Assert(exception != null);
OperationCanceledException canceledException = exception as OperationCanceledException;
if (canceledException != null)
{
// If the route throws a cancelation exception, then we'll abort the request instead of
// reporting an 'error'. We don't expect this to happen, but aborting the request is
// consistent with our behavior in other hosts.
context.Request.Abort();
return;
}
HttpRequestMessage request = context.GetOrCreateHttpRequestMessage();
HttpResponseMessage response = null;
CancellationToken cancellationToken = context.Response.GetClientDisconnectedTokenWhenFixed();
HttpResponseException responseException = exception as HttpResponseException;
try
{
if (responseException != null)
{
response = responseException.Response;
Contract.Assert(response != null);
// This method call is hardened and designed not to throw exceptions (since they won't be caught
// and handled further by its callers).
await HttpControllerHandler.CopyResponseAsync(context, request, response, _exceptionLogger,
_exceptionHandler, cancellationToken);
}
else
{
// This method call is hardened and designed not to throw exceptions (since they won't be caught and
// handled further by its callers).
bool handled = await HttpControllerHandler.CopyErrorResponseAsync(
WebHostExceptionCatchBlocks.HttpWebRoute, context, request, null,
_exceptionInfo.SourceException, _exceptionLogger, _exceptionHandler, cancellationToken);
if (!handled)
{
_exceptionInfo.Throw();
}
}
}
catch (OperationCanceledException)
{
// This block handles cancellations that might occur while we're writing an 'error' response.
//
// HttpTaskAsyncHandler treats a canceled task as an unhandled exception (logged to Application event
// log). Instead of returning a canceled task, abort the request and return a completed task.
context.Request.Abort();
}
finally
{
// The other HttpTaskAsyncHandler is HttpControllerHandler; it has similar cleanup logic.
request.DisposeRequestResources();
request.Dispose();
if (response != null)
{
response.Dispose();
}
}
}
}
}