CommandLine :: Application is swallowing my exceptions mostly, how to avoid this?

Example:

require 'commandline'
class App < CommandLine::Application
    def initialize
    end
    def main
        raise 'foo'
    end
end

      

leads to

$ ruby test.rb
ERROR: foo

      

And this is where the problem starts: During development, there will always be exceptions thrown deep somewhere in my code, and I need to see the stack, not some garbled message.

Thanks to rampion , I am using this solution:

require 'commandline'
class App < CommandLine::Application
    def initialize
    end
    def main
        raise 'foo'
        rescue Exception => e
            puts format_like_real_exception e
    end
    def format_like_real_exception(e)
        s = ''
        b = e.backtrace
        s << b.shift << ': ' << e.message << "\n"
        b.each { |l| s << "\t" << l << "\n" }
        s
    end
end

      

Of course, no formatter is needed, but I prefer it the way it is originally formatted.

0


a source to share


2 answers


Alternatively, you can simply troubleshoot errors in main

:



require 'rubygems'
require 'commandline'
class App < CommandLine::Application
    def initialize
    end
    def main
        raise 'foo'
    rescue Exception => e
        puts "BACKTRACE:"
        puts e.backtrace
    end
end

      

+1


a source


The solution I have found so far:

  • Change CommandLine::Application

    toCommandLine::Application_wo_AutoRun

  • rename the method main

    to something else that is not used, for example `start
  • calling a static method App.run

    that returns an instance and then calling your method start

    on it

Complete example:



require 'commandline'
class App < CommandLine::Application_wo_AutoRun
    def initialize
    end
    def start
        raise 'foo'
    end
end
app = App.run
app.start

      

the documentation mentions Application_wo_AutoRun

but does not help how to proceed. Only the study of sources helped here.

0


a source







All Articles