Skip to content

Commit

Permalink
Add display of cause exception.
Browse files Browse the repository at this point in the history
Also fixed tests where assert_raises interacted badly with capture_io.
  • Loading branch information
jimweirich committed Feb 13, 2014
1 parent 5ac9df0 commit fbb22e7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
14 changes: 11 additions & 3 deletions lib/rake/application.rb
Expand Up @@ -184,15 +184,23 @@ def exit_because_of_exception(ex)
# Display the error message that caused the exception.
def display_error_message(ex)
trace "#{name} aborted!"
display_exception_details(ex)
trace "Tasks: #{ex.chain}" if has_chain?(ex)
trace "(See full trace by running task with --trace)" unless
options.backtrace
end

def display_exception_details(ex)
trace ex.message
if options.backtrace
trace ex.backtrace.join("\n")
else
trace Backtrace.collapse(ex.backtrace).join("\n")
end
trace "Tasks: #{ex.chain}" if has_chain?(ex)
trace "(See full trace by running task with --trace)" unless
options.backtrace

if ex.respond_to?(:cause) && ex.cause
display_exception_details(ex.cause)
end
end

# Warn about deprecated usage.
Expand Down
43 changes: 34 additions & 9 deletions test/test_rake_application.rb
Expand Up @@ -391,10 +391,10 @@ def test_bad_run
@app.intern(Rake::Task, "default").enhance { fail }
ARGV.clear
ARGV << '-f' << '-s' << '--rakelib=""'
assert_raises(SystemExit) {
_, err = capture_io { @app.run }
assert_match(/see full trace/, err)
_, err = capture_io {
assert_raises(SystemExit){ @app.run }
}
assert_match(/see full trace/i, err)
ensure
ARGV.clear
end
Expand All @@ -403,10 +403,10 @@ def test_bad_run_with_trace
@app.intern(Rake::Task, "default").enhance { fail }
ARGV.clear
ARGV << '-f' << '-s' << '-t'
assert_raises(SystemExit) {
_, err = capture_io { @app.run }
refute_match(/see full trace/, err)
_, err = capture_io {
assert_raises(SystemExit) { @app.run }
}
refute_match(/see full trace/i, err)
ensure
ARGV.clear
end
Expand All @@ -415,10 +415,35 @@ def test_bad_run_with_backtrace
@app.intern(Rake::Task, "default").enhance { fail }
ARGV.clear
ARGV << '-f' << '-s' << '--backtrace'
assert_raises(SystemExit) {
_, err = capture_io { @app.run }
refute_match(/see full trace/, err)
_, err = capture_io {
assert_raises(SystemExit) {
@app.run
}
}
refute_match(/see full trace/, err)
ensure
ARGV.clear
end

def test_printing_original_exception_cause
custom_error = Class.new(StandardError)
@app.intern(Rake::Task, "default").enhance {
begin
raise custom_error, "Original Error"
rescue custom_error => ex
raise custom_error, "Secondary Error"
end
}
ARGV.clear
ARGV << '-f' << '-s'
_ ,err = capture_io {
assert_raises(SystemExit) {
@app.run
$stdout.puts "DBG: err=#{err.inspect}"
}
}
assert_match(/Original Error/, err)
assert_match(/Secondary Error/, err)
ensure
ARGV.clear
end
Expand Down

0 comments on commit fbb22e7

Please sign in to comment.