Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
pupeno opened this issue Feb 5, 2014 · 1 comment
Closed

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

pupeno opened this issue Feb 5, 2014 · 1 comment

Comments

@pupeno
Copy link

pupeno commented Feb 5, 2014

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.

@jimweirich
Copy link
Owner

Implemented in master. Will be in next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants