Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pass exception text into javascript #264

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion ext/mini_racer_extension/mini_racer_extension.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,8 @@ gvl_ruby_callback(void* data) {

if(callback_data.failed) {
rb_iv_set(parent, "@current_exception", result);
args->GetIsolate()->ThrowException(String::NewFromUtf8Literal(args->GetIsolate(), "Ruby exception"));
VALUE message = rb_funcall(result, rb_intern("message"), 0);
args->GetIsolate()->ThrowException(String::NewFromUtf8(args->GetIsolate(), RSTRING_PTR(message), NewStringType::kNormal, RSTRING_LENINT(message)).ToLocalChecked());
Copy link
Contributor

@eregon eregon Sep 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code previously throwed a JS String "Ruby exception", and now throws a JS String with the exception.message.
I think it should throw an actual JS exception (an Error or subclass IIUC https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)

}
else {
HandleScope scope(args->GetIsolate());
Expand Down
8 changes: 8 additions & 0 deletions test/mini_racer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,14 @@ def test_pipe_leak
end
end

def test_capture_ruby_exception_message
context = MiniRacer::Context.new()
error_message = "Actual Error Message"
context.attach("a", proc{|a| raise error_message})

assert_equal error_message, context.eval("try { a(); } catch (e) { e }")
Copy link
Contributor

@eregon eregon Sep 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it rather weird that the return value of this is a Ruby exception converted to a JS exception string, but the return value of this context.eval is just a string and not an exception? I think that's pretty confusing.
It seems like it should at least be wrapped into some MiniRacer exception or so.
EDIT: see #264 (comment)

end

def test_symbol_support
context = MiniRacer::Context.new()
assert_equal :foo, context.eval("Symbol('foo')")
Expand Down