如何使用RestSharp捕获异常 [英] How can I catch exceptions with RestSharp

查看:490
本文介绍了如何使用RestSharp捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与RestSharp合作一个项目。随着时间的流逝,我发现了RestResponse类可以抛出的几个异常,其中大多数必须处理,因此我的应用程序不会崩溃。我怎么知道所有可能的异常并分别处理。

I am working on a project with RestSharp. Over time I discovered several exceptions that RestResponse class can throw, most of which I have to handle so my app doesn't crash. How can I know of all possible exceptions and handle them individually.

推荐答案

RestResponses和错误



这是来自 RestSharp Wiki上的文档


关于错误处理的说明



**如果存在网络传输错误(网络已关闭,则DNS查找失败)等),将RestResponse.Status设置为ResponseStatus.Error,**,否则将设置为ResponseStatus.Completed。如果API返回404,则ResponseStatus仍将完成。如果您需要访问返回的HTTP状态代码,则可以在RestResponse.StatusCode中找到它。 Status属性是完成指示,与API错误处理无关。

Note about error handling

**If there is a network transport error (network is down, failed DNS lookup, etc), RestResponse.Status will be set to ResponseStatus.Error, ** otherwise it will be ResponseStatus.Completed. If an API returns a 404, ResponseStatus will still be Completed. If you need access to the HTTP status code returned you will find it at RestResponse.StatusCode. The Status property is an indicator of completion independent of the API error handling.

话虽这么说,但建议检查状态的方法 RestResponse 将查看 RestResponse.Status

That being said the recommended way to check the status of the RestResponse is to look at RestResponse.Status

The内部Execute调用的源本身如下所示。

The source itself for the internal Execute call reads as follows.

private IRestResponse Execute(IRestRequest request, string httpMethod,Func<IHttp, string, HttpResponse> getResponse)
{
    AuthenticateIfNeeded(this, request);
    IRestResponse response = new RestResponse();
    try
    {
        var http = HttpFactory.Create();

        ConfigureHttp(request, http);

        response = ConvertToRestResponse(request, getResponse(http, httpMethod));
        response.Request = request;
        response.Request.IncreaseNumAttempts();

    }
    catch (Exception ex)
    {
        response.ResponseStatus = ResponseStatus.Error;
        response.ErrorMessage = ex.Message;
        response.ErrorException = ex;
    }

    return response;
}

因此,您知道可以期待标准的.net异常。 推荐用法建议仅检查是否存在 ErrorException 像代码示例中一样。

So with that, you know that you can expect standard .net exceptions. The recommended usage suggests just checking for the existence of an ErrorException like in the code example.

//Snippet of code example in above link
var response = client.Execute<T>(request);

if (response.ErrorException != null)
{
    const string message = "Error retrieving response.  Check inner details for more info.";
    var twilioException = new ApplicationException(message, response.ErrorException);
    throw twilioException;
}

如果您要针对某种异常执行特定操作,只需执行使用如下所示的一行进行类型比较。

If you want to preform a specific action on a certain kind of exception just preform a type comparison using a line like the following.

if (response.ErrorException.GetType() == typeof(NullReferenceException))
{
  //handle error
}




我怎么知道所有

How can I know of all possible exceptions and handle them individually.

老实说,我建议不要单独捕获所有异常,我发现该特定要求值得怀疑。您确定他们不只是希望您优雅地捕获并处理异常

Honestly, I'd recommend against catching all the exceptions individually and I'd find that particular requirement questionable. Are you sure they don't just want you to catch and handle exceptions gracefully?

如果您绝对需要分别处理每种可能的情况,那么我会记录在测试和检查中出现的异常反对那些。如果您尝试捕获所有内容,则可能会有一百多种不同的异常。这就是异常类的基础。

If you absolutely need to handle each possible case individually then I'd log the exceptions that crop up in testing and check against those. If you were to try and catch everything you could potentially have over a hundred different exceptions. That is what the base Exception class is for.

异常类是处理所有从Exception继承的内容的全部。总体思路是,您要特别记录一些异常,您可以实际执行某些操作,例如通知用户互联网不可用或远程服务器已关闭,并让异常类处理任何其他极端情况。 msdn链接

The exception class is the catch-all for handling anything that inherits from Exception. The general idea is that you make special note of the exceptions that you can actually do something with like notifying the user that the internet is unavailable or the remote server is down and let the exception class handle any other edge cases. msdn link

这篇关于如何使用RestSharp捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆