Exception: Mongoid::Errors::MongoidError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/mongoid/errors/mongoid_error.rb

Overview

Default parent Mongoid error for all custom errors. This handles the base key for the translations and provides the convenience method for translating the messages.

Direct Known Subclasses

AmbiguousRelationship, Callback, DeleteRestriction, DocumentNotFound, EagerLoad, InvalidCollection, InvalidConfigOption, InvalidDatabase, InvalidField, InvalidFind, InvalidIndex, InvalidOptions, InvalidScope, InvalidSetPolymorphicRelation, InvalidStorageOptions, InvalidTime, InverseNotFound, MixedRelations, NestedAttributesMetadataNotFound, NoDefaultSession, NoEnvironment, NoMapReduceOutput, NoParent, NoSessionConfig, NoSessionDatabase, NoSessionHosts, NoSessionsConfig, ReadonlyAttribute, ScopeOverwrite, TooManyNestedAttributeRecords, UnknownAttribute, UnsavedDocument, UnsupportedJavascript, UnsupportedVersion, Validations, VersioningNotOnRoot, MultiParameterAttributes::Errors::AttributeAssignmentError, MultiParameterAttributes::Errors::MultiparameterAssignmentErrors

Constant Summary

BASE_KEY =
"mongoid.errors.messages"

Instance Method Summary (collapse)

Instance Method Details

- (String) compose_message(key, attributes)

Compose the message.

Examples:

Create the message

error.compose_message

Returns:

  • (String)

    The composed message.

Since:

  • 3.0.0



20
21
22
23
24
# File 'lib/mongoid/errors/mongoid_error.rb', line 20

def compose_message(key, attributes)
  "\nProblem:\n  #{problem(key, attributes)}"+
  "\nSummary:\n  #{summary(key, attributes)}"+
  "\nResolution:\n  #{resolution(key, attributes)}"
end

- (String) problem(key, attributes)

Create the problem.

Examples:

Create the problem.

error.problem

Returns:

  • (String)

    The problem.

Since:

  • 3.0.0



48
49
50
# File 'lib/mongoid/errors/mongoid_error.rb', line 48

def problem(key, attributes)
  translate("#{key}.message", attributes)
end

- (String) resolution(key, attributes)

Create the resolution.

Examples:

Create the resolution.

error.resolution

Returns:

  • (String)

    The resolution.

Since:

  • 3.0.0



72
73
74
# File 'lib/mongoid/errors/mongoid_error.rb', line 72

def resolution(key, attributes)
  translate("#{key}.resolution", attributes)
end

- (String) summary(key, attributes)

Create the summary.

Examples:

Create the summary.

error.summary

Returns:

  • (String)

    The summary.

Since:

  • 3.0.0



60
61
62
# File 'lib/mongoid/errors/mongoid_error.rb', line 60

def summary(key, attributes)
  translate("#{key}.summary", attributes)
end

- (String) translate(key, options)

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

Examples:

Translate the message.

error.translate("errors", :key => value)

Parameters:

  • key (String)

    The key of the error in the locales.

  • options (Hash)

    The objects to pass to create the message.

Returns:

  • (String)

    A localized error message string.



36
37
38
# File 'lib/mongoid/errors/mongoid_error.rb', line 36

def translate(key, options)
  ::I18n.translate("#{BASE_KEY}.#{key}", { locale: :en }.merge(options))
end