# encoding: utf-8
module Mongoid #:nodoc
module Errors #:nodoc
# Raised when querying the database for a document by a specific id which
# does not exist. If multiple ids were passed then it will display all of
# those.
#
# Example:
#
# DocumentNotFound.new(Person, ["1", "2"])
class DocumentNotFound < RuntimeError
def initialize(klass, ids)
@klass, @identifier = klass, ids.is_a?(Array) ? ids.join(", ") : ids
end
def message
"Document not found for class #{@klass} and id(s) #{@identifier}"
end
end
# Raised when invalid options are passed into a constructor or method.
#
# Example:
#
# InvalidOptions.new
class InvalidOptions < RuntimeError; end
# Raised when the database connection has not been set up properly, either
# by attempting to set an object on the db that is not a +Mongo::DB+, or
# not setting anything at all.
#
# Example:
#
# InvalidDatabase.new("Not a DB")
class InvalidDatabase < RuntimeError
def initialize(database)
@database = database
end
def message
"Database should be a Mongo::DB, not #{@database.class.name}"
end
end
# Raised when a persisence method ending in ! fails validation. The message
# will contain the full error messages from the +Document+ in question.
#
# Example:
#
# Validations.new(person.errors)
class Validations < RuntimeError
def initialize(errors)
@errors = errors
end
def message
"Validation failed: #{@errors.full_messages}"
end
end
# This error is raised when trying to access a Mongo::Collection from an
# embedded document.
#
# Example:
#
# InvalidCollection.new(Address)
class InvalidCollection < RuntimeError
def initialize(klass)
@klass = klass
end
def message
"Access to the collection for #{@klass.name} is not allowed " +
"since it is an embedded document, please access a collection from " +
"the root document"
end
end
end
end