Thursday, April 01, 2010

Gracefully support HTTP errors

require 'rubygems'
require 'mechanize'

a = { |agent| agent.user_agent_alias = 'Mac Safari'  }
a.get(:url => '',
      :headers => {'Accept' => 'text/xml', 'Connection' =>'keep-live'}

If mechanize found error code '401' (for example) then mechanize will raise error message:
`fetch_page': 401 => Net::HTTPUnauthorized (Mechanize::ResponseCodeError)
from /usr/lib/ruby/gems/1.8/gems/mechanize-1.0.0/lib \
/mechanize.rb:259:in `get'
from agent-test.rb:6

I dont know but what i really want from this gem is return 'http error message' rather than raise an error and die (if they found http error codes).

So far to get things done maybe i'll replace this line of code (mechanize.rb, def fetch_page)
raise unless @user || @password
return page unless @user || @password

Also you can watch the request here

Maybe using rescue is better
  a.get(:url => '',
        :headers => {
                     'Accept' => 'text/xml', 
                     'Connection' =>'keep-alive'
  rescue WWW::Mechanize::ResponseCodeError
  puts $!.page.body

No comments: