diff --git a/lib/rake/application.rb b/lib/rake/application.rb index d9439429d..43e6ecf60 100644 --- a/lib/rake/application.rb +++ b/lib/rake/application.rb @@ -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. diff --git a/test/test_rake_application.rb b/test/test_rake_application.rb index aa5ed39f8..f0407edf2 100644 --- a/test/test_rake_application.rb +++ b/test/test_rake_application.rb @@ -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 @@ -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 @@ -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