[11/05/2020 5:44 PM] Amber R: Hi Ketan, the issue with throwing a specific exception is that we have to parse the error response and throw an exception based on that
For eg. if call in GetCollections method fails, if we throw a custom exception when it’s not success status code, isn’t it the same as as using .EnsureSuccessStatusCode()? Just the message will be different since it’s custom exception, but we still won’t know why the call fail
I got this message from one of my colleague. We were argueing on why using EnsureSuccessCode() is a bad idea.
So in case of HTTP request failure, EnsureSuccessStatusCode() throws an exception with message saying - ‘Response does not indicate success status code’ & there is no other information attached to it. All the information about why your HTTP call failed is lost. Your HTTP request can fail due to multiple reasons like auth issues, network issues, server side issues, DNS related issues but EnsureSuccessStatusCode() will not tell you what exactly caused the HTTP request failure.
Vs
If we do custom exception, then we have control on the way HTTP Response is being handled. We can analyze the response received from HTTP call & decide what to do with it.
So even if it looks like the only difference is in message but thats only thing thats important here & we should not loose it.
Example :
Comments