Good Error Messages Point the Way

   By chris on May 31st 2008 in /dev/random | 906 views

Ran into an odd error today, the result of this line in a library I’m using:

raise '@level is blank. Did you override the allow_to method in your controller?' if @level.blank?

A quick check of my code and “err no, I don’t think so. Should I?”

Yes I should, and in that then call super() to initialize @level. So I rewrote the error message in that lib to read as the following, because I know it’s going to bite me again some time:

raise '@level is blank. Did you override the allow_to method in your controller? You should, and call super() from within it.' if @level.blank?

Bad error messages throw up an error number. Mediocre ones hint at the problem. Good error messages point the way to a solution.

2 Responses to “Good Error Messages Point the Way”

  1. AG responded on 30 Dec 2008 at 5:57 am #

    ok, good point about the nature and evolution of error messages. and i’m happy i found your post, thanks man.

    for this particular problem though, I don’t get why super would help, and if you figured out how, could you please paste some code and some more background information? it’s just that inventing the wheel over and over again ….

    just because “Bad blog posts throw up blog topics. Mediocre ones hint at the content. Good blog posts point the way to a better day.” ;-)

  2. chris responded on 30 Dec 2008 at 10:05 am #

    Well, I do actually describe the solution, just not as a single block of code. I suppose I could have been more clear. If you get this error above you actually do need to over-ride the allow_to method in your controller and call super() from within it, like so:

    controller Blah
    def allow_to()
    # Do your own stuff here
    super()
    end
    end

    From the Loved By Less source we see this sort of example:

    protected
    def allow_to
    super :all, :all=>true
    end

    We need to call super() because that throws the call back up to the ApplicationController which has:

    def allow_to( level = nil, args = {} )
    return unless level
    @level ||= []
    @level << [level, args]
    end

    the block of code that handles permissions for actions in the project.