Skip to content

Instantly share code, notes, and snippets.

@lkaczanowski
Last active November 4, 2022 23:08
Show Gist options
  • Save lkaczanowski/febb25cc49f339c5f516 to your computer and use it in GitHub Desktop.
Save lkaczanowski/febb25cc49f339c5f516 to your computer and use it in GitHub Desktop.
RestSharp extensions for error handling
using System;
using System.Net;
using System.Text;
using RestSharp;
namespace RestSharpExtensions
{
internal static class RestSharpExtensions
{
public static bool IsSuccessStatusCode(this HttpStatusCode responseCode)
{
var numericResponse = (int)responseCode;
const int statusCodeOk = (int)HttpStatusCode.OK;
const int statusCodeBadRequest = (int)HttpStatusCode.BadRequest;
return numericResponse >= statusCodeOk &&
numericResponse < statusCodeBadRequest;
}
public static bool IsSuccessful(this IRestResponse response)
{
return response.StatusCode.IsScuccessStatusCode() &&
response.ResponseStatus == ResponseStatus.Completed;
}
public static void EnsureResponseWasSuccessful(this IRestClient client, IRestRequest request, IRestResponse response)
{
if (response.IsSuccessful())
{
return;
}
var requestUri = client.BuildUri(request);
throw RestException.CreateException(requestUri, response);
}
}
public class RestException : Exception
{
public RestException(HttpStatusCode httpStatusCode, Uri requestUri, string content, string message, Exception innerException)
: base(message, innerException)
{
HttpStatusCode = httpStatusCode;
RequestUri = requestUri;
Content = content;
}
public HttpStatusCode HttpStatusCode { get; private set; }
public Uri RequestUri { get; private set; }
public string Content { get; private set; }
public static RestException CreateException(Uri requestUri, IRestResponse response)
{
Exception innerException = null;
var messageBuilder = new StringBuilder();
messageBuilder.AppendLine(string.Format("Processing request [{0}] resulted with following errors:", requestUri));
if (response.StatusCode.IsScuccessStatusCode() == false)
{
messageBuilder.AppendLine("- Server responded with unsuccessfult status code: " + response.StatusDescription);
}
if (response.ErrorException != null)
{
messageBuilder.AppendLine("- An exception occurred while processing request: " + response.ErrorMessage);
innerException = response.ErrorException;
}
return new RestException(response.StatusCode, requestUri, response.Content, messageBuilder.ToString(), innerException);
}
}
}
@lkaczanowski
Copy link
Author

Fixed. Thanks!

@lerasah
Copy link

lerasah commented Feb 27, 2022

Thanks :)

@Hoang-Minh
Copy link

Thank you !

@rubenstolk
Copy link

The typo is still there in a few locations. But how to use this though?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment