Sha256: a8e8e07d2f7a3ec2b6dab237e3fcba44859fd82778b558a77a97c6d74a816a85

Contents?: true

Size: 1.51 KB

Versions: 38

Compression:

Stored size: 1.51 KB

Contents

module ActionSubscriber
  module Middleware
    class ErrorHandler
      include ::ActionSubscriber::Logging

      def initialize(app)
        @app = app
      end

      def call(env)
        @app.call(env)
      rescue Exception => error # make sure we capture any exception from the top of the hierarchy
        logger.error { "FAILED #{env.message_id}" }

        # There is more to this rescue than meets the eye. MarchHare's java library will rescue errors
        # and attempt to close the channel with its default exception handler. To avoid this, we will
        # stop errors right here. If you want to handle errors, you must do it in the error handler and
        # it should not re-raise. As a bonus, not killing these threads is better for your runtime :).
        begin
          ::ActionSubscriber.configuration.error_handler.call(error, env.to_h)
        rescue Exception => inner_error
          logger.error { "ActionSubscriber error handler raised error, but should never raise. Error: #{inner_error}" }
        end
      ensure
        # This second rescue is a little extreme, but we need to be very cautious here to avoid errors
        # being sent back to bunny or march_hare land.
        begin
          # Make sure we attempt to `nack` a message that did not get processed if something fails
          env.safe_nack
        rescue Exception => inner_error
          logger.error { "ActionSubscriber error handler raised error while nack-ing message. Error: #{inner_error}" }
        end
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
action_subscriber-5.3.3-java lib/action_subscriber/middleware/error_handler.rb
action_subscriber-5.3.3 lib/action_subscriber/middleware/error_handler.rb
action_subscriber-5.3.2-java lib/action_subscriber/middleware/error_handler.rb
action_subscriber-5.3.2 lib/action_subscriber/middleware/error_handler.rb
action_subscriber-5.3.1-java lib/action_subscriber/middleware/error_handler.rb
action_subscriber-5.3.1 lib/action_subscriber/middleware/error_handler.rb
action_subscriber-5.3.1.pre-java lib/action_subscriber/middleware/error_handler.rb
action_subscriber-5.3.1.pre lib/action_subscriber/middleware/error_handler.rb
action_subscriber-5.3.0-java lib/action_subscriber/middleware/error_handler.rb
action_subscriber-5.3.0 lib/action_subscriber/middleware/error_handler.rb
action_subscriber-5.2.4-java lib/action_subscriber/middleware/error_handler.rb
action_subscriber-5.2.4 lib/action_subscriber/middleware/error_handler.rb
action_subscriber-5.2.3-java lib/action_subscriber/middleware/error_handler.rb
action_subscriber-5.2.3 lib/action_subscriber/middleware/error_handler.rb
action_subscriber-5.2.2-java lib/action_subscriber/middleware/error_handler.rb
action_subscriber-5.2.2 lib/action_subscriber/middleware/error_handler.rb
action_subscriber-5.2.1-java lib/action_subscriber/middleware/error_handler.rb
action_subscriber-5.2.1 lib/action_subscriber/middleware/error_handler.rb
action_subscriber-5.2.0-java lib/action_subscriber/middleware/error_handler.rb
action_subscriber-5.2.0 lib/action_subscriber/middleware/error_handler.rb