Module: Kumi::Core::ErrorReporting

Overview

Mixin module providing error reporting capabilities to classes that need to report localized errors consistently.

Usage:

class MyAnalyzer
  include ErrorReporting

  def analyze(errors)
    report_error(errors, "Something went wrong", location: some_location)
    raise_localized_error("Critical error", location: some_location)
  end
end

Instance Method Summary collapse

Instance Method Details

#inferred_locationSyntax::Location

Get current location from caller stack (fallback method)



89
90
91
92
93
94
# File 'lib/kumi/core/error_reporting.rb', line 89

def inferred_location
  fallback = caller_locations.find(&:absolute_path)
  return nil unless fallback

  Syntax::Location.new(file: fallback.path, line: fallback.lineno, column: 0)
end

#raise_localized_error(message, location: nil, error_class: Errors::SemanticError, type: :semantic, context: {}) ⇒ Object

Immediately raise a localized error (parser pattern)



37
38
39
# File 'lib/kumi/core/error_reporting.rb', line 37

def raise_localized_error(message, location: nil, error_class: Errors::SemanticError, type: :semantic, context: {})
  ErrorReporter.raise_error(message, location: location, error_class: error_class, type: type, context: context)
end

#raise_syntax_error(message, location: nil, context: {}) ⇒ Object

Immediately raise a syntax error



57
58
59
# File 'lib/kumi/core/error_reporting.rb', line 57

def raise_syntax_error(message, location: nil, context: {})
  raise_localized_error(message, location: location, error_class: Kumi::Core::Errors::SyntaxError, type: :syntax, context: context)
end

#raise_type_error(message, location: nil, context: {}) ⇒ Object

Immediately raise a type error



62
63
64
# File 'lib/kumi/core/error_reporting.rb', line 62

def raise_type_error(message, location: nil, context: {})
  raise_localized_error(message, location: location, error_class: Errors::TypeError, type: :type, context: context)
end

#report_enhanced_error(errors, message, location: nil, suggestions: [], similar_names: [], type: :semantic) ⇒ Object

Create an enhanced error with suggestions



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/kumi/core/error_reporting.rb', line 74

def report_enhanced_error(errors, message, location: nil, suggestions: [], similar_names: [], type: :semantic)
  entry = ErrorReporter.create_enhanced_error(
    message,
    location: location,
    suggestions: suggestions,
    similar_names: similar_names,
    type: type
  )
  errors << entry
  entry
end

#report_error(errors, message, location: nil, type: :semantic, context: {}) ⇒ ErrorReporter::ErrorEntry

Report an error to an accumulator (analyzer pattern)



26
27
28
# File 'lib/kumi/core/error_reporting.rb', line 26

def report_error(errors, message, location: nil, type: :semantic, context: {})
  ErrorReporter.add_error(errors, message, location: location, type: type, context: context)
end

#report_semantic_error(errors, message, location: nil, context: {}) ⇒ Object

Report a semantic error to an accumulator



52
53
54
# File 'lib/kumi/core/error_reporting.rb', line 52

def report_semantic_error(errors, message, location: nil, context: {})
  report_error(errors, message, location: location, type: :semantic, context: context)
end

#report_syntax_error(errors, message, location: nil, context: {}) ⇒ Object

Report a syntax error to an accumulator



42
43
44
# File 'lib/kumi/core/error_reporting.rb', line 42

def report_syntax_error(errors, message, location: nil, context: {})
  report_error(errors, message, location: location, type: :syntax, context: context)
end

#report_type_error(errors, message, location: nil, context: {}) ⇒ Object

Report a type error to an accumulator



47
48
49
# File 'lib/kumi/core/error_reporting.rb', line 47

def report_type_error(errors, message, location: nil, context: {})
  report_error(errors, message, location: location, type: :type, context: context)
end