ruby-changes:31557
From: nobu <ko1@a...>
Date: Sun, 10 Nov 2013 22:16:39 +0900 (JST)
Subject: [ruby-changes:31557] nobu:r43636 (trunk): error.c: Exception#cause
nobu 2013-11-10 22:16:33 +0900 (Sun, 10 Nov 2013) New Revision: 43636 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=43636 Log: error.c: Exception#cause * error.c (exc_cause): captured previous exception. * eval.c (make_exception): capture previous exception automagically. [Feature #8257] Modified files: trunk/ChangeLog trunk/error.c trunk/eval.c trunk/test/ruby/test_exception.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 43635) +++ ChangeLog (revision 43636) @@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sun Nov 10 22:16:19 2013 Nobuyoshi Nakada <nobu@r...> + + * error.c (exc_cause): captured previous exception. + + * eval.c (make_exception): capture previous exception automagically. + [Feature #8257] + Sun Nov 10 08:37:20 2013 Zachary Scott <e@z...> * thread.c: [DOC] Remove duplicate reference Index: eval.c =================================================================== --- eval.c (revision 43635) +++ eval.c (revision 43636) @@ -641,6 +641,10 @@ make_exception(int argc, VALUE *argv, in https://github.com/ruby/ruby/blob/trunk/eval.c#L641 if (argc > 2) set_backtrace(mesg, argv[2]); } + { + VALUE cause = get_errinfo(); + if (!NIL_P(cause)) rb_iv_set(mesg, "cause", cause); + } return mesg; } Index: error.c =================================================================== --- error.c (revision 43635) +++ error.c (revision 43636) @@ -780,6 +780,14 @@ rb_exc_set_backtrace(VALUE exc, VALUE bt https://github.com/ruby/ruby/blob/trunk/error.c#L780 return exc_set_backtrace(exc, bt); } +VALUE +exc_cause(VALUE exc) +{ + ID id_cause; + CONST_ID(id_cause, "cause"); + return rb_attr_get(exc, id_cause); +} + static VALUE try_convert_to_exception(VALUE obj) { @@ -1742,6 +1750,7 @@ Init_Exception(void) https://github.com/ruby/ruby/blob/trunk/error.c#L1750 rb_define_method(rb_eException, "inspect", exc_inspect, 0); rb_define_method(rb_eException, "backtrace", exc_backtrace, 0); rb_define_method(rb_eException, "set_backtrace", exc_set_backtrace, 1); + rb_define_method(rb_eException, "cause", exc_cause, 0); rb_eSystemExit = rb_define_class("SystemExit", rb_eException); rb_define_method(rb_eSystemExit, "initialize", exit_initialize, -1); Index: test/ruby/test_exception.rb =================================================================== --- test/ruby/test_exception.rb (revision 43635) +++ test/ruby/test_exception.rb (revision 43636) @@ -476,4 +476,19 @@ end.join https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L476 def test_stackoverflow assert_raise(SystemStackError){m} end + + def test_cause + msg = "[Feature #8257]" + e = assert_raise(StandardError) { + begin + raise msg + rescue => e + assert_nil(e.cause, msg) + raise StandardError + end + } + cause = e.cause + assert_instance_of(RuntimeError, cause, msg) + assert_equal(msg, cause.message, msg) + end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/