ruby-changes:53178
From: naruse <ko1@a...>
Date: Sun, 28 Oct 2018 06:48:25 +0900 (JST)
Subject: [ruby-changes:53178] naruse:r65393 (trunk): Print exception's cause like Java
naruse 2018-10-28 06:45:30 +0900 (Sun, 28 Oct 2018) New Revision: 65393 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=65393 Log: Print exception's cause like Java Print `cause` of the exception if the exception is not caught and printed its backtraces and error message [Feature #8257] Modified files: trunk/NEWS trunk/eval_error.c trunk/test/ruby/test_backtrace.rb Index: test/ruby/test_backtrace.rb =================================================================== --- test/ruby/test_backtrace.rb (revision 65392) +++ test/ruby/test_backtrace.rb (revision 65393) @@ -297,4 +297,36 @@ class TestBacktrace < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/ruby/test_backtrace.rb#L297 end assert_not_match(/\Acore#/, e.backtrace_locations[0].base_label) end + + def test_notty_backtrace + err = ["-:1:in `<main>': unhandled exception"] + assert_in_out_err([], "raise", [], err) + + err = ["-:2:in `foo': foo! (RuntimeError)", + "\tfrom -:4:in `<main>'"] + assert_in_out_err([], <<-"end;", [], err) + def foo + raise "foo!" + end + foo + end; + + err = ["-:7:in `rescue in bar': bar! (RuntimeError)", + "\tfrom -:4:in `bar'", + "\tfrom -:9:in `<main>'", + "\t2: from -:9:in `<main>'", + "\t1: from -:5:in `bar'", + "-:2:in `foo': foo! (RuntimeError)"] + assert_in_out_err([], <<-"end;", [], err) + def foo + raise "foo!" + end + def bar + foo + rescue + raise "bar!" + end + bar + end; + end end Index: eval_error.c =================================================================== --- eval_error.c (revision 65392) +++ eval_error.c (revision 65393) @@ -220,6 +220,29 @@ print_backtrace(const VALUE eclass, cons https://github.com/ruby/ruby/blob/trunk/eval_error.c#L220 } } +VALUE rb_get_message(VALUE exc); + +static void +show_cause(VALUE errinfo, VALUE str, VALUE highlight, VALUE reverse) +{ + VALUE cause = rb_attr_get(errinfo, id_cause); + if (!NIL_P(cause)) { + volatile VALUE eclass = CLASS_OF(cause); + VALUE errat = rb_get_backtrace(cause); + VALUE emesg = rb_get_message(cause); + if (reverse) { + show_cause(cause, str, highlight, reverse); + print_errinfo(eclass, errat, emesg, str, highlight!=0); + print_backtrace(eclass, errat, str, FALSE); + } + else { + print_backtrace(eclass, errat, str, TRUE); + print_errinfo(eclass, errat, emesg, str, highlight!=0); + show_cause(cause, str, highlight, reverse); + } + } +} + void rb_error_write(VALUE errinfo, VALUE emesg, VALUE errat, VALUE str, VALUE highlight, VALUE reverse) { @@ -254,17 +277,17 @@ rb_error_write(VALUE errinfo, VALUE emes https://github.com/ruby/ruby/blob/trunk/eval_error.c#L277 len = p - (msg = buff); } write_warn2(str, msg, len); + show_cause(errinfo, str, highlight, reverse); print_backtrace(eclass, errat, str, TRUE); print_errinfo(eclass, errat, emesg, str, highlight!=0); } else { print_errinfo(eclass, errat, emesg, str, highlight!=0); print_backtrace(eclass, errat, str, FALSE); + show_cause(errinfo, str, highlight, reverse); } } -VALUE rb_get_message(VALUE exc); - void rb_ec_error_print(rb_execution_context_t * volatile ec, volatile VALUE errinfo) { Index: NEWS =================================================================== --- NEWS (revision 65392) +++ NEWS (revision 65393) @@ -36,6 +36,12 @@ sufficient information, see the ChangeLo https://github.com/ruby/ruby/blob/trunk/NEWS#L36 user = users.find {|user| cond(user) } +* Print exception backtrace and error message in reverse order when the + exception is not caught and STDOUT is unchanged and a tty. [Feature #8661] + +* Print `cause` of the exception if the exception is not caught and printed + its backtraces and error message [Feature #8257] + === Core classes updates (outstanding ones only) [Array] -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/