using System; using System.Net.Http; using Sony.Filtr.DeezerAPI.Model; namespace Sony.Filtr.DeezerAPI { [Serializable] public class DeezerException : Exception { public DeezerException() { } public DeezerException(string message, Exception inner) : base(message, inner) { } protected DeezerException( System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) { } public DeezerException(Error error) : base(error?.message) { Error = error; } public Error Error { get; set; } public static DeezerException FromHttpResponseMessage(HttpResponseMessage httpResponse, string userMessage = "") { 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 = $"User message: '{userMessage}' " + $"StatusCode: {httpResponse.StatusCode}. " + $"Reason: {httpResponse.ReasonPhrase}. " + $"Method: {method}. " + $"RequestUri: {requestUri}. " + $"Original response: {rawResponse}. "; return new DeezerException(new Error() { code = (int)httpResponse.StatusCode, message = message, type = ""}); } } }