Class: ErrorNormalizer::MessageParser

Inherits:
Object
  • Object
show all
Defined in:
lib/error_normalizer/message_parser.rb,
lib/error_normalizer/message_parser/english.rb

Overview

Base implementation of the error message parser. Message parsers attempt to extract key, message and payload from the given message. Instances of MessageParser can’t parse errors on its own because it does not define any error matchers but it defines all necessary parse logic since it doesn’t depend on the locale.

You can easily define your own parser by inheriting from MessageParser:

class RussianMessageParser < ErrorNormalizer::MessageParser
  locale :ru

  value_matcher :must_be_equal_to, /(?<err>должен быть равным) (?<val>.+)/u
  list_matcher :must_be_on_of, /\A(?<err>должен быть одним из): (?<val>.+)/u
end

ActiveModel ignored for now because we don’t plan to use its validations. In case message isn’t recognized we set error to be a simple normalized message (no spaces and special characters).

Here are the links to ActiveModel::Errors and Dry::Validation list of error messages:

Direct Known Subclasses

English

Defined Under Namespace

Classes: English

Constant Summary collapse

AlreadyDefinedError =
Class.new(StandardError)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ MessageParser

Returns a new instance of MessageParser.



79
80
81
82
83
84
# File 'lib/error_normalizer/message_parser.rb', line 79

def initialize(message)
  @locale = self.class.locale
  @message = message
  @key = nil
  @payload = {}
end

Class Attribute Details

.list_matchersHash (readonly)

Returns list matchers.

Returns:

  • (Hash)

    list matchers



76
77
78
# File 'lib/error_normalizer/message_parser.rb', line 76

def list_matchers
  @list_matchers
end

.value_matchersHash (readonly)

Returns value matchers.

Returns:

  • (Hash)

    value matchers



73
74
75
# File 'lib/error_normalizer/message_parser.rb', line 73

def value_matchers
  @value_matchers
end

Instance Attribute Details

#localeString (readonly)

Returns parser locale.

Returns:

  • (String)

    parser locale



87
88
89
# File 'lib/error_normalizer/message_parser.rb', line 87

def locale
  @locale
end

Class Method Details

.list_matcher(key, matcher) ⇒ void

This method returns an undefined value.

Define message list matcher with a corresponding error key. List matchers add a “list” or “range” property to the error payload.

Parameters:

  • key (Symbol)

    set the error key for a given matcher

  • matcher (Regexp)

    match and extract error and payload via regexp named groups

Raises:

  • (ArgumentError)


61
62
63
64
65
66
67
68
69
70
# File 'lib/error_normalizer/message_parser.rb', line 61

def list_matcher(key, matcher)
  raise ArgumentError, 'matcher should be a Regexp' unless matcher.is_a?(Regexp)

  key = key.to_s
  @list_matchers ||= {}

  raise AlreadyDefinedError if @list_matchers.key?(key)

  @list_matchers[key] = matcher
end

.locale(i18n_locale = nil) ⇒ Symbol

Get or set parser locale

Returns:

  • (Symbol)


34
35
36
37
38
# File 'lib/error_normalizer/message_parser.rb', line 34

def locale(i18n_locale = nil)
  return @locale if i18n_locale.nil?

  @locale = i18n_locale.intern
end

.value_matcher(key, matcher) ⇒ void

This method returns an undefined value.

Define message value matcher with a corresponding error key. Value matchers add a “value” property to the error payload.

Parameters:

  • key (Symbol)

    set the error key for a given matcher

  • matcher (Regexp)

    match and extract error and payload via regexp named groups

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
54
# File 'lib/error_normalizer/message_parser.rb', line 45

def value_matcher(key, matcher)
  raise ArgumentError, 'matcher should be a Regexp' unless matcher.is_a?(Regexp)

  key = key.to_s
  @value_matchers ||= {}

  raise AlreadyDefinedError if @value_matchers.key?(key)

  @value_matchers[key] = matcher
end

Instance Method Details

#parseArray

Parse error message

Returns:

  • (Array)

    a tuple of parsed [key, message, payload]



91
92
93
94
95
96
97
98
99
100
# File 'lib/error_normalizer/message_parser.rb', line 91

def parse
  parse_value_message
  return to_a if @key

  parse_list_message
  return to_a if @key

  @key = normalize_message(@message)
  to_a
end

#to_aArray

Returns a tuple of parsed [key, message, payload].

Returns:

  • (Array)

    a tuple of parsed [key, message, payload]



103
104
105
# File 'lib/error_normalizer/message_parser.rb', line 103

def to_a
  [@key, @message, @payload]
end