Skip to content

Commit

Permalink
Update error handling logic when TracedError when rescue_from with ba…
Browse files Browse the repository at this point in the history
…cktrace

* Restore original 2.4.8 behavior for GraphQL::Backtrace::TracedError
  • Loading branch information
yn-misaki committed Feb 10, 2025
1 parent 1e144de commit 9e332f0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/graphql/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1114,9 +1114,6 @@ def error_handlers

# @api private
def handle_or_reraise(context, err)
if context[:backtrace] || using_backtrace
err = GraphQL::Backtrace::TracedError.new(err, context)
end
handler = Execution::Errors.find_handler_for(self, err.class)
if handler
obj = context[:current_object]
Expand All @@ -1128,6 +1125,10 @@ def handle_or_reraise(context, err)
end
handler[:handler].call(err, obj, args, context, field)
else
if context[:backtrace] || using_backtrace
err = GraphQL::Backtrace::TracedError.new(err, context)
end

raise err
end
end
Expand Down
44 changes: 44 additions & 0 deletions spec/graphql/schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -560,4 +560,48 @@ class Query < GraphQL::Schema::Object
assert schema2.subscription
assert schema2.instance_variable_get(:@subscription_extension_added)
end

describe "backtrace error handling" do
class CustomError < RuntimeError; end
class Query < GraphQL::Schema::Object
field :test, Integer, null: false

def test
raise CustomError
end
end

it "raises a TracedError when backtrace is enabled" do
schema = Class.new(GraphQL::Schema) do
query(Query)
use GraphQL::Backtrace
end
query_str = '{ test }'

assert_raises(GraphQL::Backtrace::TracedError) do
schema.execute(query_str)
end
end

it "rescues them when using rescue_from with backtrace" do
schema = Class.new(GraphQL::Schema) do
query(Query)
use GraphQL::Backtrace

rescue_from(CustomError) do
raise GraphQL::ExecutionError.new('Handled CustomError')
end
end
query_str = '{ test }'
expected_errors = [
{
'message' => 'Handled CustomError',
'locations' => [{'line' => 1, 'column' => 3}],
'path' => ['test']
}
]

assert_equal expected_errors, schema.execute(query_str).to_h['errors']
end
end
end

0 comments on commit 9e332f0

Please sign in to comment.