I'm trying to be able to have a global exception capture where I can add extra information when an error happens. I have two classes, "crawler" and "amazon". What I want to do is be able to call "crawl", execute a function in amazon, and use the exception handling in the crawl function.
Here are the two classes I have:
require 'mechanize'
class Crawler
  Mechanize.html_parser = Nokogiri::HTML
  def initialize
    @agent = Mechanize.new
  end
  def crawl
    puts "crawling"
    begin
      #execute code in Amazon class here?
    rescue Exception => e
      puts "Exception: #{e.message}"
      puts "On url: #{@current_url}"
      puts e.backtrace
    end
  end
  def get(url)
    @current_url = url
    @agent.get(url)
  end
end
class Amazon < Crawler
  #some code with errors
  def stuff
    page = get("http://www.amazon.com")
    puts page.parser.xpath("//asldkfjasdlkj").first['href']
  end
end
a = Amazon.new
a.crawl
Is there a way I can call "stuff" inside of "crawl" so I can use that exception handling over the entire stuff function? Is there a better way to accomplish this?
EDIT: This is when I ended up doing
require 'mechanize'
class Crawler
  Mechanize.html_parser = Nokogiri::HTML
  def initialize
    @agent = Mechanize.new
  end
  def crawl
    yield
  rescue Exception => e
    puts "Exception: #{e.message}"
    puts "On url: #{@current_url}"
    puts e.backtrace
  end
  def get(url)
    @current_url = url
    @agent.get(url)
  end
end
c = Crawler.new
c.crawl do
  page = c.get("http://www.amazon.com")
  puts page.parser.xpath("//asldkfjasdlkj").first['href']
end
 
     
    