ChangingThePresent
The Few, The Proud, The Pradipta 416
My Resume
Web DOC PDF
RTF ODT TXT
powered by emurse

making errors better

bryan / 24.Apr.2008

First, I haven’t written in awhile. Like many bloggers that I follow, success in one area diminishes my capacity in other areas. My contracting business is doing very well. More than that however, I don’t like to waste space or mince words on my blog, as you have already noticed. I won’t write unless I have something that I think is worth sharing; I won’t write just to keep a fresh article in the feeds.

I have been doing a lot of debugging – mostly of the needle-in-a-haystack variety. Simultaneously I am working on a new, re-engineered version of the legacy app I am debugging. So I have the opportunity to do some things “right” this time (I didn’t write the legacy app, and the developer who did is AWESOME and a friend. I am not criticizing the code here. What always happens is the problems and business rules become clearer as the project matures, giving rise to an opportunity to craft a better model)

There are of course great plugins to do some of this, and more. I personally have found that in some cases less is more and projects that rely to heavily on 3rd party code may end up paying for at debug time.

Here are some tweaks to errors and whatnot that I use:

1. extend StandardError with a simple to_xml method, for use in ActiveResource and pretty much anywhere:

class StandardError
  def to_xml
    return <<-ENDOFDOC
      <error type='#{self.class.to_s}'>
        <message>#{message}</message>
      </error>
    ENDOFDOC
  end
end
1.5. UPDATE: I also added a status method, since I use resources so heavily.

@status=400 #its never the server's fault
attr_accessor :status
and in the custom errors mentioned below, change as needed:

LocalApplication::SomeParameterIsMissing.new.status # => 406

This works nicely for StandardError and any descendent classes that don’t already have a to_xml method. For ActiveRecord:: and some other error classes, there is a to_xml which will override this one.

2. put a general error handler in ApplicationController


def handle_error(err=nil)
  begin
    if err.nil?
      @error = GenericNilError.new("Oops, something happened...")
    else
      @error = err
    end
    respond_to do |flavah|
      flavah.xml { render :xml => @error.to_xml, status => 500 and return false }
      flavah.html  # a template for error
    end
  rescue StandardError => e
    # some failsafe that hits a static
    render :nothing => true, :status => 500 and return false
  end
end
3. use local errors As in the example above, roll your own errors. Then you respond in customized ways. If you’re not already rescuing different kinds of errors and handling them in unique ways, you may want to explore that.

module LocalApplicationError
  class GenericError < StandardError ; end
  class ShouldNeverHappenError < StandardError ; end
  # etc...
end
4. have an error log So much better than scrolling through #{RAILS_ENV}.log

err_logger = Logger.new #customize how you want it
[ActiveRecord ActiveResource ActionController].each do |mod|
  mod.module_eval <<- ENDOFMONKEY
    include LocalApplicationError #why not throw this in there too, see #3 above
    @err_logger = err_logger
    mattr_reader :err_logger
  ENDOFMONKEY
end

5. Use specs so you don’t raise unknown errors!

Leave a Comment

back to top

micro theme by seaofclouds, and powered with Mephisto Hosting by hostingrails.com