rubymotion-community / BubbleWrap

Cocoa wrappers and helpers for RubyMotion (Ruby for iOS and OS X) - Making Cocoa APIs more Ruby like, one API at a time. Fork away and send your pull requests
Other
1.18k stars 208 forks source link

BubbleWrap::HTTP response not populating response.error_message #136

Closed utx0 closed 12 years ago

utx0 commented 12 years ago

I think this is a bug in BW.....

I have a services that returns an error message and status code like the working example I have setup below. The problem I am having is that the response.error_message is not populated with the error message that the services is returning. However its returned in the body?

BubbleWrap::HTTP.post("http://httpstat.us/502") do |response|
      puts "response = #{response}"
      puts "response.body = #{response.body.to_str}"
      puts "response.error_message = #{response.error_message}"
      puts "response.status_code = #{response.status_code.to_s}"
      puts "response ok = #{response.ok?}"
 end

Here is the out put I am getting:

response = #<BubbleWrap::HTTP::Response:130956416 - url: #<NSURL:0x7ce4050>, body: #     <NSConcreteData:0x7b97a90>, headers: {"Access-Control-Allow-Origin"=>"*", "Via"=>"1.1     ngphm5fe02.tmof.in.telstra.com.au:3128", "Server"=>"nginx", "Content-Type"=>"text/plain", "Content-Length"=>"15",     "Connection"=>"close", "Date"=>"Wed, 05 Sep 2012 06:34:12 GMT"}, status code: 502, error message:  >
response.body = 502 Bad Gateway
response.error_message = 
response.status_code = 502
response ok = false

I have tried to workout if the error message needs to be past back in any special format, but am currently unsure? Please advise if this is the case. Thanks heaps. Luke

dmarkow commented 12 years ago

It's returned in the body because the httpstat.us website puts it in the body (i.e. this isn't BW doing that).

However, I'd think the error_message should contain "Bad Gateway". I'll investigate when I get a chance.

dmarkow commented 12 years ago

Ok, it looks like the error_message field is actually for iOS's URL loading error codes (e.g. "can't find host", "DNS lookup failed", "bad url", etc.) For example, using an invalid url with a typo: (I added the rescue clauses, otherwise it crashes trying to convert nil using to_str)

BW::HTTP.post("http://httpstat.uz/502") do |response|
  puts "response = #{response}"
  puts "response.body = #{response.body.to_str rescue ''}"
  puts "response.error_message = #{response.error_message}"
  puts "response.status_code = #{response.status_code.to_s rescue ''}"
  puts "response ok = #{response.ok?}"
end

gives you:

response = #<BubbleWrap::HTTP::Response:146317216 - url: , body: , headers: , status code: , error message: A server with the specified hostname could not be found. >
2012-09-07 11:15:12.098 gesture[23238:c07] http.rb:372:in `call_delegator_with_response': undefined method `to_str' for nil:NilClass (NoMethodError)
    from http.rb:178:in `connection:didFailWithError:'
response.body = 
response.error_message = A server with the specified hostname could not be found.
response.status_code = 
response ok = false

I think instead of making error_message mean multiple things, we should add something like status_description (we can use NSHTTPURLResponse.localizedStringForStatusCode(...) to get it).

dmarkow commented 12 years ago

You can now get status descriptions:

puts "response.status_code = #{response.status_code.to_s}"
puts "response.status_description = #{response.status_description}"
response.status_code = 502
response.status_description = bad gateway