From ed5acddc40e77be0419b39a5647d195c0da53924 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Thu, 5 Mar 2015 11:31:01 -0800 Subject: [PATCH] Fix rhino errors --- lib/execjs/ruby_rhino_runtime.rb | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/execjs/ruby_rhino_runtime.rb b/lib/execjs/ruby_rhino_runtime.rb index 5875bc1..13b7b3b 100644 --- a/lib/execjs/ruby_rhino_runtime.rb +++ b/lib/execjs/ruby_rhino_runtime.rb @@ -10,7 +10,7 @@ def initialize(runtime, source = "") fix_memory_limit! @rhino_context @rhino_context.eval(source) rescue Exception => e - reraise_error(e) + raise wrap_error(e) end def exec(source, options = {}) @@ -28,13 +28,13 @@ def eval(source, options = {}) unbox @rhino_context.eval("(#{source})") end rescue Exception => e - reraise_error(e) + raise wrap_error(e) end def call(properties, *args) unbox @rhino_context.eval(properties).call(*args) rescue Exception => e - reraise_error(e) + raise wrap_error(e) end def unbox(value) @@ -58,17 +58,18 @@ def unbox(value) end end - def reraise_error(e) - case e - when ::Rhino::JSError - if e.message == "syntax error" - raise RuntimeError, e.message - else - raise ProgramError, e.message - end - else - raise e - end + def wrap_error(e) + return e unless e.is_a?(::Rhino::JSError) + + error_class = e.message == "syntax error" ? RuntimeError : ProgramError + + stack = e.backtrace + stack = stack.map { |line| line.sub(" at ", "").sub("", "(execjs)").strip } + stack.unshift("(execjs):1") if e.javascript_backtrace.empty? + + error = error_class.new(e.value.to_s) + error.set_backtrace(stack) + error end private