Module: EasyTalk::ErrorHelper

Extended by:
T::Sig
Defined in:
lib/easy_talk/errors_helper.rb

Overview

Helper module for generating consistent error messages

Class Method Summary collapse

Class Method Details

.extract_inner_type(type_info) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/easy_talk/errors_helper.rb', line 28

def self.extract_inner_type(type_info)
  # No change needed here
  if type_info.respond_to?(:type) && type_info.type.respond_to?(:raw_type)
    type_info.type.raw_type
  # special boolean handling
  elsif TypeIntrospection.boolean_type?(type_info.try(:type))
    T::Boolean
  elsif type_info.respond_to?(:type_parameter)
    type_info.type_parameter
  elsif type_info.respond_to?(:raw_a) && type_info.respond_to?(:raw_b)
    # Handle SimplePairUnion types
    [type_info.raw_a, type_info.raw_b]
  elsif type_info.respond_to?(:types)
    # Handle complex union types
    type_info.types.map { |t| t.respond_to?(:raw_type) ? t.raw_type : t }
  else
    # Fallback to something sensible
    Object
  end
end

.raise_array_constraint_error(property_name:, constraint_name:, index:, expected:, got:) ⇒ Object

Raises:



15
16
17
18
19
# File 'lib/easy_talk/errors_helper.rb', line 15

def self.raise_array_constraint_error(property_name:, constraint_name:, index:, expected:, got:)
  message = "Error in property '#{property_name}': Constraint '#{constraint_name}' at index #{index} " \
            "expects #{expected}, but received #{got.inspect} (#{got.class})."
  raise ConstraintError, message
end

.raise_constraint_error(property_name:, constraint_name:, expected:, got:) ⇒ Object

Raises:



9
10
11
12
13
# File 'lib/easy_talk/errors_helper.rb', line 9

def self.raise_constraint_error(property_name:, constraint_name:, expected:, got:)
  message = "Error in property '#{property_name}': Constraint '#{constraint_name}' expects #{expected}, " \
            "but received #{got.inspect} (#{got.class})."
  raise ConstraintError, message
end

.raise_unknown_option_error(property_name:, option:, valid_options:) ⇒ Object

Raises:



21
22
23
24
25
26
# File 'lib/easy_talk/errors_helper.rb', line 21

def self.raise_unknown_option_error(property_name:, option:, valid_options:)
  option = option.keys.first if option.is_a?(Hash)
  message = "Unknown option '#{option}' for property '#{property_name}'. " \
            "Valid options are: #{valid_options.join(', ')}."
  raise UnknownOptionError, message
end

.validate_array_element(property_name:, constraint_name:, inner_type:, element:, index:) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/easy_talk/errors_helper.rb', line 74

def self.validate_array_element(property_name:, constraint_name:, inner_type:, element:, index:)
  if inner_type.is_a?(Array)
    validate_union_element(property_name, constraint_name, inner_type, element, index)
  else
    validate_single_type_element(property_name, constraint_name, inner_type, element, index)
  end
end

.validate_constraint_value(property_name:, constraint_name:, value_type:, value:) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/easy_talk/errors_helper.rb', line 117

def self.validate_constraint_value(property_name:, constraint_name:, value_type:, value:)
  return if value.nil?

  if TypeIntrospection.boolean_type?(value_type)
    return if value.is_a?(Array) && value.all? { |v| [true, false].include?(v) }

    unless [true, false].include?(value)
      raise_constraint_error(
        property_name: property_name,
        constraint_name: constraint_name,
        expected: 'Boolean (true or false)',
        got: value
      )
    end
    return
  end

  # Handle simple scalar types (String, Integer, etc.)
  if value_type.is_a?(Class)
    unless value.is_a?(value_type)
      raise_constraint_error(
        property_name: property_name,
        constraint_name: constraint_name,
        expected: value_type,
        got: value
      )
    end
  # Handle array types specifically
  elsif TypeIntrospection.typed_array?(value_type)
    # This is an array type, validate it
    validate_typed_array_values(
      property_name: property_name,
      constraint_name: constraint_name,
      type_info: value_type,
      array_value: value
    )
  # Handle Sorbet type objects
  elsif value_type.class.ancestors.include?(T::Types::Base)
    # Extract the inner type
    inner_type = extract_inner_type(value_type)

    if inner_type.is_a?(Array)
      # For union types, check if the value matches any of the allowed types
      unless inner_type.any? { |t| value.is_a?(t) }
        expected = inner_type.join(' or ')
        raise_constraint_error(
          property_name: property_name,
          constraint_name: constraint_name,
          expected: expected,
          got: value
        )
      end
    elsif !value.is_a?(inner_type)
      raise_constraint_error(
        property_name: property_name,
        constraint_name: constraint_name,
        expected: inner_type,
        got: value
      )
    end
  end
end

.validate_single_type_element(property_name, constraint_name, inner_type, element, index) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/easy_talk/errors_helper.rb', line 94

def self.validate_single_type_element(property_name, constraint_name, inner_type, element, index)
  # Skip if element is a boolean (booleans are valid in many contexts)
  return if [true, false].include?(element)

  if TypeIntrospection.boolean_type?(inner_type)
    raise_array_constraint_error(
      property_name: property_name,
      constraint_name: constraint_name,
      index: index,
      expected: 'Boolean (true or false)',
      got: element
    )
  elsif !element.is_a?(inner_type)
    raise_array_constraint_error(
      property_name: property_name,
      constraint_name: constraint_name,
      index: index,
      expected: inner_type,
      got: element
    )
  end
end

.validate_typed_array_values(property_name:, constraint_name:, type_info:, array_value:) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/easy_talk/errors_helper.rb', line 49

def self.validate_typed_array_values(property_name:, constraint_name:, type_info:, array_value:)
  # Raise error if value is not an array but type expects one
  unless array_value.is_a?(Array)
    inner_type = extract_inner_type(type_info)
    expected_desc = TypeIntrospection.boolean_type?(inner_type) ? 'Boolean (true or false)' : inner_type.to_s
    raise_constraint_error(
      property_name: property_name,
      constraint_name: constraint_name,
      expected: expected_desc,
      got: array_value
    )
  end

  inner_type = extract_inner_type(type_info)
  array_value.each_with_index do |element, index|
    validate_array_element(
      property_name: property_name,
      constraint_name: constraint_name,
      inner_type: inner_type,
      element: element,
      index: index
    )
  end
end

.validate_union_element(property_name, constraint_name, inner_type, element, index) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/easy_talk/errors_helper.rb', line 82

def self.validate_union_element(property_name, constraint_name, inner_type, element, index)
  return if inner_type.any? { |t| element.is_a?(t) }

  raise_array_constraint_error(
    property_name: property_name,
    constraint_name: constraint_name,
    index: index,
    expected: inner_type.join(' or '),
    got: element
  )
end