lib/execjs/ruby_rhino_runtime.rb in execjs-2.3.0 vs lib/execjs/ruby_rhino_runtime.rb in execjs-2.4.0
- old
+ new
@@ -8,11 +8,11 @@
@rhino_context = ::Rhino::Context.new
fix_memory_limit! @rhino_context
@rhino_context.eval(source)
rescue Exception => e
- reraise_error(e)
+ raise wrap_error(e)
end
def exec(source, options = {})
source = encode(source)
@@ -26,17 +26,17 @@
if /\S/ =~ source
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)
case value = ::Rhino::to_ruby(value)
when Java::OrgMozillaJavascript::NativeFunction
@@ -56,20 +56,21 @@
else
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("<eval>", "(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
# Disables bytecode compiling which limits you to 64K scripts
def fix_memory_limit!(context)