Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Correctly raise TypeError even if nil or false is given #1936

Merged
merged 2 commits into from

2 participants

@ryoqun
Collaborator

No description provided.

spec/ruby/shared/kernel/raise.rb
@@ -16,6 +16,22 @@
lambda { @object.raise }.should raise_error(RuntimeError)
end
+ it "raises a given Exception instance" do
+ error = RuntimeError.new
+ lambda { @object.raise(error) }.should raise_error(error)
+ end
+
+ it "raises RuntimeError if string given" do
+ lambda { @object.raise("a bad thing") }.should raise_error(RuntimeError)
+ end
+
+ it "raises TypeError if a given object isn't Exception or its instance" do
@brixen Owner
brixen added a note

it "raises a TypeError when passed a non-Exception object"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
spec/ruby/shared/kernel/raise.rb
@@ -16,6 +16,22 @@
lambda { @object.raise }.should raise_error(RuntimeError)
end
+ it "raises a given Exception instance" do
+ error = RuntimeError.new
+ lambda { @object.raise(error) }.should raise_error(error)
+ end
+
+ it "raises RuntimeError if string given" do
+ lambda { @object.raise("a bad thing") }.should raise_error(RuntimeError)
+ end
+
+ it "raises TypeError if a given object isn't Exception or its instance" do
+ lambda { @object.raise(Object.new) }.should raise_error(TypeError)
+ lambda { @object.raise(true) }.should raise_error(TypeError)
@brixen Owner
brixen added a note

it "raises a TypeError when passed true"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
spec/ruby/shared/kernel/raise.rb
@@ -16,6 +16,22 @@
lambda { @object.raise }.should raise_error(RuntimeError)
end
+ it "raises a given Exception instance" do
+ error = RuntimeError.new
+ lambda { @object.raise(error) }.should raise_error(error)
+ end
+
+ it "raises RuntimeError if string given" do
+ lambda { @object.raise("a bad thing") }.should raise_error(RuntimeError)
+ end
+
+ it "raises TypeError if a given object isn't Exception or its instance" do
+ lambda { @object.raise(Object.new) }.should raise_error(TypeError)
+ lambda { @object.raise(true) }.should raise_error(TypeError)
+ lambda { @object.raise(false) }.should raise_error(TypeError)
@brixen Owner
brixen added a note

Etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@ryoqun
Collaborator

@brixen Thanks for comments for specs. I've updated them all.

@brixen brixen merged commit b6fe43f into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
11 kernel/bootstrap/thread.rb
@@ -234,7 +234,7 @@ def join_inner(timeout = undefined)
end
private :join_inner
- def raise(exc=$!, msg=nil, trace=nil)
+ def raise(exc=undefined, msg=nil, trace=nil)
Rubinius.lock(self)
unless @alive
@@ -243,11 +243,18 @@ def raise(exc=$!, msg=nil, trace=nil)
end
begin
+ if exc.equal?(undefined)
+ no_argument = true
+ exc = $!
+ end
+
if exc.respond_to? :exception
exc = exc.exception msg
Kernel.raise TypeError, 'exception class/object expected' unless Exception === exc
exc.set_backtrace trace if trace
- elsif exc.kind_of? String or !exc
+ elsif no_argument
+ exc = RuntimeError.exception nil
+ elsif exc.kind_of? String
exc = RuntimeError.exception exc
else
Kernel.raise TypeError, 'exception class/object expected'
View
2  kernel/delta/kernel.rb
@@ -18,7 +18,7 @@ def raise(exc=undefined, msg=undefined, ctx=nil)
exc = exc.exception msg
end
raise ::TypeError, 'exception class/object expected' unless exc.kind_of?(::Exception)
- elsif exc.kind_of? String or !exc
+ elsif exc.kind_of? String
exc = ::RuntimeError.exception exc
else
raise ::TypeError, 'exception class/object expected'
View
25 spec/ruby/shared/kernel/raise.rb
@@ -16,6 +16,31 @@
lambda { @object.raise }.should raise_error(RuntimeError)
end
+ it "raises a given Exception instance" do
+ error = RuntimeError.new
+ lambda { @object.raise(error) }.should raise_error(error)
+ end
+
+ it "raises a RuntimeError if string given" do
+ lambda { @object.raise("a bad thing") }.should raise_error(RuntimeError)
+ end
+
+ it "raises a TypeError when passed a non-Exception object" do
+ lambda { @object.raise(Object.new) }.should raise_error(TypeError)
+ end
+
+ it "raises a TypeError when passed true" do
+ lambda { @object.raise(true) }.should raise_error(TypeError)
+ end
+
+ it "raises a TypeError when passed false" do
+ lambda { @object.raise(false) }.should raise_error(TypeError)
+ end
+
+ it "raises a TypeError when passed nil" do
+ lambda { @object.raise(nil) }.should raise_error(TypeError)
+ end
+
it "re-raises the rescued exception" do
lambda do
begin
Something went wrong with that request. Please try again.