[Rant] How to abort with helpful info - the rant way?

Stefan Lang langstefan at gmx.at
Thu Dec 22 12:20:45 EST 2005

On Thursday 22 December 2005 02:34, Kevin Burge wrote:
> Stefan,
> I'm getting an error like:
> rant: [ERROR] in file `C:/home/kevin/work/openssl/root.rant', line
> 31: #<Rant::Generators::Project:0x2b579b0> couldn't locate project
> file for dependency "zlib_zlib"
> Exception `Rant::RantAbortException' at
> C:/home/kevin/work/dev/run/ruby/lib/ruby/site_ruby/1.8/rant/rantlib
>.rb:765 - Ran
> t::RantAbortException
> The generator is defined in import/raven.rb, but nowhere in this
> output do I have any indication of the location in raven.rb.
> Can you give a quick run-down on ways of "getting out" of a rant
> file. rant.abort, rant.abort_at, ruby's exit?  What do you
> recommend?

To allow better understanding, first some more bits on Rant's

  The "rant" command creates a new Rant::RantApp instance and calls
its "run" method, which initiates Rantfile reading, task invocation
etc. The "run" method catches any exception, prints information
about it to stderr and returns 1 (0 if no exception is thrown).

  The ususal way to abort Rant would be to raise an
Rant::RantAbortException. The method "rant.abort" does just that,
and printsan optionally given error message to stderr first.

  The method "rant.abort_at" is like "rant.abort", except
that it takes a hash { :file => "file name", :ln => <line number>}
as first argument, which it will interpret as error location.
Use this method to report wrong usage of a generator, e.g.

  def Project.rant_gen(rant, ch, args, &block)
    if args.empty?
      rant.abort_at(ch, "Project: project name required as first 

If the file "sub.rant", in line number 45, contains the code
  gen Project

Running rant will print the error message:

rant: [ERROR] in file `/home/stefan/project/foo/sub.rant', line 45:
              Project: project name required as first argument
rant aborted!

  If you run rant with the option --trace-abort (not officially
supported), the abort/abort_at method will print a full backtrace.
(Sadly enough, this backtrace often won't be useful, because
an exception thrown from within a task block is catched and
a new one thrown => original backtrace lost :( ).

  To abort from within an Rantfile, I'd recommend to just throw
an exception, either of a custom class or a simple StandardError.

  task :default do
    # ...
    raise "some error"

Running this task will give something like:

  rant: [ERROR] in file `/home/stefan/Rantfile', line 30:
                some error
  rant: [ERROR] Task `default' fail.
  rant aborted!

To just print an error/warning message but continue:

  rant.warn_msg "there is something suspicious"
  rant.err_msg "something is faulty"

Don't do a direct "exit".


More information about the make-cafe mailing list