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.
-
#handle_exception(error, source:, context: nil) ⇒ void
Handle an error according to the configured error handling mode (log, report, raise, etc).
-
#log_and_report_error(error, source:, context: nil) ⇒ void
Report an error using the configured handler or MultiErrorReporter.
-
#log_error(error, source:, context: nil) ⇒ void
Log an errors with structured data.
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::LogStruct 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 config.error_handling_modes.standard_errors else # Ensures the case statement is exhaustive T.absurd(source) end end |
#handle_exception(error, source:, context: nil) ⇒ void
This method returns an undefined value.
Handle an error according to the configured error handling mode (log, report, raise, etc)
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/log_struct/concerns/error_handling.rb', line 65 def handle_exception(error, source:, context: nil) 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) when ErrorHandlingMode::Report, ErrorHandlingMode::ReportProduction log_and_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.
Report an error using the configured handler or MultiErrorReporter
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/log_struct/concerns/error_handling.rb', line 51 def log_and_report_error(error, source:, context: nil) log_error(error, source: source, 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 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 44 45 46 47 |
# File 'lib/log_struct/concerns/error_handling.rb', line 39 def log_error(error, source:, context: nil) # Create structured log entry error_log = Log::Error.from_exception( source, error, context || {} ) LogStruct.error(error_log) end |