Class: ErrorNormalizer::Error
- Inherits:
-
Object
- Object
- ErrorNormalizer::Error
- Defined in:
- lib/error_normalizer/error.rb
Overview
Struct which makes cosmetic normalization on calling either #to_hash or #to_json.
Translates message with path via i18n if corresponding options is passed (see #initialize).
Provides case equality check (Error.===) to support plain Hash structs.
Class Method Summary collapse
-
.===(other) ⇒ Boolean
Case equality check.
Instance Method Summary collapse
-
#full_message ⇒ String
Translate message with path via i18n.
-
#initialize(error_key, message: nil, type: 'params', i18n_messages: nil, **payload) ⇒ Error
constructor
A new instance of Error.
-
#to_hash ⇒ Hash
Error Hash representation.
-
#to_json ⇒ String
Error JSON string representation.
Constructor Details
#initialize(error_key, message: nil, type: 'params', i18n_messages: nil, **payload) ⇒ Error
Returns a new instance of Error.
31 32 33 34 35 36 37 |
# File 'lib/error_normalizer/error.rb', line 31 def initialize(error_key, message: nil, type: 'params', i18n_messages: nil, **payload) @key = error_key = @type = type = @payload = payload end |
Class Method Details
.===(other) ⇒ Boolean
Case equality check
41 42 43 44 45 46 47 |
# File 'lib/error_normalizer/error.rb', line 41 def self.===(other) return true if other.is_a?(Error) return false unless other.is_a?(Hash) h = other.transform_keys(&:to_s) h.key?('key') && h.key?('message') && h.key?('payload') && h.key?('type') end |
Instance Method Details
#full_message ⇒ String
Translate message with path via i18n. Delegates path translation to SchemaPathTranslator.
52 53 54 55 56 57 58 59 |
# File 'lib/error_normalizer/error.rb', line 52 def return unless && @type == 'params' path = payload[:path] return if path.nil? translate_path(path) end |
#to_hash ⇒ Hash
Returns error Hash representation.
62 63 64 65 66 67 68 69 |
# File 'lib/error_normalizer/error.rb', line 62 def to_hash { key: @key, message: , payload: payload, type: @type } end |
#to_json ⇒ String
Returns error JSON string representation.
72 73 74 |
# File 'lib/error_normalizer/error.rb', line 72 def to_json to_hash.to_json end |