1
0
Fork 0
mirror of https://github.com/rails/execjs synced 2023-03-27 23:21:20 -04:00

Wrap error with lineno

This commit is contained in:
Joshua Peek 2015-03-09 16:43:17 -07:00
parent 3b15a50eeb
commit cb4c3ceb4b

View file

@ -8,40 +8,48 @@ module ExecJS
@ctx = Duktape::Context.new(complex_object: nil) @ctx = Duktape::Context.new(complex_object: nil)
@ctx.exec_string(encode(source), '(execjs)') @ctx.exec_string(encode(source), '(execjs)')
rescue Exception => e rescue Exception => e
reraise_error(e) raise wrap_error(e)
end end
def exec(source, options = {}) def exec(source, options = {})
return unless /\S/ =~ source return unless /\S/ =~ source
@ctx.eval_string("(function(){#{encode(source)}})()", '(execjs)') @ctx.eval_string("(function(){#{encode(source)}})()", '(execjs)')
rescue Exception => e rescue Exception => e
reraise_error(e) raise wrap_error(e)
end end
def eval(source, options = {}) def eval(source, options = {})
return unless /\S/ =~ source return unless /\S/ =~ source
@ctx.eval_string("(#{encode(source)})", '(execjs)') @ctx.eval_string("(#{encode(source)})", '(execjs)')
rescue Exception => e rescue Exception => e
reraise_error(e) raise wrap_error(e)
end end
def call(identifier, *args) def call(identifier, *args)
@ctx.call_prop(identifier.split("."), *args) @ctx.call_prop(identifier.split("."), *args)
rescue Exception => e rescue Exception => e
reraise_error(e) raise wrap_error(e)
end end
private private
def reraise_error(e) def wrap_error(e)
case e klass = case e
when Duktape::SyntaxError when Duktape::SyntaxError
raise RuntimeError, e.message RuntimeError
when Duktape::Error when Duktape::Error
raise ProgramError, e.message ProgramError
when Duktape::InternalError when Duktape::InternalError
raise RuntimeError, e.message RuntimeError
end
if klass
re = / \(line (\d+)\)$/
lineno = e.message[re, 1] || 1
error = klass.new(e.message.sub(re, ""))
error.set_backtrace(["(execjs):#{lineno}"] + e.backtrace)
error
else else
raise e e
end end
end end
end end