Refit.ApiException错误处理

cra*_*ond 11 refit

我如何获得Refit.ApiException的内容?

根据内容的内容,我想让用户知道如何继续.所以我看到抛出的异常有以下内容......

内容"{\"error \":\"invalid_grant \",\"error_description \":\"用户名或密码不正确.\"}"

问题是,我该如何访问?

cra*_*ond 8

浏览RestService类https://github.com/paulcbetts/refit/blob/master/Refit/RestService.cs

想通了我可以使用GetContentAs方法

所以就这样做了..

((Refit.ApiException)ex).GetContentAs<Dictionary<String, String>>()) 
Run Code Online (Sandbox Code Playgroud)

解析键值内容.


jar*_*ari 7

作为额外的提示:

GetContentAs<T>();现已弃用。

GetContentAsAsync<T>();代替使用。


Pav*_*ekh 6

您可以为ApiException添加一个catch块.你可以从这个catch块中获取内容.见下文:

catch (ApiException ex)
{
    var content = ex.GetContentAs<Dictionary<String, String>>();
    Debug.WriteLine(ex.Message);
}
Run Code Online (Sandbox Code Playgroud)