Note that there are some explanatory texts on larger screens.

plurals
  1. POWCF REST Service & iOS Error Handling With NSError
    primarykey
    data
    text
    <p>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:</p> <pre><code> [WebGet(UriTemplate = "get/{id}", ResponseFormat = WebMessageFormat.Json)] public Person Get(string id) { //check security if(!SecurityHelper.IsAuthenticated()) { throw new WebFaultException&lt;PersonException&gt;(new PersonException { Reason = "Permission denied." }, HttpStatusCode.Unauthorized); } </code></pre> <p>I can use jQuery to call the service like such:</p> <pre><code> $.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); } }); }); </code></pre> <p>...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.</p> <p>Now - I'm trying to put together my iOS app to call the same service and everything is working great from there as well <em>except</em> 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:</p> <pre><code>//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:&amp;response error:&amp;error]; //check for error if(error) { //debug NSLog(error.description); //send back error return error; } else { </code></pre> <p>In the error.description I'm only getting a generic message like "The operation couldn't be completed."</p> <p>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.</p> <p>Thanks in advance for any help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload