jimweirich / rake

A make-like build utility for Ruby.

Home Page:http://rake.rubyforge.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Print the cause of an exception as well as the exception itself

pupeno opened this issue · comments

Currently when there's an exception in a rake task, the message and backtrace of it is printed, but if that exception is wrapping another one, the inner one is ignored. Sometimes, that information is essential to be able to trace and fix a bug. It would be nice if rake printed that information.

I managed to make rake print it by monkey patching it with:

module Rake
  class Application
    def display_error_message(ex)
      trace "#{name} aborted!"
      display_exception(ex)
      trace "Tasks: #{ex.chain}" if has_chain?(ex)
      trace "(See full trace by running task with --trace)" unless options.backtrace
    end

    private

    def display_exception(ex, margin="")
      trace "#{margin}#{ex.message}"
      if options.backtrace
        trace "#{margin}#{ex.backtrace.join("\n#{margin}")}"
      else
        trace "#{margin}#{Backtrace.collapse(ex.backtrace).join("\n#{margin}")}"
      end
      if ex.respond_to?(:cause) && !ex.cause.nil? # Ruby < 2.1.0 doesn't have *cause*
        trace "#{margin}which was caused by:"
        display_exception(ex.cause, "#{margin}  ")
      end
    end
  end
end

If you would accept something like this in rake itself, I can create the appropriate pull request.

Implemented in master. Will be in next release.