/
DevicePortalException.cs
177 lines (159 loc) · 6.42 KB
/
DevicePortalException.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
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
//----------------------------------------------------------------------------------------------
// <copyright file="DevicePortalException.cs" company="Microsoft Corporation">
// Licensed under the MIT License. See LICENSE.TXT in the project root license information.
// </copyright>
//----------------------------------------------------------------------------------------------
using System;
using System.IO;
#if !WINDOWS_UWP
using System.Net;
using System.Net.Http;
#endif // !WINDOWS_UWP
using System.Runtime.InteropServices.WindowsRuntime;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Security;
using System.Threading.Tasks;
#if WINDOWS_UWP
using Windows.Foundation;
using Windows.Storage.Streams;
using Windows.Web.Http;
#endif // WINDOWS_UWP
namespace Microsoft.Tools.WindowsDevicePortal
{
/// <summary>
/// Base exception class for a Device Portal exception
/// </summary>
#if !WINDOWS_UWP
[Serializable]
#endif // !WINDOWS_UWP
public class DevicePortalException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="DevicePortalException"/> class.
/// </summary>
/// <param name="responseMessage">Http response message</param>
/// <param name="message">Optional exception message</param>
/// <param name="innerException">Optional inner exception</param>
public DevicePortalException(
HttpResponseMessage responseMessage,
string message = "",
Exception innerException = null) : this(
responseMessage.StatusCode,
responseMessage.ReasonPhrase,
responseMessage.RequestMessage != null ? responseMessage.RequestMessage.RequestUri : null,
message,
innerException)
{
try
{
if (responseMessage.Content != null)
{
Stream dataStream = null;
#if !WINDOWS_UWP
using (HttpContent content = responseMessage.Content)
{
dataStream = new MemoryStream();
Task copyTask = content.CopyToAsync(dataStream);
copyTask.ConfigureAwait(false);
copyTask.Wait();
// Ensure we point the stream at the origin.
dataStream.Position = 0;
}
#else // WINDOWS_UWP
IBuffer dataBuffer = null;
using (IHttpContent messageContent = responseMessage.Content)
{
IAsyncOperationWithProgress<IBuffer, ulong> bufferOperation = messageContent.ReadAsBufferAsync();
while (bufferOperation.Status != AsyncStatus.Completed)
{
}
dataBuffer = bufferOperation.GetResults();
if (dataBuffer != null)
{
dataStream = dataBuffer.AsStream();
}
}
#endif // WINDOWS_UWP
if (dataStream != null)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(HttpErrorResponse));
HttpErrorResponse errorResponse = (HttpErrorResponse)serializer.ReadObject(dataStream);
this.HResult = errorResponse.ErrorCode;
this.Reason = errorResponse.ErrorMessage;
}
}
}
catch (Exception)
{
// Do nothing if we fail to get additional error details from the response body.
}
}
/// <summary>
/// Initializes a new instance of the <see cref="DevicePortalException"/> class.
/// </summary>
/// <param name="statusCode">Http status code</param>
/// <param name="reason">Reason for exception</param>
/// <param name="requestUri">Request URI which threw the exception</param>
/// <param name="message">Optional message</param>
/// <param name="innerException">Optional inner exception</param>
public DevicePortalException(
HttpStatusCode statusCode,
string reason,
Uri requestUri = null,
string message = "",
Exception innerException = null) : base(
message,
innerException)
{
this.StatusCode = statusCode;
this.Reason = reason;
this.RequestUri = requestUri;
}
/// <summary>
/// Gets the HTTP Status code
/// </summary>
public HttpStatusCode StatusCode { get; private set; }
/// <summary>
/// Gets a reason for the exception
/// </summary>
public string Reason { get; private set; }
/// <summary>
/// Gets the request URI that threw the exception
/// </summary>
public Uri RequestUri { get; private set; }
#if !WINDOWS_UWP
/// <summary>
/// Get object data override
/// </summary>
/// <param name="info">Serialization info</param>
/// <param name="context">Streaming context</param>
[SecurityCritical]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
// TODO - look at an example of how this function is implemented
base.GetObjectData(info, context);
}
#endif // !WINDOWS_UWP
#region data contract
/// <summary>
/// Object containing additional error information from
/// an HTTP response.
/// </summary>
[DataContract]
private class HttpErrorResponse
{
/// <summary>
/// Gets or sets the ErrorCode
/// </summary>
[DataMember(Name = "ErrorCode")]
public int ErrorCode { get; set; }
/// <summary>
/// Gets or sets the ErrorMessage
/// </summary>
[DataMember(Name = "ErrorMessage")]
public string ErrorMessage { get; set; }
}
#endregion
}
}