123456789_123456789_123456789_123456789_123456789_

Exception: Mongoid::Errors::DocumentNotFound

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, MongoidError, StandardError
Instance Chain:
self, MongoidError, StandardError
Inherits: Mongoid::Errors::MongoidError
Defined in: lib/mongoid/errors/document_not_found.rb

Overview

Raised when querying the database for a document by a specific id or by set of attributes which does not exist. If multiple ids were passed then it will display all of those.

Constant Summary

MongoidError - Inherited

BASE_KEY

Class Method Summary

Instance Attribute Summary

MongoidError - Inherited

Instance Method Summary

MongoidError - Inherited

#compose_message

Compose the message.

#translate

Given the key of the specific error and the options hash, translate the message.

#translate_problem

Create the problem.

#translate_resolution

Create the resolution.

#translate_summary

Create the summary.

Constructor Details

.new(klass, params, unmatched = nil) ⇒ DocumentNotFound

Create the new error.

Examples:

Create the error.

DocumentNotFound.new(Person, ["1", "2"])

Create the error with attributes instead of ids

DocumentNotFound.new(Person, :ssn => "1234", :name => "Helen")

Parameters:

  • klass (Class)

    The model class.

  • params (Hash | Array | Object)

    The attributes or ids.

  • unmatched (Array | Hash) (defaults to: nil)

    The unmatched ids, if appropriate. If there is a shard key this will be a hash.

[ GitHub ]

  
# File 'lib/mongoid/errors/document_not_found.rb', line 23

def initialize(klass, params, unmatched = nil)
  unmatched = Array(params) if !unmatched && !params.is_a?(Hash) && params

  @klass, @params = klass, params
  super(
    compose_message(
      message_key(params, unmatched),
      {
        klass: klass.name,
        searched: searched(params),
        attributes: params,
        total: total(params),
        missing: missing(unmatched),
        shard_key: shard_key(unmatched)
      }
    )
  )
end

Instance Attribute Details

#klass (readonly)

[ GitHub ]

  
# File 'lib/mongoid/errors/document_not_found.rb', line 9

attr_reader :klass, :params

#params (readonly)

[ GitHub ]

  
# File 'lib/mongoid/errors/document_not_found.rb', line 9

attr_reader :klass, :params

Instance Method Details

#message_key(params, unmatched) ⇒ String (private)

Create the problem.

Examples:

Create the problem.

error.problem

Returns:

[ GitHub ]

  
# File 'lib/mongoid/errors/document_not_found.rb', line 96

def message_key(params, unmatched)
  if !params && !unmatched
    'no_documents_found'
  elsif params.is_a?(Hash)
    'document_with_attributes_not_found'
  elsif unmatched.is_a?(Hash) && unmatched.size >= 2
    'document_with_shard_key_not_found'
  else
    'document_not_found'
  end
end

#missing(unmatched) ⇒ String (private)

Get the string to display the document params that were unmatched.

Examples:

Get the missing string.

error.missing(1)

Parameters:

  • unmatched (Object | Array)

    The ids that did not match.

Returns:

  • (String)

    The missing string.

[ GitHub ]

  
# File 'lib/mongoid/errors/document_not_found.rb', line 52

def missing(unmatched)
  if unmatched.is_a?(::Array)
    unmatched.join(', ')
  elsif unmatched.is_a?(::Hash)
    unmatched[:_id] || unmatched['_id']
  else
    unmatched
  end
end

#searched(params) ⇒ String (private)

Get the string to display the document params that were searched for.

Examples:

Get the searched string.

error.searched(1)

Parameters:

  • params (Object | Array)

    The ids that were searched for.

Returns:

  • (String)

    The searched string.

[ GitHub ]

  
# File 'lib/mongoid/errors/document_not_found.rb', line 70

def searched(params)
  if params.is_a?(::Array)
    params.take(3).join(', ') + ' ...'
  else
    params
  end
end

#shard_key(unmatched) ⇒ String (private)

Get the shard key from the unmatched hash.

Returns:

  • (String)

    the shard key and value.

[ GitHub ]

  
# File 'lib/mongoid/errors/document_not_found.rb', line 111

def shard_key(unmatched)
  return unless unmatched.is_a?(Hash)

  h = unmatched.dup
  h.delete('_id')
  h.delete(:_id)
  h.map { |k, v| "#{k}: #{v}" }.join(', ')
end

#total(params) ⇒ Integer (private)

Get the total number of expected documents.

Examples:

Get the total.

error.total([ 1, 2, 3 ])

Parameters:

Returns:

[ GitHub ]

  
# File 'lib/mongoid/errors/document_not_found.rb', line 86

def total(params)
  params.is_a?(::Array) ? params.count : 1
end