Class: JsonapiErrorsHandler::ErrorMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi_errors_handler/error_mapper.rb

Overview

Maps any of the given error classes into the serializable errors from the predefined collection

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.mapped_errorsObject (readonly)

Returns the value of attribute mapped_errors.



11
12
13
# File 'lib/jsonapi_errors_handler/error_mapper.rb', line 11

def mapped_errors
  @mapped_errors
end

Class Method Details

.descendant_of_predefined?(error) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/jsonapi_errors_handler/error_mapper.rb', line 33

def self.descendant_of_predefined?(error)
  return false if error.is_a?(Class)

  error.class < JsonapiErrorsHandler::Errors::StandardError
end

.map_errors!(errors_hash = {}) ⇒ Object



14
15
16
# File 'lib/jsonapi_errors_handler/error_mapper.rb', line 14

def self.map_errors!(errors_hash = {})
  @mapped_errors.merge!(errors_hash)
end

.mapped_error(error) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/jsonapi_errors_handler/error_mapper.rb', line 22

def self.mapped_error(error)
  return error if descendant_of_predefined?(error)

  error_class = error.is_a?(Class) ? error.to_s : error.class.name
  root_class = error_class.split('::').first
  mapped = mapped_errors[error_class] || mapped_errors[root_class]
  return unless mapped

  Object.const_get(mapped).new
end

.mapped_error?(error_klass) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/jsonapi_errors_handler/error_mapper.rb', line 18

def self.mapped_error?(error_klass)
  mapped_errors.key?(error_klass)
end