简体   繁体   English

使用NSError的WCF REST服务和iOS错误处理

[英]WCF REST Service & iOS Error Handling With NSError

I have a WCF RESTful service and am trying to sketch out how I will handle errors on the server and various clients. 我有WCF RESTful服务,正在尝试勾画如何处理服务器和各种客户端上的错误。 The service will be accessible from both web (jQuery) and iOS products. 可以从Web(jQuery)和iOS产品访问该服务。 Here is a look at how I'm throwing errors on the service: 这是我如何在服务上引发错误的一个例子:

    [WebGet(UriTemplate = "get/{id}", ResponseFormat = WebMessageFormat.Json)]
    public Person Get(string id)
    {
        //check security
        if(!SecurityHelper.IsAuthenticated()) { throw new WebFaultException<PersonException>(new PersonException { Reason = "Permission denied." }, HttpStatusCode.Unauthorized); }

I can use jQuery to call the service like such: 我可以使用jQuery这样调用服务:

        $.ajax({
          type: "GET",
          dataType: "json",
          url: "/person/get/123",
          success: function(data) {
            alert('success');
          },
          error: function(xhr, status, error) {
            alert("AJAX Error!");
            alert(xhr.responseText);
          }
        });
      });

...and everything works great - the call is made and the error is thrown (as I have not supplied any authentication) and the error: callback is called. ...而且一切工作正常-进行了调用,并引发了错误(因为我没有提供任何身份验证),并且错误:调用了回调。 In the error callback when I examine xhr.responseText I get the correct JSON object ({"reason":"Permission denied!"}) showing the error reason that was supplied by the server. 在检查xhr.responseText的错误回调中,我得到了正确的JSON对象({“原因”:“权限被拒绝!”}),显示了服务器提供的错误原因。

Now - I'm trying to put together my iOS app to call the same service and everything is working great from there as well except I can't get the error details that were supplied by the service. 现在-我正在尝试将我的iOS应用放在一起以调用相同的服务,并且从那里开始一切都很好, 除非我无法获得该服务提供的错误详细信息。 Here is the code I have in my call to the REST service from iOS: 这是我从iOS调用REST服务时使用的代码:

//set up for any errors
NSError *error = nil;

//set up response
NSURLResponse *response = [[NSURLResponse alloc] init];

//make the request
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

//check for error
if(error)
{
    //debug
    NSLog(error.description);

    //send back error
    return error;
}
else 
{

In the error.description I'm only getting a generic message like "The operation couldn't be completed." 在error.description中,我仅收到一条通用消息,例如“操作无法完成”。

How do I go about getting custom error information that is sent by the server? 如何获取服务器发送的自定义错误信息? I've been looking at the userInfo property on the NSError class but cannot figure out if I can get at the custom info and, if so, how I go about doing it. 我一直在看NSError类的userInfo属性,但无法弄清楚我是否可以获取自定义信息,如果可以的话,我该如何去做。

Thanks in advance for any help. 在此先感谢您的帮助。

The error message will be on the data returned by the request (the response body): 错误消息将出现在请求(响应主体)返回的数据上:

//make the request
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

if (error) {
    if (data) {
        NSString *respBody = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    } else {
        NSLog(@"%@", error.description);
    }
}
else 
{
    // get response
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM