RESTSharp 抛出时未捕获异常
Exception not caught when thrown by RESTSharp
我正在使用 RestSharp 与 REST-Server 通信并为调用编写了一个小包装函数
private T Get<T>(string restAdress) where T : new()
{
try
{
// throw new Exception(); // This one is caught
IRestClient restClient = new RestClient(settings.Value.HostToConnect).UseSerializer(new JsonNetSerializer()); // (1)
RestRequest restRequest = new RestRequest(restAdress);
IRestResponse<T> result = restClient.Get<T>(restRequest); // (2)
return result.Data;
}
catch (Exception e) // debugger wont stop here
{
// debugger wont stop here too
// code within this block is not executed
}
return null; // (3)
}
因为我想使用 Newtonsoft-Attributes,所以我提供了一个自定义(反)序列化器 (1)。
public class JsonNetSerializer : IRestSerializer
{
public string Serialize(object obj) =>
JsonConvert.SerializeObject(obj);
public string Serialize(RestSharp.Parameter bodyParameter) =>
JsonConvert.SerializeObject(bodyParameter.Value);
public T Deserialize<T>(IRestResponse response) =>
JsonConvert.DeserializeObject<T>(response.Content); // (4)
public string[] SupportedContentTypes { get; } =
{
"application/json", "text/json", "text/x-json", "text/javascript", "*+json"
};
public string ContentType { get; set; } = "application/json";
public DataFormat DataFormat { get; } = DataFormat.Json;
}
调用 REST 服务并尝试获取结果时 (2) 如果反序列化失败 (4),将抛出 Exception
。但是 Exception
没有被 try-catch
块捕获。我尝试调试,但调试器在第 (3) 行继续运行后,catch
和带有 catch 的日志记录永远不会执行。调试器甚至不会在 catch (Exception e)
处停止,它会直接从 (4) 到 (3)。
(抱歉不是英文,window 的标题说 "Exception User-Unhandled")
任何人都可以向我解释这种行为吗?
我正在维护 RestSharp,所以我希望我能回答这个问题。
默认情况下,RestSharp 不会引发反序列化。如果您查看代码,反序列化发生在 RestClient
.
的私有 IRestResponse<T> Deserialize<T>(IRestRequest request, IRestResponse raw)
方法中
当 RestSharp 无法反序列化响应时,它会创建一个错误响应。您将响应状态设置为 Error
并将异常放入响应对象的 ErrorException
属性 中,同时异常消息会进入 ErrorMessage
响应。
如果将 RestClient
实例的 FailOnDeserialization
属性 分配给 true
,仍然可以告诉 RestSharp 进行反序列化。
这里发生的是调试器的一个有趣设置,另一个例子是这个 closed bug report。当调试器达到抛出异常的程度时,它将中断,导致您遇到的行为。
如果您取消选中对话框中的异常设置 "Break when this exception type is user-unhandled",您应该能够到达您的 catch 块,因为一旦抛出指定的异常,调试器就不再中断执行。
在您的情况下,您可以在 "Ausnahmeeinstellungen" 下找到该选项。
我正在使用 RestSharp 与 REST-Server 通信并为调用编写了一个小包装函数
private T Get<T>(string restAdress) where T : new()
{
try
{
// throw new Exception(); // This one is caught
IRestClient restClient = new RestClient(settings.Value.HostToConnect).UseSerializer(new JsonNetSerializer()); // (1)
RestRequest restRequest = new RestRequest(restAdress);
IRestResponse<T> result = restClient.Get<T>(restRequest); // (2)
return result.Data;
}
catch (Exception e) // debugger wont stop here
{
// debugger wont stop here too
// code within this block is not executed
}
return null; // (3)
}
因为我想使用 Newtonsoft-Attributes,所以我提供了一个自定义(反)序列化器 (1)。
public class JsonNetSerializer : IRestSerializer
{
public string Serialize(object obj) =>
JsonConvert.SerializeObject(obj);
public string Serialize(RestSharp.Parameter bodyParameter) =>
JsonConvert.SerializeObject(bodyParameter.Value);
public T Deserialize<T>(IRestResponse response) =>
JsonConvert.DeserializeObject<T>(response.Content); // (4)
public string[] SupportedContentTypes { get; } =
{
"application/json", "text/json", "text/x-json", "text/javascript", "*+json"
};
public string ContentType { get; set; } = "application/json";
public DataFormat DataFormat { get; } = DataFormat.Json;
}
调用 REST 服务并尝试获取结果时 (2) 如果反序列化失败 (4),将抛出 Exception
。但是 Exception
没有被 try-catch
块捕获。我尝试调试,但调试器在第 (3) 行继续运行后,catch
和带有 catch 的日志记录永远不会执行。调试器甚至不会在 catch (Exception e)
处停止,它会直接从 (4) 到 (3)。
任何人都可以向我解释这种行为吗?
我正在维护 RestSharp,所以我希望我能回答这个问题。
默认情况下,RestSharp 不会引发反序列化。如果您查看代码,反序列化发生在 RestClient
.
IRestResponse<T> Deserialize<T>(IRestRequest request, IRestResponse raw)
方法中
当 RestSharp 无法反序列化响应时,它会创建一个错误响应。您将响应状态设置为 Error
并将异常放入响应对象的 ErrorException
属性 中,同时异常消息会进入 ErrorMessage
响应。
如果将 RestClient
实例的 FailOnDeserialization
属性 分配给 true
,仍然可以告诉 RestSharp 进行反序列化。
这里发生的是调试器的一个有趣设置,另一个例子是这个 closed bug report。当调试器达到抛出异常的程度时,它将中断,导致您遇到的行为。
如果您取消选中对话框中的异常设置 "Break when this exception type is user-unhandled",您应该能够到达您的 catch 块,因为一旦抛出指定的异常,调试器就不再中断执行。
在您的情况下,您可以在 "Ausnahmeeinstellungen" 下找到该选项。