Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 4802

How to distinguish between network/server/serialization errors in granular manner?

$
0
0

I have a simple REST client written using RestSharp.

var opts = new RestClientOptions("http://api.muyservice.com/"){    // ResponseStatus should not be an Error when deserialisation failed.    FailOnDeserializationError = false,};using var client = new RestClient(opts);var request = new RestRequest("myEndpoint");var response = await client.ExecuteAsync<ResponseDto>(request);// error handling// network error? No, not really.// Also an http codes 4xx/5xx, except 404if (response.ResponseStatus != ResponseStatus.Completed) {    // log network errors}else if (response.StatusCode != HttpStatusCode.OK){    // http request completed successfully, but server returned non-200 results.    // handle server errors    if (response.StatusCode == HttpStatusCode.BadRequest)    {        // extract error details from response body    }    else    {        // handle generic server error    }}else if (response.Data == null){    // serialization failed}else{    // all ok, handle the results}

I want to handle network/server/serialisation errors in the different ways (e.g. log different details, suggest to adjust timeouts in the config in case of request timeout and so on), but I have not found the straightforward way to distinguish between the error types (network/server/deserialisation) and make the granular error handling.

What problems I found so far:

  • the response.ResponseStatus will be Error in case of network error or in case of server errors 5xx/4xx (except 404).
  • in case of a timeout the ResponseStatus will be TimedOut, but the ErrorMessage and the ErrorException will be Operation was canceled (no single word about the timeout)
  • IsSuccessfulStatusCode will say Success for http 404 code.
  • there is no dedicated error state for the deserialisation (which is understandable because different deserialisers can be used). So if null is the valid value for the ResponseDto, then there is no way to diagnose deserialisation errors at all?

Is there is a straightforward way to handle the error in RestSharp in a granular way?


Viewing all articles
Browse latest Browse all 4802

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>