2011-12-09 05:28:27 -05:00
|
|
|
module Rhino
|
|
|
|
|
|
|
|
class JSError < StandardError
|
|
|
|
|
|
|
|
def initialize(native)
|
|
|
|
@native = native # NativeException wrapping a Java Throwable
|
2012-04-11 08:42:23 -04:00
|
|
|
message = value ? value : ( cause ? cause.details : @native )
|
|
|
|
super(message)
|
2012-01-07 11:01:38 -05:00
|
|
|
end
|
|
|
|
|
2012-05-21 04:12:43 -04:00
|
|
|
def inspect
|
|
|
|
"#<#{self.class.name}: #{message}>"
|
|
|
|
end
|
|
|
|
|
2012-08-02 12:10:20 -04:00
|
|
|
# Returns the error message, in case of a native JavaScript value, will
|
|
|
|
# return that value converted to a String.
|
|
|
|
def message
|
|
|
|
super.to_s # since 1.9.x message is expected to allways be a string
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the (nested) cause of this error if any, most likely a
|
|
|
|
# #Rhino::JS::JavaScriptException instance.
|
2011-12-09 05:28:27 -05:00
|
|
|
def cause
|
2012-05-18 15:46:27 -04:00
|
|
|
return @cause if defined?(@cause)
|
|
|
|
@cause = begin
|
|
|
|
if @native.respond_to?(:cause) && @native.cause
|
|
|
|
@native.cause
|
|
|
|
else
|
|
|
|
@native.is_a?(JS::RhinoException) ? @native : nil
|
|
|
|
end
|
|
|
|
end
|
2011-12-09 05:28:27 -05:00
|
|
|
end
|
|
|
|
|
2012-08-02 12:10:20 -04:00
|
|
|
# Return the thown (native) JavaScript value.
|
2012-04-11 08:42:23 -04:00
|
|
|
def value
|
|
|
|
return @value if defined?(@value)
|
|
|
|
if cause.respond_to?(:value) # e.g. JavaScriptException.getValue
|
|
|
|
@value = cause.value
|
|
|
|
elsif ( unwrap = self.unwrap ) && unwrap.respond_to?(:value)
|
|
|
|
@value = unwrap.value
|
|
|
|
else
|
|
|
|
@value = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-02 12:10:20 -04:00
|
|
|
# Attempts to unwrap the (native) JavaScript/Java exception.
|
2012-01-07 11:01:38 -05:00
|
|
|
def unwrap
|
|
|
|
return @unwrap if defined?(@unwrap)
|
|
|
|
cause = self.cause
|
|
|
|
if cause && cause.is_a?(JS::WrappedException)
|
|
|
|
e = cause.getWrappedException
|
|
|
|
if e && e.is_a?(Java::OrgJrubyExceptions::RaiseException)
|
|
|
|
@unwrap = e.getException
|
|
|
|
else
|
|
|
|
@unwrap = e
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@unwrap = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-02 12:10:20 -04:00
|
|
|
# The backtrace is constructed using #javascript_backtrace + the Ruby part.
|
2012-05-18 16:00:03 -04:00
|
|
|
def backtrace
|
|
|
|
if js_backtrace = javascript_backtrace
|
|
|
|
js_backtrace.push(*super)
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-02 12:10:20 -04:00
|
|
|
# Returns the JavaScript back-trace part for this error (the script stack).
|
2012-05-18 15:46:27 -04:00
|
|
|
def javascript_backtrace(keep_elements = false)
|
|
|
|
if cause.is_a?(JS::RhinoException)
|
|
|
|
cause.getScriptStack.map do |element| # ScriptStackElement[]
|
|
|
|
keep_elements ? element : element.to_s
|
|
|
|
end
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
2011-12-09 05:28:27 -05:00
|
|
|
end
|
|
|
|
|
2012-05-18 15:46:27 -04:00
|
|
|
Rhino::JS::RhinoException.useMozillaStackStyle(false)
|
|
|
|
|
2011-12-09 05:28:27 -05:00
|
|
|
end
|
|
|
|
|
2012-08-02 12:10:20 -04:00
|
|
|
end
|