Skip to content

Commit

Permalink
Calling find with nil should raise a proper exception
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Aug 23, 2011
1 parent 3a7315a commit 4332ffc
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/mongoid/criteria.rb
Expand Up @@ -298,7 +298,7 @@ def search(*args)
#
# @since 2.0.0
def raise_invalid
raise Errors::InvalidOptions.new(:calling_document_find_with_nil_is_invalid, {})
raise Errors::InvalidFind.new
end

protected
Expand Down
1 change: 1 addition & 0 deletions lib/mongoid/errors.rb
Expand Up @@ -5,6 +5,7 @@
require "mongoid/errors/invalid_collection"
require "mongoid/errors/invalid_database"
require "mongoid/errors/invalid_field"
require "mongoid/errors/invalid_find"
require "mongoid/errors/invalid_options"
require "mongoid/errors/invalid_type"
require "mongoid/errors/mixed_relations"
Expand Down
19 changes: 19 additions & 0 deletions lib/mongoid/errors/invalid_find.rb
@@ -0,0 +1,19 @@
# encoding: utf-8
module Mongoid #:nodoc
module Errors #:nodoc

# Raised when invalid arguments are passed to #find.
class InvalidFind < MongoidError

# Create the new invalid find error.
#
# @example Create the error.
# InvalidFind.new
#
# @since 2.2.0
def initialize
super(translate("calling_document_find_with_nil_is_invalid", {}))
end
end
end
end
9 changes: 9 additions & 0 deletions spec/unit/mongoid/criteria_spec.rb
Expand Up @@ -945,6 +945,15 @@

context "with a single argument" do

context "when the arg is nil" do

it "adds the id selector" do
expect {
criteria.search(nil)
}.to raise_error(Mongoid::Errors::InvalidFind)
end
end

context "when the arg is a string" do

let(:id) do
Expand Down
17 changes: 17 additions & 0 deletions spec/unit/mongoid/errors/invalid_find_spec.rb
@@ -0,0 +1,17 @@
require "spec_helper"

describe Mongoid::Errors::InvalidFind do

describe "#message" do

let(:error) do
described_class.new
end

it "returns the warning find with nil" do
error.message.should include(
"Calling Document#find with nil is invalid"
)
end
end
end

0 comments on commit 4332ffc

Please sign in to comment.