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



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

def initialize(schema)
  @schema = schema
end

Class Method Details

.use(schema) ⇒ Object



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

def self.use(schema)
  schema_class = schema.is_a?(Class) ? schema : schema.target.class
  schema_class.error_handler = self.new(schema_class)
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.



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

def with_error_handling(ctx)
  yield
rescue StandardError => err
  rescues = @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