Browse files

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

  • Loading branch information...
1 parent 45acfdb commit d692ddc2409c234dea066224aaea6941aba41399 @kares kares committed Aug 2, 2012
Showing with 15 additions and 4 deletions.
  1. +13 −2 lib/rhino/error.rb
  2. +2 −2 spec/rhino/error_spec.rb
View
15 lib/rhino/error.rb
@@ -12,7 +12,14 @@ def inspect
"#<#{self.class.name}: #{message}>"
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
return @cause if defined?(@cause)
@cause = begin
@@ -24,6 +31,7 @@ def cause
end
end
+ # Return the thown (native) JavaScript value.
def value
return @value if defined?(@value)
if cause.respond_to?(:value) # e.g. JavaScriptException.getValue
@@ -35,6 +43,7 @@ def value
end
end
+ # Attempts to unwrap the (native) JavaScript/Java exception.
def unwrap
return @unwrap if defined?(@unwrap)
cause = self.cause
@@ -50,6 +59,7 @@ def unwrap
end
end
+ # The backtrace is constructed using #javascript_backtrace + the Ruby part.
def backtrace
if js_backtrace = javascript_backtrace
js_backtrace.push(*super)
@@ -58,6 +68,7 @@ def backtrace
end
end
+ # Returns the JavaScript back-trace part for this error (the script stack).
def javascript_backtrace(keep_elements = false)
if cause.is_a?(JS::RhinoException)
cause.getScriptStack.map do |element| # ScriptStackElement[]
@@ -72,4 +83,4 @@ def javascript_backtrace(keep_elements = false)
end
-end
+end
View
4 spec/rhino/error_spec.rb
@@ -42,7 +42,7 @@ def cause
e.should be_a(Rhino::JSError)
e.value.should be_a(Rhino::JS::NativeObject)
e.value['foo'].should == 'bar'
- e.value.should == e.message
+ e.message.should == e.value.to_s
else
fail "expected to rescue"
end
@@ -54,7 +54,7 @@ def cause
rescue => e
e.should be_a(Rhino::JSError)
e.value.should == 'mehehehe'
- e.value.should == e.message
+ e.message.should == e.value.to_s
else
fail "expected to rescue"
end

0 comments on commit d692ddc

Please sign in to comment.