Skip to content

Commit b9098f6

Browse files
authored
Merge pull request rails#43712 from Shopify/as-cache-error-handling
Report Memcached and Redis cache errors to Rails.error
2 parents 51a6f93 + c63da20 commit b9098f6

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

activesupport/lib/active_support.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ def self.eager_load!
9494
cattr_accessor :test_order # :nodoc:
9595
cattr_accessor :test_parallelization_threshold, default: 50 # :nodoc:
9696

97+
singleton_class.attr_accessor :error_reporter # :nodoc:
98+
9799
def self.cache_format_version
98100
Cache.format_version
99101
end

activesupport/lib/active_support/cache/mem_cache_store.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,9 @@ def deserialize_entry(payload, raw: false, **)
298298

299299
def rescue_error_with(fallback)
300300
yield
301-
rescue Dalli::DalliError => e
302-
logger.error("DalliError (#{e}): #{e.message}") if logger
301+
rescue Dalli::DalliError => error
302+
ActiveSupport.error_reporter&.report(error, handled: true, severity: :warning)
303+
logger.error("DalliError (#{error}): #{error.message}") if logger
303304
fallback
304305
end
305306
end

activesupport/lib/active_support/cache/redis_cache_store.rb

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -458,16 +458,11 @@ def serialize_entries(entries, **options)
458458

459459
def failsafe(method, returning: nil)
460460
yield
461-
rescue ::Redis::BaseError => e
462-
handle_exception exception: e, method: method, returning: returning
461+
rescue ::Redis::BaseError => error
462+
ActiveSupport.error_reporter&.report(error, handled: true, severity: :warning)
463+
@error_handler&.call(method: method, exception: error, returning: returning)
463464
returning
464465
end
465-
466-
def handle_exception(exception:, method:, returning:)
467-
if @error_handler
468-
@error_handler.(method: method, exception: exception, returning: returning)
469-
end
470-
end
471466
end
472467
end
473468
end

activesupport/lib/active_support/railtie.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ class Railtie < Rails::Railtie # :nodoc:
112112
end
113113
end
114114

115+
initializer "active_support.set_error_reporter" do |app|
116+
ActiveSupport.error_reporter = app.executor.error_reporter
117+
end
118+
115119
initializer "active_support.set_configs" do |app|
116120
app.config.active_support.each do |k, v|
117121
k = "#{k}="

0 commit comments

Comments
 (0)