-
Notifications
You must be signed in to change notification settings - Fork 1.4k
How to return custom GraphQL::ExecutionError by rescue_from #1850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I found So, override class MySchema < GraphQL::Schema
protected def rescue_middleware
@rescue_middleware ||= MyRescueMiddleware.new.tap { |m| middleware.insert(0, m) }
end
rescue_from(ActiveRecord::RecordInvalid) do |error|
GraphQL::ExecutionError.new(message: error.message, extensions: { "code" => "INVALID_RECORD" })
end
end
class MyRescueMiddleware < ::GraphQL::Schema::RescueMiddleware
private def attempt_rescue(err)
rescue_table.each { |klass, handler|
if klass.is_a?(Class) && err.is_a?(klass) && handler
return handler.call(err)
end
}
raise(err)
end
end |
Please tell me if there is a better way. 🙏 |
Have you found any other way to do this, i am facing the same issue too. |
I've used the graphql-errors gem for cases like this which allows for passing extensions: GraphQL::Errors.configure(Schema) do
rescue_from ActiveRecord::RecordInvalid do |error, _object, _args, ctx|
ctx.add_error(GraphQL::ExecutionError.new(message: error.message, extensions: { "code" => "INVALID_RECORD" }))
end
end The gem rescues on a per field basis, so that means you can still serve partial responses. |
Hi, I just merged some new error handling for the interpreter runtime which supports returning/raising custom errors: #2458 Please give it a try and let me know if you run into any other trouble by opening a new issue! |
I want to return custom GraphQL::ExecutionError by rescue_from.
But it seems that it can not, now.
How do you think?
The text was updated successfully, but these errors were encountered: