Class: Harmoniser::ErrorHandler
- Inherits:
-
Object
- Object
- Harmoniser::ErrorHandler
- Defined in:
- lib/harmoniser/error_handler.rb
Constant Summary collapse
- TIMEOUT =
25- DEFAULT_ERROR_HANDLER =
->(exception, ctx) do Harmoniser.logger.error("Error handler called: exception = `#{exception.}`, context = `#{ctx}`") end
Class Method Summary collapse
Instance Method Summary collapse
- #handle_error(exception, ctx = {}) ⇒ Object
-
#initialize(handlers = []) ⇒ ErrorHandler
constructor
A new instance of ErrorHandler.
- #on_error(handler = nil, &block) ⇒ Object
Constructor Details
#initialize(handlers = []) ⇒ ErrorHandler
Returns a new instance of ErrorHandler.
14 15 16 |
# File 'lib/harmoniser/error_handler.rb', line 14 def initialize(handlers = []) @handlers = handlers end |
Class Method Details
.default ⇒ Object
9 10 11 |
# File 'lib/harmoniser/error_handler.rb', line 9 def default @default ||= new([DEFAULT_ERROR_HANDLER]) end |
Instance Method Details
#handle_error(exception, ctx = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/harmoniser/error_handler.rb', line 27 def handle_error(exception, ctx = {}) coordinator = Thread::Queue.new Thread.new do handlers .each { |handler| trigger_handler(handler, exception, ctx) } .tap { coordinator.push(true) } end.tap do |thread| thread.report_on_exception = true end !!coordinator.pop(timeout: TIMEOUT) end |
#on_error(handler = nil, &block) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/harmoniser/error_handler.rb', line 18 def on_error(handler = nil, &block) h = handler || block raise ArgumentError, "Please, provide a handler or a block" if h.nil? raise TypeError, "Handler must respond to call" unless h.respond_to?(:call) @handlers << h self end |