1
0
Fork 0
mirror of https://github.com/rubyjs/therubyrhino synced 2023-03-27 23:21:34 -04:00

error.message should be a String value (1.9.3 compat)

This commit is contained in:
kares 2012-08-02 18:10:20 +02:00
parent 45acfdbbbf
commit d692ddc240
2 changed files with 15 additions and 4 deletions

View file

@ -12,7 +12,14 @@ module Rhino
"#<#{self.class.name}: #{message}>" "#<#{self.class.name}: #{message}>"
end end
# most likely a Rhino::JS::JavaScriptException # 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.
def cause def cause
return @cause if defined?(@cause) return @cause if defined?(@cause)
@cause = begin @cause = begin
@ -24,6 +31,7 @@ module Rhino
end end
end end
# Return the thown (native) JavaScript value.
def value def value
return @value if defined?(@value) return @value if defined?(@value)
if cause.respond_to?(:value) # e.g. JavaScriptException.getValue if cause.respond_to?(:value) # e.g. JavaScriptException.getValue
@ -35,6 +43,7 @@ module Rhino
end end
end end
# Attempts to unwrap the (native) JavaScript/Java exception.
def unwrap def unwrap
return @unwrap if defined?(@unwrap) return @unwrap if defined?(@unwrap)
cause = self.cause cause = self.cause
@ -50,6 +59,7 @@ module Rhino
end end
end end
# The backtrace is constructed using #javascript_backtrace + the Ruby part.
def backtrace def backtrace
if js_backtrace = javascript_backtrace if js_backtrace = javascript_backtrace
js_backtrace.push(*super) js_backtrace.push(*super)
@ -58,6 +68,7 @@ module Rhino
end end
end end
# Returns the JavaScript back-trace part for this error (the script stack).
def javascript_backtrace(keep_elements = false) def javascript_backtrace(keep_elements = false)
if cause.is_a?(JS::RhinoException) if cause.is_a?(JS::RhinoException)
cause.getScriptStack.map do |element| # ScriptStackElement[] cause.getScriptStack.map do |element| # ScriptStackElement[]
@ -72,4 +83,4 @@ module Rhino
end end
end end

View file

@ -42,7 +42,7 @@ describe Rhino::JSError do
e.should be_a(Rhino::JSError) e.should be_a(Rhino::JSError)
e.value.should be_a(Rhino::JS::NativeObject) e.value.should be_a(Rhino::JS::NativeObject)
e.value['foo'].should == 'bar' e.value['foo'].should == 'bar'
e.value.should == e.message e.message.should == e.value.to_s
else else
fail "expected to rescue" fail "expected to rescue"
end end
@ -54,7 +54,7 @@ describe Rhino::JSError do
rescue => e rescue => e
e.should be_a(Rhino::JSError) e.should be_a(Rhino::JSError)
e.value.should == 'mehehehe' e.value.should == 'mehehehe'
e.value.should == e.message e.message.should == e.value.to_s
else else
fail "expected to rescue" fail "expected to rescue"
end end