Skip to content

Commit 00b72d2

Browse files
committed
Fix errors when a non-null field fails auth check
1 parent b064389 commit 00b72d2

7 files changed

Lines changed: 18 additions & 13 deletions

File tree

lib/graphql.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ def self.eager_load!
2121
class Error < StandardError
2222
end
2323

24+
class RuntimeError < Error
25+
end
26+
2427
# This error is raised when GraphQL-Ruby encounters a situation
2528
# that it *thought* would never happen. Please report this bug!
2629
class InvariantError < Error

lib/graphql/execution/batching/field_compatibility.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,8 @@ def resolve_batch(frs, objects, context, kwargs)
110110
new_return_value
111111
end
112112
end
113-
rescue GraphQL::Error => err
114-
if err.respond_to?(:path=) # TODO sort out error hierarchy so I can match these without a respond_to check
115-
err
116-
else
117-
raise
118-
end
113+
rescue RuntimeError => err
114+
err
119115
rescue StandardError => stderr
120116
begin
121117
context.query.handle_or_reraise(stderr)

lib/graphql/execution/batching/prepare_object_step.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,17 @@ def create_result
8181
if new_obj
8282
@authorized_value = true
8383
@object = new_obj
84+
elsif @is_non_null
85+
@graphql_result[@key] = @field_resolve_step.add_non_null_error(@is_from_array)
8486
else
85-
@graphql_result[@key] = @authorization_error
87+
@graphql_result[@key] = @field_resolve_step.add_graphql_error(@authorization_error)
8688
end
8789
rescue GraphQL::Error => err
88-
@graphql_result[@key] = @field_resolve_step.add_graphql_error(err)
90+
if @is_non_null
91+
@graphql_result[@key] = @field_resolve_step.add_non_null_error(@is_from_array)
92+
else
93+
@graphql_result[@key] = @field_resolve_step.add_graphql_error(err)
94+
end
8995
end
9096
end
9197

lib/graphql/execution_error.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module GraphQL
33
# If a field's resolve function returns a {ExecutionError},
44
# the error will be inserted into the response's `"errors"` key
55
# and the field will resolve to `nil`.
6-
class ExecutionError < GraphQL::Error
6+
class ExecutionError < GraphQL::RuntimeError
77
# @return [GraphQL::Language::Nodes::Field] the field where the error occurred
88
def ast_node
99
ast_nodes.first

lib/graphql/invalid_null_error.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
module GraphQL
33
# Raised automatically when a field's resolve function returns `nil`
44
# for a non-null field.
5-
class InvalidNullError < GraphQL::Error
5+
class InvalidNullError < GraphQL::RuntimeError
66
# @return [GraphQL::BaseType] The owner of {#field}
77
attr_reader :parent_type
88

lib/graphql/query/context.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ def skip
119119
# @param error [GraphQL::ExecutionError] an execution error
120120
# @return [void]
121121
def add_error(error)
122-
if !error.is_a?(ExecutionError)
123-
raise TypeError, "expected error to be a ExecutionError, but was #{error.class}"
122+
if !error.is_a?(GraphQL::RuntimeError)
123+
raise TypeError, "expected error to be a GraphQL::RuntimeError, but was #{error.class}"
124124
end
125125
errors << error
126126
nil

lib/graphql/unauthorized_error.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module GraphQL
44
# It's passed to {Schema.unauthorized_object}.
55
#
66
# Alternatively, custom code in `authorized?` may raise this error. It will be routed the same way.
7-
class UnauthorizedError < GraphQL::Error
7+
class UnauthorizedError < GraphQL::RuntimeError
88
# @return [Object] the application object that failed the authorization check
99
attr_reader :object
1010

0 commit comments

Comments
 (0)