forked from mono/aspnetwebstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpError.cs
More file actions
296 lines (268 loc) · 11.8 KB
/
HttpError.cs
File metadata and controls
296 lines (268 loc) · 11.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
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Web.Http.ModelBinding;
using System.Web.Http.Properties;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
namespace System.Web.Http
{
/// <summary>
/// Defines a serializable container for arbitrary error information.
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix", Justification = "This type is only a dictionary to get the right serialization format")]
[SuppressMessage("Microsoft.Usage", "CA2237:MarkISerializableTypesWithSerializable", Justification = "DCS does not support IXmlSerializable types that are also marked as [Serializable]")]
[XmlRoot("Error")]
public sealed class HttpError : Dictionary<string, object>, IXmlSerializable
{
private const string MessageKey = "Message";
private const string MessageDetailKey = "MessageDetail";
private const string ModelStateKey = "ModelState";
private const string ExceptionMessageKey = "ExceptionMessage";
private const string ExceptionTypeKey = "ExceptionType";
private const string StackTraceKey = "StackTrace";
private const string InnerExceptionKey = "InnerException";
/// <summary>
/// Initializes a new instance of the <see cref="HttpError"/> class.
/// </summary>
public HttpError()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpError"/> class containing error message <paramref name="message"/>.
/// </summary>
/// <param name="message">The error message to associate with this instance.</param>
public HttpError(string message)
{
if (message == null)
{
throw Error.ArgumentNull("message");
}
Message = message;
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpError"/> class for <paramref name="exception"/>.
/// </summary>
/// <param name="exception">The exception to use for error information.</param>
/// <param name="includeErrorDetail"><c>true</c> to include the exception information in the error; <c>false</c> otherwise</param>
public HttpError(Exception exception, bool includeErrorDetail)
{
if (exception == null)
{
throw Error.ArgumentNull("exception");
}
Message = SRResources.ErrorOccurred;
if (includeErrorDetail)
{
Add(ExceptionMessageKey, exception.Message);
Add(ExceptionTypeKey, exception.GetType().FullName);
Add(StackTraceKey, exception.StackTrace);
if (exception.InnerException != null)
{
Add(InnerExceptionKey, new HttpError(exception.InnerException, includeErrorDetail));
}
}
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpError"/> class for <paramref name="modelState"/>.
/// </summary>
/// <param name="modelState">The invalid model state to use for error information.</param>
/// <param name="includeErrorDetail"><c>true</c> to include exception messages in the error; <c>false</c> otherwise</param>
public HttpError(ModelStateDictionary modelState, bool includeErrorDetail)
{
if (modelState == null)
{
throw Error.ArgumentNull("modelState");
}
if (modelState.IsValid)
{
throw Error.Argument("modelState", SRResources.ValidModelState);
}
Message = SRResources.BadRequest;
HttpError modelStateError = new HttpError();
foreach (KeyValuePair<string, ModelState> keyModelStatePair in modelState)
{
string key = keyModelStatePair.Key;
ModelErrorCollection errors = keyModelStatePair.Value.Errors;
if (errors != null && errors.Count > 0)
{
IEnumerable<string> errorMessages = errors.Select(error =>
{
if (includeErrorDetail && error.Exception != null)
{
return error.Exception.Message;
}
else
{
return String.IsNullOrEmpty(error.ErrorMessage) ? SRResources.ErrorOccurred : error.ErrorMessage;
}
}).ToArray();
modelStateError.Add(key, errorMessages);
}
}
Add(ModelStateKey, modelStateError);
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpError"/> class containing error message <paramref name="message"/>
/// and error message detail <paramref name="messageDetail"/>.
/// </summary>
/// <param name="message">The error message to associate with this instance.</param>
/// <param name="messageDetail">The error message detail to associate with this instance.</param>
internal HttpError(string message, string messageDetail)
: this(message)
{
if (messageDetail == null)
{
throw Error.ArgumentNull("message");
}
Add(MessageDetailKey, messageDetail);
}
/// <summary>
/// The high-level, user-visible message explaining the cause of the error. Information carried in this field
/// should be considered public in that it will go over the wire regardless of the <see cref="IncludeErrorDetailPolicy"/>.
/// As a result care should be taken not to disclose sensitive information about the server or the application.
/// </summary>
public string Message
{
get { return GetPropertyValue<String>(MessageKey); }
set { this[MessageKey] = value; }
}
/// <summary>
/// The <see cref="ModelState"/> containing information about the errors that occurred during model binding.
/// </summary>
/// <remarks>
/// The inclusion of <see cref="System.Exception"/> information carried in the <see cref="ModelState"/> is
/// controlled by the <see cref="IncludeErrorDetailPolicy"/>. All other information in the <see cref="ModelState"/>
/// should be considered public in that it will go over the wire. As a result care should be taken not to
/// disclose sensitive information about the server or the application.
/// </remarks>
public HttpError ModelState
{
get { return GetPropertyValue<HttpError>(ModelStateKey); }
}
/// <summary>
/// A detailed description of the error intended for the developer to understand exactly what failed.
/// </summary>
/// <remarks>
/// The inclusion of this field is controlled by the <see cref="IncludeErrorDetailPolicy"/>. The
/// field is expected to contain information about the server or the application that should not
/// be disclosed broadly.
/// </remarks>
public string MessageDetail
{
get { return GetPropertyValue<String>(MessageDetailKey); }
set { this[MessageDetailKey] = value; }
}
/// <summary>
/// The message of the <see cref="System.Exception"/> if available.
/// </summary>
/// <remarks>
/// The inclusion of this field is controlled by the <see cref="IncludeErrorDetailPolicy"/>. The
/// field is expected to contain information about the server or the application that should not
/// be disclosed broadly.
/// </remarks>
public string ExceptionMessage
{
get { return GetPropertyValue<String>(ExceptionMessageKey); }
set { this[ExceptionMessageKey] = value; }
}
/// <summary>
/// The type of the <see cref="System.Exception"/> if available.
/// </summary>
/// <remarks>
/// The inclusion of this field is controlled by the <see cref="IncludeErrorDetailPolicy"/>. The
/// field is expected to contain information about the server or the application that should not
/// be disclosed broadly.
/// </remarks>
public string ExceptionType
{
get { return GetPropertyValue<String>(ExceptionTypeKey); }
set { this[ExceptionTypeKey] = value; }
}
/// <summary>
/// The stack trace information associated with this instance if available.
/// </summary>
/// <remarks>
/// The inclusion of this field is controlled by the &
85A6
lt;see cref="IncludeErrorDetailPolicy"/>. The
/// field is expected to contain information about the server or the application that should not
/// be disclosed broadly.
/// </remarks>
public string StackTrace
{
get { return GetPropertyValue<String>(StackTraceKey); }
set { this[StackTraceKey] = value; }
}
/// <summary>
/// The inner <see cref="System.Exception"/> associated with this instance if available.
/// </summary>
/// <remarks>
/// The inclusion of this field is controlled by the <see cref="IncludeErrorDetailPolicy"/>. The
/// field is expected to contain information about the server or the application that should not
/// be disclosed broadly.
/// </remarks>
public HttpError InnerException
{
get { return GetPropertyValue<HttpError>(InnerExceptionKey); }
}
/// <summary>
/// Gets a particular property value from this error instance.
/// </summary>
/// <typeparam name="TValue">The type of the property.</typeparam>
/// <param name="key">The name of the error property.</param>
/// <returns>The value of the error property.</returns>
public TValue GetPropertyValue<TValue>(string key)
{
TValue value;
if (this.TryGetValue(key, out value))
{
return value;
}
return default(TValue);
}
XmlSchema IXmlSerializable.GetSchema()
{
return null;
}
void IXmlSerializable.ReadXml(XmlReader reader)
{
if (reader.IsEmptyElement)
{
reader.Read();
return;
}
reader.ReadStartElement();
while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
{
string key = XmlConvert.DecodeName(reader.LocalName);
string value = reader.ReadInnerXml();
this.Add(key, value);
reader.MoveToContent();
}
reader.ReadEndElement();
}
void IXmlSerializable.WriteXml(XmlWriter writer)
{
foreach (KeyValuePair<string, object> keyValuePair in this)
{
string key = keyValuePair.Key;
object value = keyValuePair.Value;
writer.WriteStartElement(XmlConvert.EncodeLocalName(key));
if (value != null)
{
HttpError innerError = value as HttpError;
if (innerError == null)
{
writer.WriteValue(value);
}
else
{
((IXmlSerializable)innerError).WriteXml(writer);
}
}
writer.WriteEndElement();
}
}
}
}