ahoward/main

Catching of Main::Parameter::NoneSuch exception

joonty opened this issue · 0 comments

Hi,

Thanks for an excellent tool.

I have a question about the behaviour of catching exceptions, specifically the exception thrown when trying to access a parameter that doesn't exist. The exception thrown is Main::Parameter::NoneSuch, and the exception message is the string representation of the key used with the params object. This is fine, except for the fact that Main catches the exception and simply prints out the exception message, which doesn't have any context associated with it.

Reading that back, it may be a little hard to understand what I mean. So here's an example:

Main {
  def run
    # Try to access non-existent param
    params[:nothing]
  end
}

The output of this:

$ ruby example.rb
nothing

If you're not used to working with Main you could end up tearing you hair out, trying to work out why your program has stopped running and it's now spitting out a single word (this may or may not have happened to me).

I would propose either re-raising the exception (it certainly is exceptional behaviour, and it should only ever happen in development) or at least printing the exception class name, e.g. Main::Parameter::NoneSuch => nothing. Or perhaps catch that exception specifically and print out a helpful message.

I'm happy to fork and code a possible solution if you would be willing to look over it?

Thanks