简体   繁体   中英

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. The service will be accessible from both web (jQuery) and iOS products. 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:

        $.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.

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. Here is the code I have in my call to the REST service from iOS:

//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."

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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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