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.4.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
40
41
# File 'lib/graphql/errors.rb', line 24

def instrument(_type, field)
  old_resolve_proc = field.resolve_proc

  new_resolve_proc = lambda do |obj, args, ctx|
    wrap_proc(obj, args, ctx, old_resolve_proc)
  end

  old_lazy_resolve_proc = field.lazy_resolve_proc

  new_lazy_resolve_proc = lambda do |obj, args, ctx|
    wrap_proc(obj, args, ctx, old_lazy_resolve_proc)
  end

  field.redefine do
    resolve(new_resolve_proc)
    lazy_resolve(new_lazy_resolve_proc)
  end
end

#rescue_from(*classes, &block) ⇒ Object

Raises:



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/graphql/errors.rb', line 43

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

  classes.each do |klass|
    if klass.is_a?(Module) && klass.respond_to?(:===)
      @handler_by_class[klass] ||= block
    else
      raise NotRescuableError.new(klass.inspect)
    end
  end
end