dingbat / nsrails

iOS/Mac OS framework for Rails
http://nsrails.com
MIT License
517 stars 37 forks source link

It seems like [NSRRequest sendSynchronous] does not catch server response on error? #36

Closed tomoyuki28jp closed 11 years ago

tomoyuki28jp commented 11 years ago

When I run following code and its server returns unauthorized response status code with an error message, I think we have no way to to access the server error message. Please let me know if I'm wrong.

User *user = [[User alloc] init]; user.email = @"email@foo.com"; user.password = @"password";

NSRRequest request = [NSRRequest POST]; [request routeTo:@"/users/sign_in.json"]; [request setBodyToObject:user]; NSError error; [request sendSynchronous:&error];

dingbat commented 11 years ago

The error message should be received by NSRails and be accessible from your NSError object, like so:

[error.userInfo objectForKey:NSLocalizedDescriptionKey]

If you've tried this, what is the issue exactly? Is "error" nil when there really was an error? Is "error" non-nil but without the error message?

Worst case, I would set a breakpoint at sendSynchronous, step through the code and see what's happening. Everything is well documented and debugging this on your end with your data could really help me out.

tomoyuki28jp commented 11 years ago

Thanks for your reply, @dingbat.

Rails server response: render status:401, json:{message:"Invalid email or passoword."}

iOS code: [error.userInfo objectForKey:NSLocalizedDescriptionKey] // => 'Expected status code in (200-299), got 401'

Is there a way to get {message:"Invalid email or passoword."}? Somebody in #iphonedev channel says that the 401 response does not contain a message-body, but I could not found such description in the RFC specification.

dingbat commented 11 years ago

Hm. Try this before your call and try it again.

[NSRConfig defaultConfig].succinctErrorMessages = NO;
dingbat commented 11 years ago

@tomoyuki28jp any updates?

tomoyuki28jp commented 11 years ago

I've tried succinctErrorMessages, but still I cannot get the server error response body. I'm seeing following output in my Xcode debug console.

[NSRails][connection] Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x81a07a0 {NSErrorFailingURLKey=http://localhost:3000/tokens.json, NSErrorFailingURLStringKey=http://localhost:3000/tokens.json, NSUnderlyingError=0x81a16e0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1012.)"}

dingbat commented 11 years ago

Should be resolved now.

Update your NSRails to the latest version. You should be able to retrieve the JSON data from the error using the NSRErrorResponseBodyKey key in the error's userInfo.

dingbat commented 11 years ago

@tomoyuki28jp yes? no?

tomoyuki28jp commented 11 years ago

Sorry for this late response, @dingbat. I confirmed that works. Thanks!