Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a proper strategy for handling error responses from RestSharp?

A typical http call using RestSharp looks as follows:

var client = new RestClient("http://exampleapi.com");
var request = new RestRequest("someapi", Method.GET);
IRestResponse response = client.Execute(request);

From the documentation at https://github.com/restsharp/RestSharp/wiki/Getting-Started:

If there is a network transport error (network is down, failed DNS lookup, etc), RestResponse.Status will be set to ResponseStatus.Error, otherwise it will be ResponseStatus.Completed. If an API returns a 404, ResponseStatus will still be Completed. If you need access to the HTTP status code returned you will find it at RestResponse.StatusCode.

Further, the following appear to be behaviors of RestSharp responses:

  • RestClient.Execute() will never throw an exception
  • If the network request fails, ie a condition occurs that would normally result in an exception (eg network timed out, unreachable, name could not be resolved), then response.ErrorException will be populated with some Exception-derived type and response.ErrorMessage will contain some message error string and response.StatusCode will be set to ResponseStatus.Error, Response.Status.Aborted, ResponseStatus.TimedOut, etc.
  • If the network request succeeds but there's some HTTP error (eg 404 not found, 500 server error, etc.), then response.StatusCode will be set to NotFound, etc, Response.ErrorException and Response.Error will be null and response.StatusCode will be set to 'ResponseStatus.Completed`.

I may have missed some possible responses, but I think the gist is there.

Given this, how should I determine response success or failure? Options include:

  • If ErrorException == null then check the http response
  • If response.ResponseStatus == ResponseStatus.Completed then check Response.StatusCode and depending on the result, grab the response data and handle accordingly if not what you expect
  • If the http response is some error then depending on the type of error check ErrorException
  • More...?

I don't want to overthink this but I am assuming there's a pattern (for lack of better term) for handling this cleanly.

like image 755
Robert Avatar asked Dec 26 '15 20:12

Robert


People also ask

What is RestSharp used for?

RestSharp is an open source HTTP client library that makes it easy to consume RESTful services. RestSharp provides a developer friendly interface to work with RESTful services while abstracting the internal intricacies of working with HTTP requests. RestSharp supports both synchronous and asynchronous requests.

Is RestSharp better than HTTP client?

The main conclusion is that one is not better than the other, and we shouldn't compare them since RestSharp is a wrapper around HttpClient. The decision between using one of the two tools depends on the use case and the situation.

Is RestSharp asynchronous?

RestSharp v107 changes the library API surface and its behaviour significantly. We advise looking at v107 docs to understand how to migrate to the latest version of RestSharp. The main purpose of RestSharp is to make synchronous and asynchronous calls to remote resources over HTTP.

What is RestSharp library?

RestSharp is a comprehensive, open-source HTTP client library that works with all kinds of DotNet technologies. It can be used to build robust applications by making it easy to interface with public APIs and quickly access data without the complexity of dealing with raw HTTP requests.


1 Answers

Given this, how should I determine response success or failure?

I suggest checking ((int) response.StatusCode). If 200 <= ((int) response.StatusCode) && ((int) response.StatusCode) < 400, it succeeded (for an intentionally vague definition of success). Otherwise, the status code is either outside this range, or response.ErrorException has something interesting.

If you're expecting a specific status code you may wish to take an action if it's some other, non-error code. For example, if I expect only 200 responses, I might want to log a 301 response as a warning, but continue on.

See this answer for a slightly different approach.

like image 87
LexH Avatar answered Sep 18 '22 13:09

LexH