Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

Commit

Permalink
Fix rhino errors
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Mar 5, 2015
1 parent bb8810f commit ed5acdd
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions lib/execjs/ruby_rhino_runtime.rb
Expand Up @@ -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 = {})
Expand All @@ -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)
Expand All @@ -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("<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
Expand Down

0 comments on commit ed5acdd

Please sign in to comment.