forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionContext.cs
More file actions
156 lines (129 loc) · 6.59 KB
/
ExceptionContext.cs
File metadata and controls
156 lines (129 loc) · 6.59 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
// 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.Web.Http.Controllers;
using System.Web.Http.Properties;
namespace System.Web.Http.ExceptionHandling
{
/// <summary>Represents an exception and the contextual data associated with it when it was caught.</summary>
public class ExceptionContext
{
/// <summary>Initializes a new instance of the <see cref="ExceptionContext"/> class.</summary>
/// <param name="exception">The exception caught.</param>
/// <param name="catchBlock">The catch block where the exception was caught.</param>
/// <remarks>This constructor is for unit testing purposes only.</remarks>
public ExceptionContext(Exception exception, ExceptionContextCatchBlock catchBlock)
{
if (exception == null)
{
throw new ArgumentNullException("exception");
}
Exception = exception;
if (catchBlock == null)
{
throw new ArgumentNullException("catchBlock");
}
CatchBlock = catchBlock;
}
/// <summary>
/// Initializes a new instance of the <see cref="ExceptionContext"/> class using the values provided.
/// </summary>
/// <param name="exception">The exception caught.</param>
/// <param name="catchBlock">The catch block where the exception was caught.</param>
/// <param name="actionContext">The action context in which the exception occurred.</param>
public ExceptionContext(Exception exception, ExceptionContextCatchBlock catchBlock,
HttpActionContext actionContext)
: this(exception, catchBlock)
{
if (actionContext == null)
{
throw new ArgumentNullException("actionContext");
}
ActionContext = actionContext;
HttpControllerContext controllerContext = actionContext.ControllerContext;
if (controllerContext == null)
{
throw new ArgumentException(Error.Format(SRResources.TypePropertyMustNotBeNull,
typeof(HttpActionContext).Name, "ControllerContext"), "actionContext");
}
ControllerContext = controllerContext;
HttpRequestContext requestContext = controllerContext.RequestContext;
Contract.Assert(requestContext != null);
RequestContext = requestContext;
HttpRequestMessage request = controllerContext.Request;
if (request == null)
{
throw new ArgumentException(Error.Format(SRResources.TypePropertyMustNotBeNull,
typeof(HttpControllerContext).Name, "Request"), "actionContext");
}
Request = request;
}
/// <summary>
/// Initializes a new instance of the <see cref="ExceptionContext"/> class using the values provided.
/// </summary>
/// <param name="exception">The exception caught.</param>
/// <param name="catchBlock">The catch block where the exception was caught.</param>
/// <param name="request">The request being processed when the exception was caught.</param>
public ExceptionContext(Exception exception, ExceptionContextCatchBlock catchBlock, HttpRequestMessage request)
: this(exception, catchBlock)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
Request = request;
RequestContext = request.GetRequestContext();
}
/// <summary>
/// Initializes a new instance of the <see cref="ExceptionContext"/> class using the values provided.
/// </summary>
/// <param name="exception">The exception caught.</param>
/// <param name="catchBlock">The catch block where the exception was caught.</param>
/// <param name="request">The request being processed when the exception was caught.</param>
/// <param name="response">The repsonse being returned when the exception was caught.</param>
public ExceptionContext(Exception exception, ExceptionContextCatchBlock catchBlock, HttpRequestMessage request,
HttpResponseMessage response) : this(exception, catchBlock)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
Request = request;
RequestContext = request.GetRequestContext();
if (response == null)
{
throw new ArgumentNullException("response");
}
Response = response;
}
/// <summary>Gets the exception caught.</summary>
public Exception Exception { get; private set; }
/// <summary>Gets the catch block in which the exception was caught.</summary>
public ExceptionContextCatchBlock CatchBlock { get; private set; }
/// <summary>Gets the request being processed when the exception was caught.</summary>
/// <remarks>The setter is for unit testing purposes only.</remarks>
public HttpRequestMessage Request { get; set; }
/// <summary>Gets the request context in which the exception occurred.</summary>
/// <remarks>The setter is for unit testing purposes only.</remarks>
public HttpRequestContext RequestContext { get; set; }
/// <summary>Gets the controller context in which the exception occurred, if available.</summary>
/// <remarks>
/// <para>This property will be <see langword="null"/> in most cases.</para>
/// <para>The setter is for unit testing purposes only.</para>
/// </remarks>
public HttpControllerContext ControllerContext { get; set; }
/// <summary>Gets the action context in which the exception occurred, if available.</summary>
/// <remarks>
/// <para>This property will be <see langword="null"/> in most cases.</para>
/// <para>The setter is for unit testing purposes only.</para>
/// </remarks>
public HttpActionContext ActionContext { get; set; }
/// <summary>Gets the response being sent when the exception was caught.</summary>
/// <remarks>
/// <para>This property will be <see langword="null"/> in most cases.</para>
/// <para>The setter is for unit testing purposes only.</para>
/// </remarks>
public HttpResponseMessage Response { get; set; }
}
}