in WindowsDevicePortalWrapper/WindowsDevicePortalWrapper.Shared/Exceptions/DevicePortalException.cs [113:181]
public static async Task<DevicePortalException> CreateAsync(
HttpResponseMessage responseMessage,
string message = "",
Exception innerException = null)
{
DevicePortalException error = new DevicePortalException(
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();
await content.CopyToAsync(dataStream).ConfigureAwait(false);
// Ensure we point the stream at the origin.
dataStream.Position = 0;
}
#else // WINDOWS_UWP
IBuffer dataBuffer = null;
using (IHttpContent messageContent = responseMessage.Content)
{
dataBuffer = await messageContent.ReadAsBufferAsync();
if (dataBuffer != null)
{
dataStream = dataBuffer.AsStream();
}
}
#endif // WINDOWS_UWP
if (dataStream != null)
{
HttpErrorResponse errorResponse = DevicePortal.ReadJsonStream<HttpErrorResponse>(dataStream);
if (errorResponse != null)
{
error.HResult = errorResponse.ErrorCode;
error.Reason = errorResponse.ErrorMessage;
// If we didn't get the Hresult and reason from these properties, try the other ones.
if (error.HResult == 0)
{
error.HResult = errorResponse.Code;
}
if (string.IsNullOrEmpty(error.Reason))
{
error.Reason = errorResponse.Reason;
}
}
}
}
}
catch (Exception)
{
// Do nothing if we fail to get additional error details from the response body.
}
return error;
}