/// <summary> /// Processes the error response. /// </summary> /// <param name="url">The URL.</param> /// <param name="ex">The ex.</param> /// <returns> /// Error result built using the data in the response /// </returns> /// <exception cref="Microsoft.Marketplace.SaasKit.Exceptions.MeteredBillingException"> /// Token expired. Please logout and login again. /// Not Found /// Bad Request /// Internal Server error /// </exception> /// <exception cref="MeteredBillingException"></exception> protected override T ProcessErrorResponse(string url, WebException ex) { var webresponse = ex.Response as System.Net.HttpWebResponse; if (webresponse != null) { string responseString = string.Empty; MeteringErrorResult meteredBillingErrorResult = new MeteringErrorResult(); using (StreamReader reader = new StreamReader(webresponse.GetResponseStream())) { responseString = reader.ReadToEnd(); meteredBillingErrorResult = JsonConvert.DeserializeObject <MeteringErrorResult>(responseString); } this.logger?.Info("Error :: " + responseString); if (webresponse.StatusCode == HttpStatusCode.Unauthorized || webresponse.StatusCode == HttpStatusCode.Forbidden) { throw new MeteredBillingException("Token expired. Please logout and login again.", SaasApiErrorCode.Unauthorized, meteredBillingErrorResult); } else if (webresponse.StatusCode == HttpStatusCode.NotFound) { this.logger?.Warn("Returning the error as " + JsonConvert.SerializeObject(new { Error = "Not Found" })); throw new MeteredBillingException(string.Format("Unable to find the request {0}", url), SaasApiErrorCode.NotFound, meteredBillingErrorResult); } else if (webresponse.StatusCode == HttpStatusCode.Conflict) { this.logger?.Warn("Returning the error as " + JsonConvert.SerializeObject(new { Error = "Conflict" })); throw new MeteredBillingException(string.Format("Conflict came for {0}", url), SaasApiErrorCode.Conflict, meteredBillingErrorResult); } else if (webresponse.StatusCode == HttpStatusCode.BadRequest) { this.logger?.Warn("Returning the error as " + JsonConvert.SerializeObject(new { Error = "Bad Request" })); throw new MeteredBillingException(string.Format("Unable to process the request {0}, server responding as BadRequest. Please verify the post data. ", url), SaasApiErrorCode.BadRequest, meteredBillingErrorResult); } } return(null); }
/// <summary> /// Initializes a new instance of the <see cref="MeteredBillingException" /> class. /// </summary> /// <param name="message">The message.</param> /// <param name="errorCode">The error code.</param> /// <param name="meteredBillingErrorDetail">The metered billing error detail.</param> /// <param name="inner">The inner.</param> public MeteredBillingException(string message, string errorCode, MeteringErrorResult meteredBillingErrorDetail, System.Exception inner) : this(message, inner) { this.ErrorCode = errorCode; this.MeteredBillingErrorDetail = meteredBillingErrorDetail; }
/// <summary> /// Initializes a new instance of the <see cref="MeteredBillingException" /> class. /// </summary> /// <param name="message">A message that describes the error.</param> /// <param name="errorCode">The error code.</param> /// <param name="meteredBillingErrorDetail">The metered billing error detail.</param> public MeteredBillingException(string message, string errorCode, MeteringErrorResult meteredBillingErrorDetail) : this(message, errorCode) { this.MeteredBillingErrorDetail = meteredBillingErrorDetail; }