Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/mruby/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ void mrb_codedump_all_file(mrb_state *mrb, struct RProc *proc, FILE *out);
mrb_noreturn void mrb_raise_nomemory(mrb_state *mrb);
mrb_value mrb_exc_inspect(mrb_state *mrb, mrb_value exc);
mrb_value mrb_exc_backtrace(mrb_state *mrb, mrb_value exc);
mrb_noreturn void mrb_exc_raise_with_cause(mrb_state *mrb, mrb_value exc, mrb_value cause);
mrb_value mrb_get_backtrace(mrb_state *mrb);
void mrb_exc_mesg_set(mrb_state *mrb, struct RException *exc, mrb_value mesg);
mrb_value mrb_exc_mesg_get(mrb_state *mrb, struct RException *exc);
Expand Down
4 changes: 2 additions & 2 deletions mrbgems/mruby-kernel-ext/src/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ mrb_f_hash(mrb_state *mrb, mrb_value self)
}

static const mrb_mt_entry kernel_ext_rom_entries[] = {
MRB_MT_ENTRY(mrb_f_raise, MRB_SYM(fail), MRB_ARGS_OPT(2) | MRB_MT_PRIVATE),
MRB_MT_ENTRY(mrb_f_raise, MRB_SYM(fail), MRB_ARGS_OPT(2) | MRB_ARGS_KEY(1,1) | MRB_MT_PRIVATE),
MRB_MT_ENTRY(mrb_f_caller, MRB_SYM(caller), MRB_ARGS_OPT(2) | MRB_MT_PRIVATE),
MRB_MT_ENTRY(mrb_f_method, MRB_SYM(__method__), MRB_ARGS_NONE() | MRB_MT_PRIVATE),
MRB_MT_ENTRY(mrb_f_callee, MRB_SYM(__callee__), MRB_ARGS_NONE() | MRB_MT_PRIVATE),
Expand All @@ -387,7 +387,7 @@ static const mrb_mt_entry kernel_ext_rom_entries[] = {
/* public counterparts on `Kernel` itself; see `kernel_module_function_entries`
in src/kernel.c */
static const mrb_mt_entry kernel_ext_module_function_entries[] = {
MRB_MT_ENTRY(mrb_f_raise, MRB_SYM(fail), MRB_ARGS_OPT(2)),
MRB_MT_ENTRY(mrb_f_raise, MRB_SYM(fail), MRB_ARGS_OPT(2) | MRB_ARGS_KEY(1,1)),
MRB_MT_ENTRY(mrb_f_caller, MRB_SYM(caller), MRB_ARGS_OPT(2)),
MRB_MT_ENTRY(mrb_f_method, MRB_SYM(__method__), MRB_ARGS_NONE()),
MRB_MT_ENTRY(mrb_f_callee, MRB_SYM(__callee__), MRB_ARGS_NONE()),
Expand Down
88 changes: 88 additions & 0 deletions src/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,92 @@ exc_set_backtrace(mrb_state *mrb, mrb_value exc)
return backtrace;
}

/*
* call-seq:
* exception.cause -> an_exception or nil
*
* Returns the exception that was being handled (`$!`) when this one was
* raised, or the one given to `raise` as `cause:`; nil if there was none.
*
* begin
* begin
* raise "a"
* rescue
* raise "b"
* end
* rescue => e
* e.cause #=> #<RuntimeError: a>
* end
*/
static mrb_value
exc_cause(mrb_state *mrb, mrb_value exc)
{
return mrb_iv_get(mrb, exc, MRB_SYM(cause));
}

/* Set on an exception that `raise` was given `cause:` for, so that raising it
takes no cause from `$!`; cleared as it is raised. */
#define EXC_CAUSE_GIVEN (1 << 17)

/* Whether exc is cause or one of the causes behind it. */
static mrb_bool
exc_cause_chain_p(mrb_state *mrb, mrb_value cause, mrb_value exc)
{
while (mrb_exception_p(cause)) {
if (mrb_obj_eq(mrb, cause, exc)) return TRUE;
cause = mrb_iv_get(mrb, cause, MRB_SYM(cause));
}
return FALSE;
}

/*
* The exception `$!` names, the one a rescue or an ensure clause is running
* for, becomes the cause of an exception raised there, as in CRuby. One that
* already has a cause keeps it, and none is taken that would make the chain
* of causes a cycle.
*/
static void
exc_set_implicit_cause(mrb_state *mrb, mrb_value exc)
{
struct RException *e = mrb_exc_ptr(exc);

if (e->flags & EXC_CAUSE_GIVEN) {
e->flags &= ~EXC_CAUSE_GIVEN;
return;
}
/* `!` is not a word, so MRB_GVSYM() cannot spell the name */
mrb_value cause = mrb_gv_get(mrb, mrb_intern_lit(mrb, "$!"));
if (!mrb_exception_p(cause)) return;
if (!mrb_nil_p(mrb_iv_get(mrb, exc, MRB_SYM(cause)))) return;
if (exc_cause_chain_p(mrb, cause, exc)) return;
mrb_iv_set(mrb, exc, MRB_SYM(cause), cause);
}

/*
* Raises exc with the cause `raise` was given: an exception, which becomes
* the cause, or nil, which takes none from `$!` for this raise. A cause that
* is exc itself is ignored, and one that has exc behind it is refused.
*/
mrb_noreturn void
mrb_exc_raise_with_cause(mrb_state *mrb, mrb_value exc, mrb_value cause)
{
if (mrb_exception_p(exc)) {
if (!mrb_nil_p(cause)) {
if (!mrb_exception_p(cause)) {
mrb_raise(mrb, E_TYPE_ERROR, "exception object expected");
}
if (!mrb_obj_eq(mrb, cause, exc)) {
if (exc_cause_chain_p(mrb, cause, exc)) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "circular causes");
}
mrb_iv_set(mrb, exc, MRB_SYM(cause), cause);
}
}
mrb_exc_ptr(exc)->flags |= EXC_CAUSE_GIVEN;
}
mrb_exc_raise(mrb, exc);
}

void
mrb_exc_set(mrb_state *mrb, mrb_value exc)
{
Expand All @@ -192,6 +278,7 @@ mrb_exc_set(mrb_state *mrb, mrb_value exc)
}
if (mrb->exc != mrb->nomem_err && !mrb_frozen_p(mrb->exc)) {
mrb_keep_backtrace(mrb, exc);
if (mrb_exception_p(exc)) exc_set_implicit_cause(mrb, exc);
}
}
}
Expand Down Expand Up @@ -932,6 +1019,7 @@ static const mrb_mt_entry exception_rom_entries[] = {
MRB_MT_ENTRY(mrb_exc_inspect, MRB_SYM(inspect), MRB_ARGS_NONE()),
MRB_MT_ENTRY(mrb_exc_backtrace, MRB_SYM(backtrace), MRB_ARGS_NONE()),
MRB_MT_ENTRY(exc_set_backtrace, MRB_SYM(set_backtrace), MRB_ARGS_REQ(1)),
MRB_MT_ENTRY(exc_cause, MRB_SYM(cause), MRB_ARGS_NONE()),
};

void
Expand Down
30 changes: 27 additions & 3 deletions src/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -547,16 +547,37 @@ mrb_false(mrb_state *mrb, mrb_value self)
*
* raise "Failed to create socket"
* raise ArgumentError, "No parameters", caller
*
* The exception being handled (`$!`) becomes the cause of the one raised;
* `cause:` gives another, or with nil none.
*
* raise ArgumentError, "bad value", cause: original_error
*/
mrb_value
mrb_f_raise(mrb_state *mrb, mrb_value self)
{
mrb_value exc, mesg;
mrb_int argc = mrb_get_args(mrb, "|oo", &exc, &mesg);
mrb_sym kw_names[] = { MRB_SYM(cause) };
mrb_value cause, opts;
mrb_kwargs kw = { 1, 0, kw_names, &cause, &opts };
mrb_int argc = mrb_get_args(mrb, "|oo:", &exc, &mesg, &kw);

/* keywords other than cause: are a Hash argument, as in CRuby */
if (mrb_hash_p(opts) && !mrb_hash_empty_p(mrb, opts)) {
switch (argc) {
case 0: exc = opts; break;
case 1: mesg = opts; break;
default: mrb_argnum_error(mrb, argc + 1, 0, 2);
}
argc++;
}

mrb->c->ci->mid = 0;
switch (argc) {
case 0:
if (!mrb_undef_p(cause)) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "only cause is given with no arguments");
}
/* Re-raise what a rescue clause is running, which is what `$!` names.
Outside one it is nil, and there being nothing to re-raise is itself
the RuntimeError. `!` is not a word, so MRB_GVSYM() cannot spell the
Expand All @@ -578,6 +599,9 @@ mrb_f_raise(mrb_state *mrb, mrb_value self)
/* fall through */
default:
exc = mrb_make_exception(mrb, exc, mesg);
if (!mrb_undef_p(cause)) {
mrb_exc_raise_with_cause(mrb, exc, cause);
}
mrb_exc_raise(mrb, exc);
break;
}
Expand Down Expand Up @@ -961,7 +985,7 @@ static const mrb_mt_entry kernel_rom_entries[] = {
MRB_MT_ENTRY(mrb_obj_is_kind_of_m, MRB_SYM_Q(kind_of), MRB_ARGS_REQ(1)), /* 15.3.1.3.26 */
MRB_MT_ENTRY(mrb_false, MRB_SYM_Q(nil), MRB_ARGS_NONE()), /* 15.3.1.3.32 */
MRB_MT_ENTRY(mrb_obj_id_m, MRB_SYM(object_id), MRB_ARGS_NONE()), /* 15.3.1.3.33 */
MRB_MT_ENTRY(mrb_f_raise, MRB_SYM(raise), MRB_ARGS_OPT(2) | MRB_MT_PRIVATE), /* 15.3.1.3.40 */
MRB_MT_ENTRY(mrb_f_raise, MRB_SYM(raise), MRB_ARGS_OPT(2) | MRB_ARGS_KEY(1,1) | MRB_MT_PRIVATE), /* 15.3.1.3.40 */
MRB_MT_ENTRY(mrb_obj_remove_instance_variable, MRB_SYM(remove_instance_variable), MRB_ARGS_REQ(1)), /* 15.3.1.3.41 */
MRB_MT_ENTRY(obj_respond_to, MRB_SYM_Q(respond_to), MRB_ARGS_ARG(1,1)), /* 15.3.1.3.43 */
MRB_MT_ENTRY(mrb_any_to_s, MRB_SYM(to_s), MRB_ARGS_NONE()), /* 15.3.1.3.46 */
Expand All @@ -981,7 +1005,7 @@ static const mrb_mt_entry kernel_rom_entries[] = {
static const mrb_mt_entry kernel_module_function_entries[] = {
MRB_MT_ENTRY(mrb_f_block_given_p_m, MRB_SYM_Q(block_given), MRB_ARGS_NONE()), /* 15.3.1.2.2 */
MRB_MT_ENTRY(mrb_f_block_given_p_m, MRB_SYM_Q(iterator), MRB_ARGS_NONE()), /* 15.3.1.2.5 */
MRB_MT_ENTRY(mrb_f_raise, MRB_SYM(raise), MRB_ARGS_OPT(2)), /* 15.3.1.2.12 */
MRB_MT_ENTRY(mrb_f_raise, MRB_SYM(raise), MRB_ARGS_OPT(2) | MRB_ARGS_KEY(1,1)), /* 15.3.1.2.12 */
#ifndef HAVE_MRUBY_IO_GEM
MRB_MT_ENTRY(mrb_p_m, MRB_SYM(p), MRB_ARGS_ANY()),
MRB_MT_ENTRY(mrb_print_m, MRB_SYM(print), MRB_ARGS_ANY()),
Expand Down
190 changes: 190 additions & 0 deletions test/t/exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -709,3 +709,193 @@ def reraise_now
end
assert_raise(RuntimeError) { raise }
end

assert('Exception#cause') do
assert_nil RuntimeError.new("x").cause
e = begin
raise "b"
rescue => x
x
end
assert_nil e.cause
end

assert('Exception#cause is what $! names when it is raised') do
e = begin
begin
raise "a"
rescue
raise "b"
end
rescue => x
x
end
assert_equal "b", e.message
assert_equal "a", e.cause.message
# an exception raised in C from a rescue clause, too
e = begin
begin
raise "a"
rescue
nil.no_such_method
end
rescue NoMethodError => x
x
end
assert_equal "a", e.cause.message
# from an ensure clause, the exception unwinding through it
e = begin
begin
raise "a"
ensure
raise "b" rescue nil
raise "c"
end
rescue => x
x
end
assert_equal "c", e.message
assert_equal "a", e.cause.message
# none once the rescue clause is left
e = begin
begin
raise "a"
rescue
end
raise "b"
rescue => x
x
end
assert_nil e.cause
end

assert('Exception#cause is kept when the exception is raised again') do
b = begin
begin
raise "a"
rescue
raise "b"
end
rescue => x
x
end
e = begin
begin
raise "z"
rescue
raise b
end
rescue => x
x
end
assert_same b, e
assert_equal "a", e.cause.message
# one raised with no cause takes the one at hand when raised again
n = begin; raise "n"; rescue => x; x; end
e = begin
begin
raise "z"
rescue
raise n
end
rescue => x
x
end
assert_equal "z", e.cause.message
# raising $! itself gives it no cause
e = begin
begin
raise "a"
rescue => y
raise y
end
rescue => x
x
end
assert_nil e.cause
end

assert('Exception#cause does not make a cycle') do
a = RuntimeError.new("a")
b = begin
begin
raise a
rescue
raise "b"
end
rescue => x
x
end
assert_same a, b.cause
e = begin
begin
raise b
rescue
raise a
end
rescue => x
x
end
assert_same a, e
assert_nil a.cause
end

assert('Kernel#raise with cause:') do
c = RuntimeError.new("c")
e = begin; raise "b", cause: c; rescue => x; x; end
assert_same c, e.cause
e = begin; raise ArgumentError, "b", cause: c; rescue => x; x; end
assert_kind_of ArgumentError, e
assert_same c, e.cause
# nil takes no cause from $!, for this raise only
e = begin
begin
raise "a"
rescue
raise "b", cause: nil
end
rescue => x
x
end
assert_nil e.cause
e2 = begin
begin
raise "z"
rescue
raise e
end
rescue => x
x
end
assert_equal "z", e2.cause.message
# an explicit cause replaces the one there was, and nil leaves it
q = begin; raise "q", cause: RuntimeError.new("c1"); rescue => x; x; end
e = begin; raise q, cause: nil; rescue => x; x; end
assert_equal "c1", e.cause.message
e = begin; raise q, cause: RuntimeError.new("c2"); rescue => x; x; end
assert_equal "c2", e.cause.message
# the exception itself as its cause is ignored
s = RuntimeError.new("s")
e = begin; raise s, cause: s; rescue => x; x; end
assert_nil e.cause
end

assert('Kernel#raise refuses a bad cause:') do
assert_raise_with_message(ArgumentError, "only cause is given with no arguments") do
raise cause: RuntimeError.new
end
assert_raise_with_message(TypeError, "exception object expected") do
raise "b", cause: 1
end
a = RuntimeError.new("a")
b = begin; raise "b", cause: a; rescue => x; x; end
assert_raise_with_message(ArgumentError, "circular causes") do
raise a, cause: b
end
end

assert('Kernel#raise takes other keywords as a Hash argument') do
e = begin; raise RuntimeError, foo: 1; rescue => x; x; end
assert_kind_of RuntimeError, e
assert_equal({foo: 1}.to_s, e.message)
end
Loading