class GraphQL::Schema::RescueMiddleware

  • If no handler is found, re-raise the error
    - If a handler is found, use it & return a {GraphQL::ExecutionError}
    - Rescue errors in a middleware chain, then check for a handler
    - Store a table of errors & handlers

def attempt_rescue(err)

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

def call(*args, next_middleware)

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

def initialize

def initialize
  @rescue_table = {}
end

def remove_handler(error_class)

Parameters:
  • the (Class) -- error class whose handler should be removed
def remove_handler(error_class)
  rescue_table.delete(error_class)
end

def rescue_from(error_class, &block)

Other tags:
    Yieldreturn: - message to put in GraphQL response

Other tags:
    Yieldparam: an - error that was rescued

Other tags:
    Yield: - A handler to return a message for this error instance

Parameters:
  • a (Class) -- class of error to rescue from

Other tags:
    Example: Rescue from not-found by telling the user -
def rescue_from(error_class, &block)
  rescue_table[error_class] = block
end