Class: GraphQL::Execution::Errors

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/execution/errors.rb

Overview

A plugin that wraps query execution with error handling. Supports class-based schemas and the new Interpreter runtime only.

Examples:

Handling ActiveRecord::NotFound


class MySchema < GraphQL::Schema
  use GraphQL::Execution::Errors

  rescue_from(ActiveRecord::NotFound) do |err, obj, args, ctx, field|
    ErrorTracker.log("Not Found: #{err.message}")
    nil
  end
end

Defined Under Namespace

Classes: NullErrorHandler

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Errors

Returns a new instance of Errors.



24
25
26
# File 'lib/graphql/execution/errors.rb', line 24

def initialize(schema)
  @schema = schema
end

Class Method Details

.use(schema) ⇒ Object



20
21
22
# File 'lib/graphql/execution/errors.rb', line 20

def self.use(schema)
  schema.error_handler = self.new(schema)
end

Instance Method Details

#with_error_handling(ctx) ⇒ Object

Call the given block with the schema’s configured error handlers.

If the block returns a lazy value, it’s not wrapped with error handling. That area will have to be wrapped itself.

Parameters:

Returns:

  • (Object)

    Either the result of the given block, or some object to replace the result, in case of error handling.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/graphql/execution/errors.rb', line 40

def with_error_handling(ctx)
  yield
rescue StandardError => err
  rescues = ctx.schema.rescues
  _err_class, handler = rescues.find { |err_class, handler| err.is_a?(err_class) }
  if handler
    runtime_info = ctx.namespace(:interpreter) || {}
    obj = runtime_info[:current_object]
    args = runtime_info[:current_arguments]
    field = runtime_info[:current_field]
    if obj.is_a?(GraphQL::Schema::Object)
      obj = obj.object
    end
    handler.call(err, obj, args, ctx, field)
  else
    raise err
  end
end