一点点细节. RestSharp公开一个Response.ErrorException属性 – 如果RestClient.Execute< T&()调用引发任何异常,它将通过ErrorException属性公开,而不是抛出.他们的文档包括以下示例:
// TwilioApi.cs public class TwilioApi { const string BaseUrl = "https://api.twilio.com/2008-08-01"; public T Execute<T>(RestRequest request) where T : new() { var client = new RestClient(); client.BaseUrl = BaseUrl; client.Authenticator = new HttpBasicAuthenticator(_accountSid,_secretKey); request.AddParameter("AccountSid",_accountSid,ParameterType.UrlSegment); // used on every request 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; } return response.Data; } }
我在代码中采用了这种模式,但是我的API服务器返回了401 Unauthorized,但是ErrorException属性仍然为空.我可以在RestResponse.StatusCode和RestResponse.StatusDescription属性中看到未经授权的状态代码和错误消息 – 但是我为什么未经授权的响应不会导致ErrorException字段被填充,我感到困惑.
解决方法
public static class RestSharpExtensionMethods { public static bool IsSuccessful(this IRestResponse response) { return response.StatusCode.IsSuccessStatusCode() && response.ResponseStatus == ResponseStatus.Completed; } public static bool IsSuccessStatusCode(this HttpStatusCode responseCode) { int numericResponse = (int)responseCode; return numericResponse >= 200 && numericResponse <= 399; } }
我提出要求反应是反序列化的:
public async Task<ResponseModel<TResponse>> PerformRequestAsync<TResponse>(IRestRequest request) { var response = await _client.ExecuteTaskAsync<ResponseModel<TResponse>>(request); ResponseModel<TResponse> responseData; if (response.IsSuccessful()) { responseData = response.Data; } else { string resultMessage = HandleErrorResponse<TResponse>(request,response); responseData = new ResponseModel<TResponse> { Success = false,ResultMessage = resultMessage }; } return responseData; }
但是,在测试期间,我发现当我没有为这种情况配置错误处理时,当请求未映射的URL时,我的Web服务返回HTML格式的404页面.这导致response.ErrorException属性包含以下字符串:
Reference to undeclared entity ‘nbsp’. Line n,position m.
显然,RestSharp尝试将响应解析为XML,即使内容类型是text / html.也许我会给RestSharp提出一个问题.
当然在生产中,你不应该在打电话给自己的服务时得到404,但是我希望这个客户端是彻底的,可重用的.
所以我可以想到两个解决方案:
>检查状态代码并显示说明
>确保服务返回一个可以解析的错误对象
前者是很容易完成的.在HandleErrorResponse()中,我根据状态代码的数值构建结果消息(用户可呈现)和错误字符串(loggable):
public string HandleErrorResponse(IRestRequest request,IRestResponse response) { string statusString = string.Format("{0} {1} - {2}",(int)response.StatusCode,response.StatusCode,response.StatusDescription); string errorString = "Response status: " + statusString; string resultMessage = ""; if (!response.StatusCode.IsScuccessStatusCode()) { if (string.IsNullOrWhiteSpace(resultMessage)) { resultMessage = "An error occurred while processing the request: " + response.StatusDescription; } } if (response.ErrorException != null) { if (string.IsNullOrWhiteSpace(resultMessage)) { resultMessage = "An exception occurred while processing the request: " + response.ErrorException.Message; } errorString += ",Exception: " + response.ErrorException; } // (other error handling here) _logger.ErrorFormat("Error response: {0}",errorString); return resultMessage; }
现在,由于我的API响应总是包装在一个ResponseModel< T>我可以使用set up an exception filter and a NotFound route在ResultMessage属性中返回带有错误或异常消息的可解析响应模型:
public class HandleErrorAttribute : ExceptionFilterAttribute { public override void OnException(HttpActionExecutedContext context) { // (log context.Exception here) context.Response = context.Request.CreateResponse(HttpStatusCode.InternalServerError,new ResponseModel<object> { Success = false,ResultMessage = "An exception occurred while processing the request: " + context.Exception.Message }); } }
和:
public class ErrorController : ApiController { public HttpResponseMessage Handle404() { const string notFoundString = "The requested resource could not be found"; var responseMessage = Request.CreateResponse(HttpStatusCode.NotFound,ResultMessage = notFoundString }); responseMessage.ReasonPhrase = notFoundString; return responseMessage; } }
这样,我的服务的响应总是可以由RestSharp解析,我可以使用通用日志记录方法:
public string HandleErrorResponse<TResponseModel>(IRestRequest request,IRestResponse<<ResponseModel<TResponseModel>> response)
并记录实际响应//(其他错误处理),如果可用:
if (response.Data != null && !string.IsNullOrWhiteSpace(response.Data.ResultMessage)) { resultMessage = response.Data.ResultMessage; errorString += string.Format(",Service response: \"{0}\"",response.Data.ResultMessage); }