Class: CovLoupe::ErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/cov_loupe/errors/error_handler.rb

Overview

Centralized error handling with configurable logging verbosity.

Error modes:

- :off   → suppress all logging
- :log   → log error class, message, and context
- :debug → same as :log plus full backtraces

convert_standard_error maps Ruby's built-in exceptions to the CovLoupe error hierarchy, using the context parameter to choose more specific messages (e.g., Errno::ENOENT becomes ResultsetNotFoundError in :coverage_loading context but FileNotFoundError otherwise).

Constant Summary collapse

VALID_ERROR_MODES =
%i[off log debug].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error_mode: :log, logger: nil) ⇒ ErrorHandler

Returns a new instance of ErrorHandler.



22
23
24
25
26
27
28
29
30
# File 'lib/cov_loupe/errors/error_handler.rb', line 22

def initialize(error_mode: :log, logger: nil)
  unless VALID_ERROR_MODES.include?(error_mode)
    raise ArgumentError,
      "Invalid error_mode: #{error_mode.inspect}. Valid modes: #{VALID_ERROR_MODES.inspect}"
  end

  @error_mode = error_mode
  @logger = logger
end

Instance Attribute Details

#error_modeObject

Returns the value of attribute error_mode.



18
19
20
# File 'lib/cov_loupe/errors/error_handler.rb', line 18

def error_mode
  @error_mode
end

#loggerObject

Returns the value of attribute logger.



18
19
20
# File 'lib/cov_loupe/errors/error_handler.rb', line 18

def logger
  @logger
end

Instance Method Details

#convert_standard_error(error, context: :general) ⇒ Object

Convert standard Ruby errors to user-friendly custom errors.

Parameters:

  • error (Exception)

    the error to convert

  • context (Symbol) (defaults to: :general)

    :general (default) or :coverage_loading for context-specific messages



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cov_loupe/errors/error_handler.rb', line 51

def convert_standard_error(error, context: :general)
  case error
  when Errno::ENOENT
    convert_enoent(error, context)
  when Errno::EACCES
    convert_eacces(error, context)
  when Errno::EISDIR
    filename = extract_filename(error.message)
    NotAFileError.new("Expected file but found directory: #{filename}", error)
  when Errno::EMFILE, Errno::ENOSPC, IOError
    FileError.new(error.message, error)
  when Errno::EROFS
    FilePermissionError.new(error.message, error)
  when JSON::ParserError
    CoverageDataError.new("Invalid coverage data format: #{error.message}", error)
  when EncodingError
    CoverageDataError.new("Invalid encoding in coverage data: #{error.message}", error)
  when RangeError
    CoverageDataError.new("Numeric overflow or range error: #{error.message}", error)
  when TypeError
    CoverageDataError.new("Invalid coverage data structure: #{error.message}", error)
  when ArgumentError
    convert_argument_error(error, context)
  when NoMethodError
    convert_no_method_error(error, context)
  else # including RuntimeError
    UnknownError.new(error.message, error)
  end
end

#handle_error(error, context: nil, reraise: true) ⇒ Object

Handle an error with appropriate logging and re-raising behavior



41
42
43
44
45
46
# File 'lib/cov_loupe/errors/error_handler.rb', line 41

def handle_error(error, context: nil, reraise: true)
  log_error(error, context)
  if reraise
    raise error.is_a?(CovLoupe::Error) ? error : convert_standard_error(error, context: context)
  end
end

#log_errors?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/cov_loupe/errors/error_handler.rb', line 32

def log_errors?
  error_mode != :off
end

#show_stack_traces?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/cov_loupe/errors/error_handler.rb', line 36

def show_stack_traces?
  error_mode == :debug
end