1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

error.c: Exception#cause

* error.c (exc_cause): captured previous exception.
* eval.c (make_exception): capture previous exception automagically.
  [Feature #8257]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43636 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-11-10 13:16:33 +00:00
parent 2fff948394
commit b050cc5ab9
4 changed files with 35 additions and 0 deletions

View file

@ -1,3 +1,10 @@
Sun Nov 10 22:16:19 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* 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@zzak.io>
* thread.c: [DOC] Remove duplicate reference

View file

@ -780,6 +780,14 @@ rb_exc_set_backtrace(VALUE exc, VALUE bt)
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)
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);

4
eval.c
View file

@ -641,6 +641,10 @@ make_exception(int argc, VALUE *argv, int isstr)
if (argc > 2)
set_backtrace(mesg, argv[2]);
}
{
VALUE cause = get_errinfo();
if (!NIL_P(cause)) rb_iv_set(mesg, "cause", cause);
}
return mesg;
}

View file

@ -476,4 +476,19 @@ end.join
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