MelbourneDeveloper/RestClient.Net

How should I handle 503 Service Unavailable

egarim opened this issue · 11 comments

Hi lets said that the following request will return a 503

var result = await client.PostAsync<DataResult, IDataParameters>(Parameters, resource, Headers);

at the moment the only way to know that there was a 503 is when you deserialize the response you can see in the text that is a 503

In the end, when I get a 503 the exception happens on the implementation of the ISerializationAdapter when try to use the text of the 503 as the JSON source of the response

is there a way to only deserialize when the response is 200?

@egarim there are two ways to handle this. You can catch the exception, or you can turn exceptions off and get the response code from the result.

@egarim here is an example of getting the response without and exception:

client.ThrowExceptionOnFailure = false;

@egarim however I do see your point! The serialization adapter doesn't have access to the response code so it's impossible to know that the response text is actually an error. I will see what I can do about that.

@egarim here is an example of getting the response without and exception:

client.ThrowExceptionOnFailure = false;

Hi, I Have tried this "client.ThrowExceptionOnFailure = false; " and it still breaks on the deserialization, I think the best way to handle this case is to have access to the response so you can decide to proceed or to break the request gracefully

@egarim I'll try to recreate the problem and get back to you.

@egarim any chance you could use fiddler to capture the exact response payload? Please include response headers.

@egarim I can recreate the problem. I will try to fix it.

@egarim thanks for your patience and taking the time to report this. This is my highest priority right now. I will get back to you ASAP.

@egarim the bug was that it was trying to deserialize even when the response was not successful. I fixed the bug in the develop branch. I put a unit test here to reproduce the problem and make sure it doesn't come back.

Note that the signature for the serialization adapter has changed.

This is a breaking change so I will release this in version 4.0. I still have another small refactor I want to make before I release this version. I will let you know when 4.0 is released.

@egarim version 4.0.0 has been released. The exception should be HttpStatusException now. And if you turn ThrowExceptionOnFailure off you should be able to get the status code in the result without it trying to deserialize. Please let me know if this fixes your issue.

Thanks, Christian, I already updated and the new signature contains enough to handle my flow, right now I'm facing other problems after updating, I already reported it

#71