print exceptions like MRI

This commit is contained in:
Josh Holtrop 2014-07-12 11:12:21 -04:00
parent 2d7d3d14ab
commit a3326b099b

View File

@ -6,11 +6,40 @@ using namespace std;
RUBY_GLOBAL_SETUP
static const char * ruby_string(VALUE obj)
{
VALUE s = rb_funcall(obj, rb_intern("to_s"), 0);
return StringValueCStr(s);
}
static void print_exception(VALUE e)
{
VALUE c = rb_funcall(e, rb_intern("class"), 0);
VALUE m = rb_funcall(e, rb_intern("message"), 0);
VALUE bt = rb_funcall(e, rb_intern("backtrace"), 0);
if ((TYPE(bt) == T_ARRAY) && (RARRAY_LEN(bt) > 0))
{
fprintf(stderr,
"%s: %s (%s)\n",
ruby_string(rb_ary_entry(bt, 0)),
ruby_string(m),
ruby_string(c));
for (int i = 1; i < RARRAY_LEN(bt); i++)
{
fprintf(stderr,
" from %s\n",
ruby_string(rb_ary_entry(bt, i)));
}
}
else
{
fprintf(stderr, "%s (%s)\n", ruby_string(m), ruby_string(c));
}
}
static void handle_bootstrap_error()
{
VALUE e = rb_errinfo();
VALUE s = rb_funcall(e, rb_intern("message"), 0);
fprintf(stderr, "%s\n", StringValueCStr(s));
print_exception(rb_errinfo());
rb_set_errinfo(Qnil);
}