Sha256: 82e3a21c18508635c4521c4580cb352d7292b35f686b7ad40a751238372f3101

Contents?: true

Size: 1.64 KB

Versions: 22

Compression:

Stored size: 1.64 KB

Contents

module GraphQL
  class Schema
    # - Store a table of errors & handlers
    # - Rescue errors in a middleware chain, then check for a handler
    # - If a handler is found, use it & return a {GraphQL::ExecutionError}
    # - If no handler is found, re-raise the error
    class RescueMiddleware
      # @return [Hash] `{class => proc}` pairs for handling errors
      attr_reader :rescue_table
      def initialize
        @rescue_table = {}
      end

      # @example Rescue from not-found by telling the user
      #   MySchema.rescue_from(ActiveRecord::NotFound) { "An item could not be found" }
      #
      # @param [Class] a class of error to rescue from
      # @yield [err] A handler to return a message for this error instance
      # @yieldparam [Exception] an error that was rescued
      # @yieldreturn [String] message to put in GraphQL response
      def rescue_from(error_class, &block)
        rescue_table[error_class] = block
      end

      # Remove the handler for `error_class`
      # @param [Class] the error class whose handler should be removed
      def remove_handler(error_class)
        rescue_table.delete(error_class)
      end

      # Implement the requirement for {GraphQL::Schema::MiddlewareChain}
      def call(*args, next_middleware)
        begin
          next_middleware.call
        rescue StandardError => err
          attempt_rescue(err)
        end
      end

      private

      def attempt_rescue(err)
        handler = rescue_table[err.class]
        if handler
          message = handler.call(err)
          GraphQL::ExecutionError.new(message)
        else
          raise(err)
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
graphql-0.15.2 lib/graphql/schema/rescue_middleware.rb
graphql-0.14.2 lib/graphql/schema/rescue_middleware.rb
graphql-0.15.1 lib/graphql/schema/rescue_middleware.rb
graphql-0.15.0 lib/graphql/schema/rescue_middleware.rb
graphql-0.14.1 lib/graphql/schema/rescue_middleware.rb
graphql-0.14.0 lib/graphql/schema/rescue_middleware.rb
graphql-0.13.0 lib/graphql/schema/rescue_middleware.rb
graphql-0.12.1 lib/graphql/schema/rescue_middleware.rb
graphql-0.12.0 lib/graphql/schema/rescue_middleware.rb
graphql-0.11.1 lib/graphql/schema/rescue_middleware.rb
graphql-0.11.0 lib/graphql/schema/rescue_middleware.rb
graphql-0.10.9 lib/graphql/schema/rescue_middleware.rb
graphql-0.10.8 lib/graphql/schema/rescue_middleware.rb
graphql-0.10.7 lib/graphql/schema/rescue_middleware.rb
graphql-0.10.6 lib/graphql/schema/rescue_middleware.rb
graphql-0.10.5 lib/graphql/schema/rescue_middleware.rb
graphql-0.10.4 lib/graphql/schema/rescue_middleware.rb
graphql-0.10.3 lib/graphql/schema/rescue_middleware.rb
graphql-0.10.2 lib/graphql/schema/rescue_middleware.rb
graphql-0.10.1 lib/graphql/schema/rescue_middleware.rb