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) ⇒ Object
- #handle_exception(error, source:, context: nil) ⇒ Object
- #log_and_report_error(error, source:, context: nil) ⇒ Object
- #log_error(error, source:, context: nil) ⇒ Object
Instance Method Details
#error_handling_mode_for(source) ⇒ Object
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 |
#handle_exception(error, source:, context: nil) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/log_struct/concerns/error_handling.rb', line 61 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) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# 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) 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) ⇒ Object
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 |