Module: Escalate
- Defined in:
- lib/escalate.rb,
lib/escalate/mixin.rb,
lib/escalate/version.rb
Defined Under Namespace
Constant Summary collapse
- LOG_FIRST_INSTANCE_VARIABLE =
:@_escalate_log_first
- DEFAULT_RESCUE_EXCEPTIONS =
[StandardError].freeze
- DEFAULT_PASS_THROUGH_EXCEPTIONS =
[SystemExit, SystemStackError, NoMemoryError, SecurityError, SignalException].freeze
- VERSION =
"0.3.0"
Class Attribute Summary collapse
-
.on_escalate_callbacks ⇒ Object
readonly
Returns the value of attribute on_escalate_callbacks.
Class Method Summary collapse
- .clear_on_escalate_callbacks ⇒ Object
-
.escalate(exception, location_message, logger, context: {}) ⇒ Object
Logs and escalated an exception.
-
.mixin(&logger_block) ⇒ Object
Returns a module to be mixed into a class or module exposing the escalate method to be used for escalating and logging exceptions.
-
.on_escalate(log_first: true, name: nil, &block) ⇒ Object
Registers an escalation callback to be executed when ‘escalate` is invoked.
Class Attribute Details
.on_escalate_callbacks ⇒ Object (readonly)
Returns the value of attribute on_escalate_callbacks.
20 21 22 |
# File 'lib/escalate.rb', line 20 def on_escalate_callbacks @on_escalate_callbacks end |
Class Method Details
.clear_on_escalate_callbacks ⇒ Object
120 121 122 |
# File 'lib/escalate.rb', line 120 def clear_on_escalate_callbacks on_escalate_callbacks.clear end |
.escalate(exception, location_message, logger, context: {}) ⇒ Object
Logs and escalated an exception
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/escalate.rb', line 35 def escalate(exception, , logger, context: {}) ensure_failsafe("Exception rescued while escalating #{exception.inspect}") do if on_escalate_callbacks.none? || on_escalate_callbacks.values.any? { |block| block.instance_variable_get(LOG_FIRST_INSTANCE_VARIABLE) } logger_allows_added_context?(logger) or context_string = " (#{context.inspect})" = <<~EOS [Escalate] #{}#{context_string} #{exception.class.name}: #{exception.} #{exception.backtrace.join("\n")} EOS if context_string logger.error() else logger.error(, **context) end end on_escalate_callbacks.values.each do |block| ensure_failsafe("Exception rescued while escalating #{exception.inspect} to #{block.inspect}") do block.call(exception, , **context) end end end end |
.mixin(&logger_block) ⇒ Object
Returns a module to be mixed into a class or module exposing the escalate method to be used for escalating and logging exceptions.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/escalate.rb', line 67 def mixin(&logger_block) Thread.current[:escalate_logger_block] = logger_block Module.new do def self.included(base) base.extend self base.escalate_logger_block = Thread.current[:escalate_logger_block] || -> { base.try(:logger) } end attr_accessor :escalate_logger_block def escalate(exception, , context: {}) Escalate.escalate(exception, , escalate_logger, context: context) end def rescue_and_escalate(, context: {}, exceptions: DEFAULT_RESCUE_EXCEPTIONS, pass_through_exceptions: DEFAULT_PASS_THROUGH_EXCEPTIONS, &block) yield rescue *Array(pass_through_exceptions) raise rescue *Array(exceptions) => exception escalate(exception, , context: context) end private def escalate_logger escalate_logger_block&.call || default_escalate_logger end def default_escalate_logger @default_escalate_logger ||= Logger.new(STDERR) end end end |
.on_escalate(log_first: true, name: nil, &block) ⇒ Object
Registers an escalation callback to be executed when ‘escalate` is invoked.
115 116 117 118 |
# File 'lib/escalate.rb', line 115 def on_escalate(log_first: true, name: nil, &block) block.instance_variable_set(LOG_FIRST_INSTANCE_VARIABLE, log_first) on_escalate_callbacks[name || block.source_location] = block end |