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
-
#error_handling_mode_for(source) ⇒ ErrorHandlingMode
Get the error handling mode for a given source.
-
#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.
-
#handle_exception(error, source:, context: nil, log: true) ⇒ void
Handle an error according to the configured error handling mode (log, report, raise, etc).
-
#handle_value_error(error, failure, context: nil) ⇒ void
Handle an error raised while LogStruct read a value it was logging, without the error's message: the message can quote that value, as a NoMethodError quotes its receiver's inspect before Ruby 3.4.
-
#log_and_report_error(error, source:, context: nil) ⇒ void
Log an error and report it using the configured handler or MultiErrorReporter.
-
#log_error(error, source:, context: nil) ⇒ void
Log an errors with structured data.
-
#report_error(error, source:, context: nil) ⇒ void
Report an error using the configured handler or MultiErrorReporter, without logging it.
Instance Method Details
#error_handling_mode_for(source) ⇒ ErrorHandlingMode
Get the error handling mode for a given source
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
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/log_struct/concerns/error_handling.rb', line 80 def filtered_context(context) previous_internal_error = Thread.current[:logstruct_formatting_internal_error] # The same filter may fail on the error's context. Keep that failure from reporting itself. Thread.current[:logstruct_formatting_internal_error] = true return context if context.nil? || context.empty? filtered = Formatter.new.process_values(context) filtered.is_a?(Hash) ? filtered : {} ensure Thread.current[:logstruct_formatting_internal_error] = previous_internal_error 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.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/log_struct/concerns/error_handling.rb', line 105 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 # A broken filter or scrubber can also fail on its own error line. Enqueuing another error # there would repeat forever on SemanticLogger's writer thread. Raise modes still raise. if source == Source::Internal && Thread.current[:logstruct_formatting_internal_error] && mode != ErrorHandlingMode::Raise return 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 |
#handle_value_error(error, failure, context: nil) ⇒ void
This method returns an undefined value.
Handle an error raised while LogStruct read a value it was logging, without the error's message: the message can quote that value, as a NoMethodError quotes its receiver's inspect before Ruby 3.4. failure says what failed, and the reported error keeps the original's class and backtrace.
96 97 98 99 100 |
# File 'lib/log_struct/concerns/error_handling.rb', line 96 def handle_value_error(error, failure, context: nil) reported = RuntimeError.new("#{error.class}: #{failure}") reported.set_backtrace(error.backtrace) handle_exception(reported, source: Source::Internal, context: context) 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
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
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.
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., backtrace: e.backtrace, level: Level::Error )) end |