Class: HatiJsonapiError::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/hati_jsonapi_error/registry.rb

Overview

This class is used to register errors and provide a fallback error.

Class Method Summary collapse

Class Method Details

.error_mapObject



30
31
32
# File 'lib/hati_jsonapi_error/registry.rb', line 30

def error_map
  @error_map ||= {}
end

.error_map=(error_map) ⇒ Object

Base.loaded? # => true Registry.error_map =

ActiveRecord::RecordNotFound => :not_found,
ActiveRecord::RecordInvalid  => 422



20
21
22
23
24
25
26
27
28
# File 'lib/hati_jsonapi_error/registry.rb', line 20

def error_map=(error_map)
  error_map.each do |error, mapped_error|
    next if loaded_error?(mapped_error)

    error_map[error] = fetch_error(mapped_error)
  end

  @error_map = error_map
end

.fallbackObject



11
12
13
# File 'lib/hati_jsonapi_error/registry.rb', line 11

def fallback
  @fallback ||= nil
end

.fallback=(err) ⇒ Object



7
8
9
# File 'lib/hati_jsonapi_error/registry.rb', line 7

def fallback=(err)
  @fallback = loaded_error?(err) ? err : fetch_error(err)
end

.lookup_error(error) ⇒ Object



34
35
36
# File 'lib/hati_jsonapi_error/registry.rb', line 34

def lookup_error(error)
  error_map[error.class] || fallback
end