Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/graphql/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,7 @@ def handle_or_reraise(context, err)
end
handler[:handler].call(err, obj, args, context, field)
else
if context[:backtrace] || using_backtrace
if (context[:backtrace] || using_backtrace) && !err.is_a?(GraphQL::ExecutionError)
err = GraphQL::Backtrace::TracedError.new(err, context)
end

Expand Down
28 changes: 28 additions & 0 deletions spec/graphql/backtrace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def execute_multiplex(multiplex:)
"name" => Proc.new { |obj| obj[:name] == :boom ? raise("Boom!") : obj[:name] },
"listField" => Proc.new { :not_a_list },
"raiseField" => Proc.new { |o, a| raise("This is broken: #{a[:message]}") },
"executionError" => Proc.new { raise GraphQL::ExecutionError, "Client-facing error" }
},
"ThingWrapper" => {
"thing" => Proc.new { |obj| obj[:thing] },
Expand All @@ -69,6 +70,7 @@ def execute_multiplex(multiplex:)
name: String
listField: [OtherThing]
raiseField(message: String!): Int
executionError: Int
}

type ThingWrapper {
Expand Down Expand Up @@ -110,6 +112,10 @@ def execute_multiplex(multiplex:)
end
end

it "doesn't wrap GraphQL::ExecutionError" do
assert_equal ["Client-facing error"], backtrace_schema.execute("{ field1 { executionError } }")["errors"].map { |e| e["message"] }
end

it "annotates crashes from user code" do
err = assert_raises(GraphQL::Backtrace::TracedError) {
backtrace_schema.execute <<-GRAPHQL, root_value: "Root"
Expand Down Expand Up @@ -295,4 +301,26 @@ def result
query = GraphQL::Query.new(schema, "{ __typename }", context: { backtrace: true })
assert_includes query.current_trace.class.ancestors, custom_trace
end

describe "When validators are used" do
class ValidatorBacktraceSchema < GraphQL::Schema
class Query < GraphQL::Schema::Object
field :greeting, String do
argument :name, String, validates: { length: { minimum: 5 }}
end

def greeting(name:)
"Hello, #{name}!"
end
end

query(Query)
use GraphQL::Backtrace
end

it "works properly" do
assert_equal "Hello, Albert!", ValidatorBacktraceSchema.execute("{ greeting(name: \"Albert\") }")["data"]["greeting"]
assert_equal ["name is too short (minimum is 5)"], ValidatorBacktraceSchema.execute("{ greeting(name: \"Tim\") }")["errors"].map { |e| e["message"] }
end
end
end