When I process a json
response wich may be an error, I use this method to determine wether the json
is actually an error or may be an expected response :
bool TryParseResponseToError(string jsonResponse, out Error error)
{
// Check expected error keywords presence
// before try clause to avoid catch performance drawbacks
if (jsonResponse.Contains("error") &&
jsonResponse.Contains("status") &&
jsonResponse.Contains("code"))
{
try
{
error = new JsonSerializer<Error>().DeserializeFromString(jsonResponse);
return true;
}
catch
{
// The JSON response seemed to be an error, but failed to deserialize.
// It may be a successful JSON response : do nothing.
}
}
error = null;
return false;
}
But, I have an empty catch which is a bad code smell.
I did not see any TryToDeserialize
kind of method in ServiceStack libraries. Is there any ?
How do you process json errors with ServiceStack ?