Skip to content

Commit b3ab990

Browse files
committed
Handle exceptions in CRubyWASI consistently with Opal
1 parent eca37b2 commit b3ab990

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

app/ruby_engine.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ def run(source)
99
raise NotImplementedError
1010
end
1111

12+
def exception_to_string(err)
13+
# Beautify the backtrace a little bit
14+
backtrace = err.backtrace
15+
backtrace = backtrace.select { |i| i.include? '<anonymous>' }
16+
backtrace = backtrace.map { |i| i.gsub(/.*(<anonymous>)/, '\1') }
17+
backtrace = ["(file)"] if backtrace.empty?
18+
err.set_backtrace(backtrace)
19+
err.full_message
20+
end
21+
1222
def run_with_writer(source, writer, &block)
1323
@writer = writer
1424
@dots = 0

app/ruby_engine/cruby_wasi.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,12 @@ def run(source)
106106

107107
yield `vm.eval(source).toString()`
108108
rescue JS::Error => err
109-
@writer.log_error(err)
109+
raise err
110+
end
111+
112+
def exception_to_string(err)
113+
# "...: undefined method `reverse' for 40:Integer (NoMethodError)\n (Exception)\n"
114+
super(err).sub(/\s+\(Exception\)\s*\z/, '')
110115
end
111116
end
112117
end

app/try_ruby.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -431,13 +431,7 @@ def count_lines
431431

432432
def log_error(err)
433433
unless err.is_a? String
434-
# Beautify the backtrace a little bit
435-
backtrace = err.backtrace
436-
backtrace = backtrace.select { |i| i.include? '<anonymous>' }
437-
backtrace = backtrace.map { |i| i.gsub(/.*(<anonymous>)/, '\1') }
438-
backtrace = ["(file)"] if backtrace.empty?
439-
err.set_backtrace(backtrace)
440-
err = err.full_message
434+
err = @engine.exception_to_string(err)
441435
end
442436

443437
from = count_lines

0 commit comments

Comments
 (0)