rubyjs / mini_racer

Minimal embedded v8
MIT License
594 stars 93 forks source link

`MiniRacer::RuntimeError` does not handle multiline error messages correctly #262

Open davidtaylorhq opened 2 years ago

davidtaylorhq commented 2 years ago
begin
  MiniRacer::Context.new.eval('throw new Error("errormessage\nwith\nnewlines")')
rescue => e
  puts e.message
end

This will output

Error: errormessage

The missing lines incorrectly appear as part of e.backtrace:

JavaScript with
JavaScript newlines
JavaScript at <anonymous>:1:7
/Users/david/.rvm/gems/ruby-2.7.3/gems/mini_racer-0.6.3/lib/mini_racer.rb:228:in `eval_unsafe'
/Users/david/.rvm/gems/ruby-2.7.3/gems/mini_racer-0.6.3/lib/mini_racer.rb:228:in `block (2 levels) in eval'
...

The relevant code is:

https://github.com/rubyjs/mini_racer/blob/719abdb1a4881d65831830e6e6fb5ca1bcedbc1e/lib/mini_racer.rb#L44-L54

tisba commented 2 years ago

That's probably a tricky one, good find though, @davidtaylorhq and thx for reporting!

"Parsing" a JavaScript error seems to be a lot more complicated.

There is a special stack trace API (https://v8.dev/docs/stack-trace-api) in V8 that we might be able to hook into. In any case it would be super useful to have more then just the string representation in Ruby-land. Any idea if that would be possible, @SamSaffron? That might also be interesting for https://github.com/rubyjs/mini_racer/issues/129 and could could help making this way more robust.