Sha256: 6802611cc630981c95294f6675458d5ca1b1d2de10030620373bf7211901e570

Contents?: true

Size: 1.17 KB

Versions: 5

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

require "pakyow/support/extension"

module Pakyow
  module Behavior
    # Lets an app to be rescued from errors encountered during boot. Once rescued,
    # an app returns a 500 response with the error that caused it to fail.
    #
    module Rescuing
      extend Support::Extension

      # Error the app was rescued from.
      #
      attr_reader :rescued

      # Returns true if the app has been rescued.
      #
      def rescued?
        instance_variable_defined?(:@rescued) && !!@rescued
      end

      private

      # Enters rescue mode after logging the error.
      #
      def rescue!(error)
        @rescued = error

        performing :rescue do
          Pakyow.logger.error(error)

          message = <<~ERROR
            #{self.class} failed to initialize.

            #{error.message}
            #{error.backtrace.join("\n")}
          ERROR

          # Override call to always return an errored response.
          #
          define_singleton_method :call do |connection|
            connection.status = 500
            connection.body = StringIO.new(message)
            connection.halt
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pakyow-core-1.0.0.rc5 lib/pakyow/behavior/rescuing.rb
pakyow-core-1.0.0.rc4 lib/pakyow/behavior/rescuing.rb
pakyow-core-1.0.0.rc3 lib/pakyow/behavior/rescuing.rb
pakyow-core-1.0.0.rc2 lib/pakyow/behavior/rescuing.rb
pakyow-core-1.0.0.rc1 lib/pakyow/behavior/rescuing.rb