Class: GraphQL::Schema::RescueMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/schema/rescue_middleware.rb

Overview

  • 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 ExecutionError
  • If no handler is found, re-raise the error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRescueMiddleware

Returns a new instance of RescueMiddleware


10
11
12
# File 'lib/graphql/schema/rescue_middleware.rb', line 10

def initialize
  @rescue_table = {}
end

Instance Attribute Details

#rescue_tableHash (readonly)

Returns {class => proc} pairs for handling errors

Returns:

  • (Hash)

    {class => proc} pairs for handling errors


9
10
11
# File 'lib/graphql/schema/rescue_middleware.rb', line 9

def rescue_table
  @rescue_table
end

Instance Method Details

#call(*args, next_middleware) ⇒ Object

Implement the requirement for MiddlewareChain


32
33
34
35
36
37
38
# File 'lib/graphql/schema/rescue_middleware.rb', line 32

def call(*args, next_middleware)
  begin
    next_middleware.call
  rescue StandardError => err
    attempt_rescue(err)
  end
end

#remove_handler(error_class) ⇒ Object

Remove the handler for error_class

Parameters:

  • the (Class)

    error class whose handler should be removed


27
28
29
# File 'lib/graphql/schema/rescue_middleware.rb', line 27

def remove_handler(error_class)
  rescue_table.delete(error_class)
end

#rescue_from(error_class) {|err| ... } ⇒ Object

Examples:

Rescue from not-found by telling the user

MySchema.rescue_from(ActiveRecord::RecordNotFound) { "An item could not be found" }

Parameters:

  • a (Class)

    class of error to rescue from

Yields:

  • (err)

    A handler to return a message for this error instance

Yield Parameters:

  • an (Exception)

    error that was rescued

Yield Returns:

  • (String)

    message to put in GraphQL response


21
22
23
# File 'lib/graphql/schema/rescue_middleware.rb', line 21

def rescue_from(error_class, &block)
  rescue_table[error_class] = block
end