public static IRestResponse AssertRestSharpResponseIsOk(this IRestResponse response) { response.AssertArgumentIsNotNull(nameof(response)); //Handle Errors with RESTSharp responses that are not valid... int responseStatus = (int)response.StatusCode; if (!response.IsOk() || !(responseStatus >= 200 && responseStatus <= 299)) { throw new RestSharpHttpException(response); } return(response); }
public RestSharpHttpException(IRestResponse response, Uri requestUri = null, Exception innerException = null) : base(response.ErrorMessage, //Nest some helpful info. in the inner exception, and pass along any other possible inner exceptions as children also... new Exception($"Http Request failed for [{response.ResponseUri}] with [StatusCode={(int)response.StatusCode}-{response.StatusDescription}]; {response?.ErrorException?.Message ?? response.Content}", innerException ?? response?.ErrorException ) ) { response.AssertArgumentIsNotNull(nameof(response), "RestRequest exception yielded a null RestResponse object."); this.HttpStatusCode = response.StatusCode; this.HttpStatusDescription = response.StatusCode.ToString(); this.RequestUri = requestUri; this.ResponseUri = response.ResponseUri; this.ErrorMessage = response.ErrorMessage; this.ErrorResponseBody = response.Content; this.ResponseException = response.ErrorException; }