Module: Kumi::Core::ErrorReporter

Defined in:
lib/kumi/core/error_reporter.rb

Overview

Centralized error reporting interface for consistent location handling and error message formatting across the entire codebase.

This module provides a unified way to:

  1. Report errors with consistent location information

  2. Format error messages uniformly

  3. Handle missing location data gracefully

  4. Support both immediate raising and error accumulation patterns

Defined Under Namespace

Classes: ErrorEntry

Class Method Summary collapse

Class Method Details

.add_error(errors, message, location: nil, type: :semantic, context: {}) ⇒ Object

Add an error to an accumulator array (for analyzer passes)



102
103
104
105
106
# File 'lib/kumi/core/error_reporter.rb', line 102

def add_error(errors, message, location: nil, type: :semantic, context: {})
  entry = create_error(message, location: location, type: type, context: context)
  errors << entry
  entry
end

.build_enhanced_message(base_message, suggestions, similar_names) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/kumi/core/error_reporter.rb', line 186

def build_enhanced_message(base_message, suggestions, similar_names)
  parts = [base_message]

  parts << "Did you mean: #{similar_names.map { |name| "`#{name}`" }.join(', ')}?" unless similar_names.empty?

  unless suggestions.empty?
    parts << "Suggestions:"
    suggestions.each { |suggestion| parts << "  - #{suggestion}" }
  end

  parts.join("\n")
end

.create_enhanced_error(message, location: nil, suggestions: [], similar_names: [], type: :semantic) ⇒ ErrorEntry

Enhanced error reporting with suggestions and context



153
154
155
156
157
158
159
# File 'lib/kumi/core/error_reporter.rb', line 153

def create_enhanced_error(message, location: nil, suggestions: [], similar_names: [], type: :semantic)
  enhanced_message = build_enhanced_message(message, suggestions, similar_names)
  create_error(enhanced_message, location: location, type: type, context: {
                 suggestions: suggestions,
                 similar_names: similar_names
               })
end

.create_error(message, location: nil, type: :semantic, context: {}, backtrace: nil) ⇒ ErrorEntry

Create a standardized error entry



85
86
87
88
89
90
91
92
93
# File 'lib/kumi/core/error_reporter.rb', line 85

def create_error(message, location: nil, type: :semantic, context: {}, backtrace: nil)
  ErrorEntry.new(
    location: location,
    message: message,
    type: type,
    context: context,
    backtrace: backtrace
  )
end

.format_errors(errors) ⇒ String

Format multiple errors into a single message



125
126
127
# File 'lib/kumi/core/error_reporter.rb', line 125

def format_errors(errors)
  errors.map(&:to_s).join("\n")
end

.group_errors_by_type(errors) ⇒ Hash

Group errors by type for better organization



133
134
135
# File 'lib/kumi/core/error_reporter.rb', line 133

def group_errors_by_type(errors)
  errors.group_by(&:type)
end

.missing_location_errors(errors) ⇒ Array<ErrorEntry>

Check if any errors lack location information



141
142
143
# File 'lib/kumi/core/error_reporter.rb', line 141

def missing_location_errors(errors)
  errors.reject(&:location?)
end

.raise_error(message, location: nil, error_class: Errors::SemanticError, type: :semantic, backtrace: nil, context: {}) ⇒ Object

Immediately raise a localized error (for parser)

Raises:

  • (error_class)


115
116
117
118
119
# File 'lib/kumi/core/error_reporter.rb', line 115

def raise_error(message, location: nil, error_class: Errors::SemanticError, type: :semantic, backtrace: nil, context: {})
  entry = create_error(message, location: location, type: type, context: context, backtrace: backtrace || caller)
  # Pass both the formatted message and the original location to the error constructor
  raise error_class.new(entry.to_s, location)
end

.validate_error_locations(errors, expected_with_location: %i[syntax semantic type])) ⇒ Hash

Validate that location information is present where expected



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/kumi/core/error_reporter.rb', line 166

def validate_error_locations(errors, expected_with_location: i[syntax semantic type])
  report = {
    total_errors: errors.size,
    errors_with_location: errors.count(&:location?),
    errors_without_location: errors.reject(&:location?),
    location_coverage: 0.0
  }

  report[:location_coverage] = (report[:errors_with_location].to_f / report[:total_errors]) * 100 if report[:total_errors].positive?

  # Check specific types that should have locations
  report[:problematic_errors] = errors.select do |error|
    expected_with_location.include?(error.type) && !error.location?
  end

  report
end