Module: LogStruct::Concerns::ErrorHandling::ClassMethods

Extended by:
T::Helpers, T::Sig
Included in:
LogStruct
Defined in:
lib/log_struct/concerns/error_handling.rb

Instance Method Summary collapse

Instance Method Details

#error_handling_mode_for(source) ⇒ ErrorHandlingMode

Get the error handling mode for a given source

Parameters:

Returns:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/log_struct/concerns/error_handling.rb', line 17

def error_handling_mode_for(source)
  config = LogStruct.config

  # Use a case statement for type-safety
  case source
  when Source::TypeChecking
    config.error_handling_modes.type_checking_errors
  when Source::Internal
    config.error_handling_modes.logstruct_errors
  when Source::Security
    config.error_handling_modes.security_errors
  when Source::Rails, Source::App, Source::Job, Source::Storage, Source::Mailer,
       Source::Shrine, Source::CarrierWave, Source::Sidekiq, Source::Dotenv, Source::Puma
    config.error_handling_modes.standard_errors
  else
    # Ensures the case statement is exhaustive
    T.absurd(source)
  end
end

#filtered_context(context) ⇒ Hash{Symbol => T.untyped}?

The context filtered and scrubbed like a log line, for error reporters: it can hold a request's parameters or a mail's recipients

Parameters:

  • context (Hash{Symbol => T.untyped}, nil)

Returns:

  • (Hash{Symbol => T.untyped}, nil)


80
81
82
83
84
85
# File 'lib/log_struct/concerns/error_handling.rb', line 80

def filtered_context(context)
  return context if context.nil? || context.empty?

  filtered = Formatter.new.process_values(context)
  filtered.is_a?(Hash) ? filtered : {}
end

#handle_exception(error, source:, context: nil, log: true) ⇒ void

This method returns an undefined value.

Handle an error according to the configured error handling mode (log, report, raise, etc). Pass log: false when the caller has already logged the error in its own structured line.

Parameters:

  • error (StandardError)
  • source (Source)
  • context (Hash{Symbol => T.untyped}, nil) (defaults to: nil)
  • log (Boolean) (defaults to: true)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/log_struct/concerns/error_handling.rb', line 90

def handle_exception(error, source:, context: nil, log: true)
  mode = error_handling_mode_for(source)

  # Log / report in production, raise locally (dev/test)
  if mode == ErrorHandlingMode::LogProduction || mode == ErrorHandlingMode::ReportProduction
    raise(error) if !LogStruct.is_production?
  end

  case mode
  when ErrorHandlingMode::Ignore
    # Do nothing

  when ErrorHandlingMode::Raise
    raise(error)

  when ErrorHandlingMode::Log, ErrorHandlingMode::LogProduction
    log_error(error, source: source, context: context) if log

  when ErrorHandlingMode::Report, ErrorHandlingMode::ReportProduction
    log_error(error, source: source, context: context) if log
    report_error(error, source: source, context: context)

  else
    # Ensures the case statement is exhaustive
    T.absurd(mode)
  end
end

#log_and_report_error(error, source:, context: nil) ⇒ void

This method returns an undefined value.

Log an error and report it using the configured handler or MultiErrorReporter

Parameters:

  • error (StandardError)
  • source (Source)
  • context (Hash{Symbol => T.untyped}, nil) (defaults to: nil)


47
48
49
50
# File 'lib/log_struct/concerns/error_handling.rb', line 47

def log_and_report_error(error, source:, context: nil)
  log_error(error, source: source, context: context)
  report_error(error, source: source, context: context)
end

#log_error(error, source:, context: nil) ⇒ void

This method returns an undefined value.

Log an errors with structured data

Parameters:

  • error (StandardError)
  • source (Source)
  • context (Hash{Symbol => T.untyped}, nil) (defaults to: nil)


39
40
41
42
43
# File 'lib/log_struct/concerns/error_handling.rb', line 39

def log_error(error, source:, context: nil)
  # Create structured log entry
  error_log = Log.from_exception(source, error, context || {})
  LogStruct.error(error_log)
end

#report_error(error, source:, context: nil) ⇒ void

This method returns an undefined value.

Report an error using the configured handler or MultiErrorReporter, without logging it. A reporter that raises (a timeout calling its service) doesn't change what happens to the error: its own error is logged, and not reported through the same reporter.

Parameters:

  • error (StandardError)
  • source (Source)
  • context (Hash{Symbol => T.untyped}, nil) (defaults to: nil)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/log_struct/concerns/error_handling.rb', line 56

def report_error(error, source:, context: nil)
  context = filtered_context(context)
  error_handler = LogStruct.config.error_reporting_handler
  if error_handler
    # Use the configured handler
    error_handler.call(error, context, source)
  else
    # Fall back to MultiErrorReporter (detects Sentry, Bugsnag, etc.)
    LogStruct::MultiErrorReporter.report_error(error, context || {})
  end
rescue => e
  LogStruct.error(Log::Error.new(
    source: Source::Internal,
    error_class: e.class,
    message: "The error reporter raised while reporting #{error.class}",
    error_message: e.message,
    backtrace: e.backtrace,
    level: Level::Error
  ))
end