2011-12-09 05:28:27 -05:00
|
|
|
require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
|
|
|
|
|
|
|
describe Rhino::JSError do
|
|
|
|
|
|
|
|
it "works as a StandardError with a message being passed" do
|
|
|
|
js_error = Rhino::JSError.new 'an error message'
|
|
|
|
lambda { js_error.to_s && js_error.inspect }.should_not raise_error
|
|
|
|
|
2012-04-11 08:42:23 -04:00
|
|
|
js_error.cause.should be nil
|
2011-12-09 05:28:27 -05:00
|
|
|
js_error.message.should == 'an error message'
|
2012-04-11 08:42:23 -04:00
|
|
|
js_error.javascript_backtrace.should be nil
|
2011-12-09 05:28:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "might wrap a RhinoException wrapped in a NativeException like error" do
|
|
|
|
# JRuby's NativeException.new(rhino_e) does not work as it is
|
|
|
|
# intended to handle Java exceptions ... no new on the Ruby side
|
|
|
|
native_error_class = Class.new(RuntimeError) do
|
|
|
|
|
|
|
|
def initialize(cause)
|
|
|
|
@cause = cause
|
|
|
|
end
|
|
|
|
|
|
|
|
def cause
|
|
|
|
@cause
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
rhino_e = Rhino::JS::JavaScriptException.new("42".to_java)
|
|
|
|
js_error = Rhino::JSError.new native_error_class.new(rhino_e)
|
|
|
|
lambda { js_error.to_s && js_error.inspect }.should_not raise_error
|
|
|
|
|
2012-04-11 08:42:23 -04:00
|
|
|
js_error.cause.should be rhino_e
|
2011-12-09 05:28:27 -05:00
|
|
|
js_error.message.should == '42'
|
2012-04-11 08:42:23 -04:00
|
|
|
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'
|
2012-08-02 12:10:20 -04:00
|
|
|
e.message.should == e.value.to_s
|
2012-04-11 08:42:23 -04:00
|
|
|
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'
|
2012-08-02 12:10:20 -04:00
|
|
|
e.message.should == e.value.to_s
|
2012-04-11 08:42:23 -04:00
|
|
|
else
|
|
|
|
fail "expected to rescue"
|
|
|
|
end
|
2011-12-09 05:28:27 -05:00
|
|
|
end
|
2012-05-18 15:46:27 -04:00
|
|
|
|
|
|
|
it "contains the native error as the cause" do
|
|
|
|
begin
|
|
|
|
Rhino::Context.eval "throw 42"
|
|
|
|
rescue => e
|
|
|
|
e.cause.should_not be nil
|
|
|
|
e.cause.should be_a Java::OrgMozillaJavascript::JavaScriptException
|
|
|
|
e.cause.getValue.should == 42
|
|
|
|
e.cause.lineNumber.should == 1
|
|
|
|
e.cause.sourceName.should == '<eval>'
|
|
|
|
else
|
|
|
|
fail "expected to rescue"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has a correct javascript backtrace" do
|
|
|
|
begin
|
|
|
|
Rhino::Context.eval "throw 42"
|
|
|
|
rescue => e
|
|
|
|
e.javascript_backtrace.should be_a Enumerable
|
|
|
|
e.javascript_backtrace.size.should == 1
|
|
|
|
e.javascript_backtrace[0].should == "at <eval>:1"
|
|
|
|
|
|
|
|
e.javascript_backtrace(true).should be_a Enumerable
|
|
|
|
e.javascript_backtrace(true).size.should == 1
|
|
|
|
element = e.javascript_backtrace(true)[0]
|
|
|
|
element.file_name.should == '<eval>'
|
|
|
|
element.function_name.should be nil
|
|
|
|
element.line_number.should == 1
|
|
|
|
else
|
|
|
|
fail "expected to rescue"
|
|
|
|
end
|
|
|
|
end
|
2012-05-18 16:00:03 -04:00
|
|
|
|
2012-05-29 05:52:33 -04:00
|
|
|
it "contains function name in javascript backtrace" do
|
|
|
|
begin
|
|
|
|
Rhino::Context.eval "function fortyTwo() { throw 42 }\n fortyTwo()"
|
|
|
|
rescue => e
|
|
|
|
e.javascript_backtrace.size.should == 2
|
|
|
|
e.javascript_backtrace[0].should == "at <eval>:1 (fortyTwo)"
|
|
|
|
e.javascript_backtrace[1].should == "at <eval>:2"
|
|
|
|
else
|
|
|
|
fail "expected to rescue"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-18 16:00:03 -04:00
|
|
|
it "backtrace starts with the javascript part" do
|
|
|
|
begin
|
|
|
|
Rhino::Context.eval "throw 42"
|
|
|
|
rescue => e
|
|
|
|
e.backtrace.should be_a Array
|
|
|
|
e.backtrace[0].should == "at <eval>:1"
|
|
|
|
e.backtrace[1].should_not be nil
|
|
|
|
else
|
|
|
|
fail "expected to rescue"
|
|
|
|
end
|
|
|
|
end
|
2012-05-21 04:12:43 -04:00
|
|
|
|
|
|
|
it "inspect shows the javascript value" do
|
|
|
|
begin
|
|
|
|
Rhino::Context.eval "throw '42'"
|
|
|
|
rescue => e
|
|
|
|
e.inspect.should == '#<Rhino::JSError: 42>'
|
|
|
|
e.to_s.should == '42'
|
|
|
|
else
|
|
|
|
fail "expected to rescue"
|
|
|
|
end
|
|
|
|
end
|
2012-05-30 01:42:46 -04:00
|
|
|
|
|
|
|
it "raises correct error from function#apply" do
|
|
|
|
begin
|
|
|
|
context = Rhino::Context.new
|
|
|
|
context.eval "function foo() { throw 'bar' }"
|
|
|
|
context['foo'].apply(nil)
|
|
|
|
rescue => e
|
|
|
|
e.should be_a Rhino::JSError
|
|
|
|
e.value.should == 'bar'
|
|
|
|
else
|
|
|
|
fail "expected to rescue"
|
|
|
|
end
|
|
|
|
end
|
2011-12-09 05:28:27 -05:00
|
|
|
|
|
|
|
end
|