Module: Desiru::ErrorHandling

Included in:
Jobs::Base, Module
Defined in:
lib/desiru/errors.rb

Overview

Error handling utilities

Instance Method Summary collapse

Instance Method Details

#safe_execute(default = nil, log_level: :error) ⇒ Object

Log and swallow errors (use sparingly)



126
127
128
129
130
131
132
# File 'lib/desiru/errors.rb', line 126

def safe_execute(default = nil, log_level: :error)
  yield
rescue StandardError => e
  Desiru.logger.send(log_level, "Error in safe_execute: #{e.class} - #{e.message}")
  Desiru.logger.debug e.backtrace.join("\n") if log_level == :error
  default
end

#with_error_context(context = {}) ⇒ Object

Wrap a block with error context



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/desiru/errors.rb', line 96

def with_error_context(context = {})
  yield
rescue StandardError => e
  # Add context to existing Desiru errors
  raise Desiru::Error.new(e.message, context: context, original_error: e) unless e.is_a?(Desiru::Error)

  e.context.merge!(context)
  raise e

  # Wrap other errors with context
end

#with_retry(max_attempts: 3, backoff: :exponential, retriable_errors: [NetworkError, TimeoutError]) ⇒ Object

Retry with exponential backoff



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/desiru/errors.rb', line 109

def with_retry(max_attempts: 3, backoff: :exponential, retriable_errors: [NetworkError, TimeoutError])
  attempt = 0

  begin
    attempt += 1
    yield(attempt)
  rescue *retriable_errors => e
    raise unless attempt < max_attempts

    delay = calculate_backoff(attempt, backoff)
    Desiru.logger.warn "Retrying after #{delay}s (attempt #{attempt}/#{max_attempts}): #{e.message}"
    sleep delay
    retry
  end
end