Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refit.ApiException Error Handling

Tags:

refit

How do I get to the content of Refit.ApiException?

Depending on what the inner content is, I want to let the user know how to proceed. So I see that thrown exception has the following content ...

Content "{\"error\":\"invalid_grant\",\"error_description\":\"The user name or password is incorrect.\"}"

The question is, how do I access that?

like image 969
crazyDiamond Avatar asked Mar 02 '15 20:03

crazyDiamond


3 Answers

You can add one catch block for ApiException. and you can get content from this catch block. See below:

catch (ApiException ex)
{
    var content = ex.GetContentAs<Dictionary<String, String>>();
    Debug.WriteLine(ex.Message);
}
like image 73
Pavan V Parekh Avatar answered Oct 22 '22 10:10

Pavan V Parekh


Going through the RestService class https://github.com/paulcbetts/refit/blob/master/Refit/RestService.cs

figured out I could use the GetContentAs method

So just did this..

((Refit.ApiException)ex).GetContentAs<Dictionary<String, String>>()) 

to parse out the key value content.

like image 10
crazyDiamond Avatar answered Oct 22 '22 09:10

crazyDiamond


As an extra heads-up:

GetContentAs<T>(); is now deprecated.

Use GetContentAsAsync<T>(); instead.

like image 5
jari Avatar answered Oct 22 '22 10:10

jari