There is a server which, in the case of an invalid request, returns a deserialized JSON string representing an object of type EXCEPTION . Depending on the type of error, an object can have a completely different structure from a simple one:
public class simpleException { public string Reason {get;set;} public string Controller {get;set;} } To process the received response and understand what exactly the exception is, there is a method:
try { var Err = JsonConvert.DeserializeObject<simpleException>(responsedBodyStr); } catch (JsonSerializationException) { try { var Err = JsonConvert.DeserializeObject<TrsException>(responsedBodyStr); } catch(JsonSerializationException) { try {.... } } Try try-catch attachment try-catch much more and they turn into awful spaghetti code. Actually the question is - if you are faced with similar situations, then share your advice on how to improve the code?
upd I get the exception string like this:
catch (WebException ex) { ExcStr= new StreamReader(ex.Response.GetResponseStream()).ReadToEnd(),
WebExceptionyou can see the status code andStatus Description- SergeyExceptioncomes to you? - Andrei NOPaspfields of the object are serialized into the json string and returned with the server response. - Sergey