Skip to content

Commit 554f5f6

Browse files
committed
Move error handling inline instead of using a block
1 parent 8255dda commit 554f5f6

File tree

7 files changed

+44
-46
lines changed

7 files changed

+44
-46
lines changed

lib/graphql/execution/interpreter/runtime.rb

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -498,13 +498,17 @@ def evaluate_selection_with_args(arguments, field_defn, next_path, ast_node, fie
498498
field_result = call_method_on_directives(:resolve, object, directives) do
499499
# Actually call the field resolver and capture the result
500500
app_result = begin
501-
query.with_error_handling do
502-
query.trace("execute_field", {owner: owner_type, field: field_defn, path: next_path, ast_node: ast_node, query: query, object: object, arguments: kwarg_arguments}) do
503-
field_defn.resolve(object, kwarg_arguments, context)
504-
end
501+
query.trace("execute_field", {owner: owner_type, field: field_defn, path: next_path, ast_node: ast_node, query: query, object: object, arguments: kwarg_arguments}) do
502+
field_defn.resolve(object, kwarg_arguments, context)
505503
end
506504
rescue GraphQL::ExecutionError => err
507505
err
506+
rescue StandardError => err
507+
begin
508+
query.handle_or_reraise(err)
509+
rescue GraphQL::ExecutionError => ex_err
510+
ex_err
511+
end
508512
end
509513
after_lazy(app_result, owner: owner_type, field: field_defn, path: next_path, ast_node: ast_node, owner_object: object, arguments: resolved_arguments, result_name: result_name, result: selection_result) do |inner_result|
510514
continue_value = continue_value(next_path, inner_result, owner_type, field_defn, return_type.non_null?, ast_node, result_name, selection_result)
@@ -871,21 +875,21 @@ def after_lazy(lazy_obj, owner:, field:, path:, owner_object:, arguments:, ast_n
871875
# Wrap the execution of _this_ method with tracing,
872876
# but don't wrap the continuation below
873877
inner_obj = begin
874-
query.with_error_handling do
875-
begin
876-
if trace
877-
query.trace("execute_field_lazy", {owner: owner, field: field, path: path, query: query, object: owner_object, arguments: arguments, ast_node: ast_node}) do
878-
schema.sync_lazy(lazy_obj)
879-
end
880-
else
881-
schema.sync_lazy(lazy_obj)
882-
end
883-
rescue GraphQL::ExecutionError, GraphQL::UnauthorizedError => err
884-
err
878+
if trace
879+
query.trace("execute_field_lazy", {owner: owner, field: field, path: path, query: query, object: owner_object, arguments: arguments, ast_node: ast_node}) do
880+
schema.sync_lazy(lazy_obj)
885881
end
882+
else
883+
schema.sync_lazy(lazy_obj)
886884
end
887-
rescue GraphQL::ExecutionError => ex_err
885+
rescue GraphQL::ExecutionError, GraphQL::UnauthorizedError => ex_err
888886
ex_err
887+
rescue StandardError => err
888+
begin
889+
query.handle_or_reraise(err)
890+
rescue GraphQL::ExecutionError => ex_err
891+
ex_err
892+
end
889893
end
890894
yield(inner_obj)
891895
end

lib/graphql/query.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,8 @@ def subscription?
331331
end
332332

333333
# @api private
334-
def with_error_handling
335-
schema.with_error_handling(context) do
336-
yield
337-
end
334+
def handle_or_reraise(err)
335+
schema.handle_or_reraise(context, err)
338336
end
339337

340338
private

lib/graphql/query/null_context.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ def visible_type_membership?(tm, ctx); true; end
1212
end
1313

1414
class NullQuery
15-
def with_error_handling
16-
yield
17-
end
1815
end
1916

2017
class NullSchema < GraphQL::Schema

lib/graphql/schema.rb

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -713,26 +713,19 @@ def error_handlers
713713
}
714714
end
715715

716-
# Call the given block with the schema's configured error handlers.
717-
#
718-
# If the block returns a lazy value, it's not wrapped with error handling. That area will have to be wrapped itself.
719-
#
720-
# @param ctx [GraphQL::Query::Context]
721-
# @return [Object] Either the result of the given block, or some object to replace the result, in case of error handling.
722-
def with_error_handling(ctx)
723-
yield
724-
rescue StandardError => err
716+
# @api private
717+
def handle_or_reraise(context, err)
725718
handler = Execution::Errors.find_handler_for(self, err.class)
726719
if handler
727-
runtime_info = ctx.namespace(:interpreter) || {}
720+
runtime_info = context.namespace(:interpreter) || {}
728721
obj = runtime_info[:current_object]
729722
args = runtime_info[:current_arguments]
730723
args = args && args.keyword_arguments
731724
field = runtime_info[:current_field]
732725
if obj.is_a?(GraphQL::Schema::Object)
733726
obj = obj.object
734727
end
735-
handler[:handler].call(err, obj, args, ctx, field)
728+
handler[:handler].call(err, obj, args, context, field)
736729
else
737730
raise err
738731
end

lib/graphql/schema/argument.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,24 +247,30 @@ def coerce_into_values(parent_object, values, context, argument_values)
247247
end
248248

249249
loaded_value = nil
250-
coerced_value = context.schema.with_error_handling(context) do
250+
coerced_value = begin
251251
type.coerce_input(value, context)
252+
rescue StandardError => err
253+
context.schema.handle_or_reraise(context, err)
252254
end
253255

254256
# If this isn't lazy, then the block returns eagerly and assigns the result here
255257
# If it _is_ lazy, then we write the lazy to the hash, then update it later
256258
argument_values[arg_key] = context.schema.after_lazy(coerced_value) do |resolved_coerced_value|
257259
if loads && !from_resolver?
258-
loaded_value = context.query.with_error_handling do
260+
loaded_value = begin
259261
load_and_authorize_value(owner, coerced_value, context)
262+
rescue StandardError => err
263+
context.schema.handle_or_reraise(context, err)
260264
end
261265
end
262266

263267
maybe_loaded_value = loaded_value || resolved_coerced_value
264268
context.schema.after_lazy(maybe_loaded_value) do |resolved_loaded_value|
265269
owner.validate_directive_argument(self, resolved_loaded_value)
266-
prepared_value = context.schema.with_error_handling(context) do
270+
prepared_value = begin
267271
prepare_value(parent_object, resolved_loaded_value, context: context)
272+
rescue StandardError => err
273+
context.schema.handle_or_reraise(context, err)
268274
end
269275

270276
# TODO code smell to access such a deeply-nested constant in a distant module

lib/graphql/schema/object.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ def authorized_new(object, context)
5151
trace_payload = { context: context, type: self, object: object, path: context[:current_path] }
5252

5353
maybe_lazy_auth_val = context.query.trace("authorized", trace_payload) do
54-
context.query.with_error_handling do
55-
begin
56-
authorized?(object, context)
57-
rescue GraphQL::UnauthorizedError => err
58-
context.schema.unauthorized_object(err)
59-
end
54+
begin
55+
authorized?(object, context)
56+
rescue GraphQL::UnauthorizedError => err
57+
context.schema.unauthorized_object(err)
58+
rescue StandardError => err
59+
context.query.handle_or_reraise(err)
6060
end
6161
end
6262

lib/graphql/schema/scalar.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ def default_scalar?
4242

4343
def validate_non_null_input(value, ctx)
4444
coerced_result = begin
45-
ctx.query.with_error_handling do
46-
coerce_input(value, ctx)
47-
end
45+
coerce_input(value, ctx)
4846
rescue GraphQL::CoercionError => err
4947
err
48+
rescue StandardError => err
49+
ctx.query.handle_or_reraise(err)
5050
end
5151

5252
if coerced_result.nil?

0 commit comments

Comments
 (0)