using System; using System.Net; using System.Net.Http; namespace Sony.Filtr.ApolloAPI { public class ApolloWebAPIException : Exception { public ApolloWebAPIException(string message) : this(message, String.Empty, HttpStatusCode.OK, String.Empty, String.Empty, String.Empty) { } public ApolloWebAPIException(string message, string method, HttpStatusCode statusCode, string reason, string requestUri, string rawResponse) : base(message) { this.StatusCode = statusCode; this.Method = method; this.Reason = reason; this.RequestUri = requestUri; this.RawResponse = rawResponse; } public ApolloWebAPIException(string message, Exception ex) : base(message, ex) { } public readonly HttpStatusCode StatusCode; public readonly string Reason; public readonly string Method; public readonly string RequestUri; public readonly string RawResponse; public static ApolloWebAPIException FromHttpResponseMessage(HttpResponseMessage httpResponse) { if (httpResponse == null) { throw new ArgumentNullException(nameof(httpResponse)); } string method = httpResponse.RequestMessage == null ? String.Empty : httpResponse.RequestMessage.Method.ToString(); string requestUri = httpResponse.RequestMessage == null ? String.Empty : httpResponse.RequestMessage.RequestUri.ToString(); string rawResponse = httpResponse.Content.ReadAsStringAsync().Result; string message = $"StatusCode: {httpResponse.StatusCode}. " + $"Reason: {httpResponse.ReasonPhrase}. " + $"Method: {method}. " + $"RequestUri: {requestUri}. " + $"Original response: {rawResponse}. "; return new ApolloWebAPIException( message, method, httpResponse.StatusCode, httpResponse.ReasonPhrase, requestUri, rawResponse); } } }