Exception: Common::Exceptions::ValidationErrors

Inherits:
BaseError
  • Object
show all
Defined in:
lib/common/exceptions/validation_errors.rb

Overview

Validation Error - an ActiveModel having validation errors, can be sent to this exception

Direct Known Subclasses

ValidationErrorsBadRequest

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseError

#i18n_data, #i18n_field, #i18n_interpolated, #i18n_key, #log_to_sentry?, #message, #sentry_type, #status_code

Constructor Details

#initialize(resource) ⇒ ValidationErrors

Returns a new instance of ValidationErrors.

Raises:

  • (TypeError)


12
13
14
15
# File 'lib/common/exceptions/validation_errors.rb', line 12

def initialize(resource)
  @resource = resource
  raise TypeError, 'the resource provided has no errors' if resource.errors.empty?
end

Instance Attribute Details

#resourceObject (readonly)

Returns the value of attribute resource.



10
11
12
# File 'lib/common/exceptions/validation_errors.rb', line 10

def resource
  @resource
end

Instance Method Details

#error_attributes(key, message, full_message) ⇒ Object (private)



27
28
29
30
31
32
33
# File 'lib/common/exceptions/validation_errors.rb', line 27

def error_attributes(key, message, full_message)
  i18n_data.merge(
    title: full_message,
    detail: "#{key.to_s.underscore.dasherize} - #{message}",
    source: { pointer: "data/attributes/#{key.to_s.underscore.dasherize}" }
  )
end

#errorsObject



17
18
19
20
21
22
23
# File 'lib/common/exceptions/validation_errors.rb', line 17

def errors
  @resource.errors.map do |error|
    full_message = error.full_message
    attributes = error_attributes(error.attribute, error.message, full_message)
    SerializableError.new(attributes)
  end
end