Class: GraphQL::Errors

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

Constant Summary collapse

EmptyConfigurationError =
Class.new(StandardError)
EmptyRescueError =
Class.new(StandardError)
NotRescuableError =
Class.new(StandardError)
VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Errors

Returns a new instance of Errors.



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

def initialize(&block)
  @handler_by_class = {}
  self.instance_eval(&block)
end

Class Method Details

.configure(schema, &block) ⇒ Object



12
13
14
15
16
17
# File 'lib/graphql/errors.rb', line 12

def self.configure(schema, &block)
  raise EmptyConfigurationError unless block

  instance = new(&block)
  schema.instrument(:field, instance)
end

Instance Method Details

#instrument(_type, field) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/graphql/errors.rb', line 24

def instrument(_type, field)
  old_resolve_proc = field.resolve_proc
  new_resolve_proc = ->(object, arguments, context) do
    begin
      old_resolve_proc.call(object, arguments, context)
    rescue => exception
      if handler = find_handler(exception)
        handler.call(exception)
      else
        raise exception
      end
    end
  end

  field.redefine { resolve(new_resolve_proc) }
end

#rescue_from(*classes, &block) ⇒ Object

Raises:



41
42
43
44
45
46
47
48
# File 'lib/graphql/errors.rb', line 41

def rescue_from(*classes, &block)
  raise EmptyRescueError unless block

  classes.each do |klass|
    raise NotRescuableError.new(klass.inspect) unless klass.is_a?(Class)
    @handler_by_class[klass] ||= block
  end
end