mirror of
https://github.com/rubyjs/therubyrhino
synced 2023-03-27 23:21:34 -04:00
better error message handling + keep the throws JS object as error.value
This commit is contained in:
parent
b12d348b6e
commit
0f2aa03377
2 changed files with 42 additions and 12 deletions
|
@ -5,14 +5,8 @@ module Rhino
|
|||
|
||||
def initialize(native)
|
||||
@native = native # NativeException wrapping a Java Throwable
|
||||
end
|
||||
|
||||
def message
|
||||
cause ? cause.details : @native.to_s
|
||||
end
|
||||
|
||||
def to_s
|
||||
super
|
||||
message = value ? value : ( cause ? cause.details : @native )
|
||||
super(message)
|
||||
end
|
||||
|
||||
# most likely a Rhino::JS::JavaScriptException
|
||||
|
@ -20,6 +14,17 @@ module Rhino
|
|||
@native.respond_to?(:cause) ? @native.cause : nil
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
def unwrap
|
||||
return @unwrap if defined?(@unwrap)
|
||||
cause = self.cause
|
||||
|
|
|
@ -6,9 +6,9 @@ describe Rhino::JSError do
|
|||
js_error = Rhino::JSError.new 'an error message'
|
||||
lambda { js_error.to_s && js_error.inspect }.should_not raise_error
|
||||
|
||||
js_error.cause.should be_nil
|
||||
js_error.cause.should be nil
|
||||
js_error.message.should == 'an error message'
|
||||
js_error.javascript_backtrace.should be_nil
|
||||
js_error.javascript_backtrace.should be nil
|
||||
end
|
||||
|
||||
it "might wrap a RhinoException wrapped in a NativeException like error" do
|
||||
|
@ -30,9 +30,34 @@ describe Rhino::JSError do
|
|||
js_error = Rhino::JSError.new native_error_class.new(rhino_e)
|
||||
lambda { js_error.to_s && js_error.inspect }.should_not raise_error
|
||||
|
||||
js_error.cause.should be(rhino_e)
|
||||
js_error.cause.should be rhino_e
|
||||
js_error.message.should == '42'
|
||||
js_error.javascript_backtrace.should_not be_nil
|
||||
js_error.javascript_backtrace.should_not be nil
|
||||
end
|
||||
|
||||
it "keeps the thrown javascript object value" do
|
||||
begin
|
||||
Rhino::Context.eval "throw { foo: 'bar' }"
|
||||
rescue => e
|
||||
e.should be_a(Rhino::JSError)
|
||||
e.value.should be_a(Rhino::JS::NativeObject)
|
||||
e.value['foo'].should == 'bar'
|
||||
e.value.should == e.message
|
||||
else
|
||||
fail "expected to rescue"
|
||||
end
|
||||
end
|
||||
|
||||
it "keeps the thrown javascript string value" do
|
||||
begin
|
||||
Rhino::Context.eval "throw 'mehehehe'"
|
||||
rescue => e
|
||||
e.should be_a(Rhino::JSError)
|
||||
e.value.should == 'mehehehe'
|
||||
e.value.should == e.message
|
||||
else
|
||||
fail "expected to rescue"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in a new issue