Skip to content

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

Closed
michiomochi opened this issue Sep 14, 2018 · 5 comments
Closed

How to return custom GraphQL::ExecutionError by rescue_from #1850

michiomochi opened this issue Sep 14, 2018 · 5 comments

Comments

@michiomochi
Copy link

I want to return custom GraphQL::ExecutionError by rescue_from.
But it seems that it can not, now.

# Not work
class TestSchema < GraphQL::Schema
  rescue_from(ActiveRecord::RecordInvalid) do |error|
    GraphQL::ExecutionError.new(message: error.message, extensions: { "code" => "INVALID_RECORD" })
  end
end

# Work
class TestSchema < GraphQL::Schema
  rescue_from(ActiveRecord::RecordInvalid) do |error|
    error.message
  end
end

How do you think?

@michiomochi michiomochi changed the title How to return GraphQL::ExecutionError by rescue_from How to return custom GraphQL::ExecutionError by rescue_from Sep 14, 2018
@michiomochi
Copy link
Author

I found GraphQL::Schema#rescue_middleware.
https://github.com/rmosolgo/graphql-ruby/blob/master/lib/graphql/schema.rb#L1026

So, override GraphQL::Schema#rescue_middleware and define custom rescue middleware.

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

@michiomochi
Copy link
Author

Please tell me if there is a better way. 🙏

@dhruv-shipmnts
Copy link

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.

@DamirSvrtan
Copy link

DamirSvrtan commented Feb 5, 2019

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.

@rmosolgo
Copy link
Owner

rmosolgo commented Sep 2, 2019

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!

@rmosolgo rmosolgo closed this as completed Sep 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants