Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
MongoID.mm_typecast now raises MongoMapper::IllegalID if id is illega…
…l rather than DocumentNotFound. Fixes mongomapper#43.
  • Loading branch information
jnunemaker committed Aug 3, 2009
1 parent a0939b1 commit 7ae274e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
10 changes: 6 additions & 4 deletions lib/mongomapper.rb
Expand Up @@ -29,7 +29,7 @@ def self.mm_typecast(value)
end
rescue => exception
if exception.message == 'illegal ObjectID format'
raise MongoMapper::DocumentNotFound
raise MongoMapper::IllegalID
else
raise exception
end
Expand Down Expand Up @@ -67,9 +67,11 @@ def self.mm_typecast(value)
require dir + 'embedded_document'
require dir + 'document'

module MongoMapper
class DocumentNotFound < StandardError; end
class DocumentNotValid < StandardError
module MongoMapper
DocumentNotFound = Class.new(StandardError)
IllegalID = Class.new(StandardError)

DocumentNotValid = Class.new(StandardError) do
def initialize(document)
@document = document
super("Validation failed: #{@document.errors.full_messages.join(", ")}")
Expand Down
2 changes: 1 addition & 1 deletion test/functional/test_document.rb
Expand Up @@ -202,7 +202,7 @@ def setup
end

should "raise error if id is illegal" do
lambda { @document.find(1) }.should raise_error(MongoMapper::DocumentNotFound)
lambda { @document.find(1) }.should raise_error(MongoMapper::IllegalID)
end
end

Expand Down
4 changes: 2 additions & 2 deletions test/unit/test_mongo_id.rb
Expand Up @@ -20,10 +20,10 @@ def to_s
MongoID.mm_typecast(id.to_s).should == id
end

should "raise DocumentNotFound if invalid id" do
should "raise MongoMapper::IllegalID if invalid id" do
lambda {
MongoID.mm_typecast(1234)
}.should raise_error(MongoMapper::DocumentNotFound)
}.should raise_error(MongoMapper::IllegalID)
end

should "raise exception if message does not match illegal object id" do
Expand Down

0 comments on commit 7ae274e

Please sign in to comment.