Question

Several times before I used the method...

NSURLResponse *response;
NSError *error;

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

to send/receive data synchronously when in an NSOperation and it has worked.

I'm working on a new project (new API, new server) now and the error just isn't being populated.

I'm using Charles proxy to snoop the network traffic and I can see "Response Code 500 Internal Server Error" coming back from the failed request in Charles but in the app the error object is always nil.

Like I said, I've used this loads before and the error is always populated if an error occurs.

Is there something on the server side that I can point out to the server side dev? He's adamant that I must be doing something wrong. The only thing I don't understand is that Charles is picking up response code 500 from it.

N.B. The problem is not that the error is occurring. The problem is that when it does occur the NSError object is not reporting it back to me. Just to clear it up in case anyone tells me to ask the server dev to fix the error. :-)

Was it helpful?

Solution

Your error object is not populated because the request succeeded from NSURLConnection's perspective -- that is, it was able to send data to the server and receive a response.

For protocol-specific errors, you need to inspect the NSURLResponse you get back. In this case, cast the response to NSHTTPURLResponse and invoke -statusCode.

OTHER TIPS

The error is only populated if there is no response from the server or not a valid HTTP response, e.g. connection loss etc.

"Response Code 500 Internal Server Error" means there was an internal server error and your server returns an HTTP message with the response code 500. You should use NSHTTPURLResponse instead of NSURLResponse and call the statusCode method to check the HTTP response code. If the response code starts with 2 everything is fine. So I usually have a check like this: statusCode / 100 == 2.

For more HTTP response codes see http://en.wikipedia.org/w/index.php?title=HTTP_response_codes

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top