Skip to content

Exceptions from within promises #566

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
staugaard opened this issue Feb 24, 2017 · 3 comments
Closed

Exceptions from within promises #566

staugaard opened this issue Feb 24, 2017 · 3 comments

Comments

@staugaard
Copy link
Contributor

It looks like the RescueMiddleware does not handle exceptions coming from within promises.

When we hit a ActiveRecord::RecordNotFound in a non-lazy resolver then the middleware takes care of it, but when we hit that same exception inside a lazy promise resolver, then the error makes it's way all the way out to the rails controller.

@rmosolgo
Copy link
Owner

Oh, interesting. I guess that's a flaw in the runtime design. Middleware only wraps resolve, not lazy_resolve.

I guess the best "fix" is to introduce an error handling plugin like

MySchema = GraphQL::Schema.define do 
  use GraphQL::Rescues
end 

which will wrap both eager- and lazy-loading with error handling. (In my opinion, the real best case would be to avoid raising errors altogether 😆 )

@ianks
Copy link
Contributor

ianks commented Mar 8, 2017

You can catch the error and return a GraphQL::Exception. This is how I handle promises in one of my resolvers. Note: al the Rails.appliction.executor.wrap stuff is to avoid autoload deadlocks in Rails 5 😞 :

    def promise(&_blk)
      Rails.application.executor.wrap do
        Concurrent::Promise
          .execute { Rails.application.executor.wrap { yield } }
          .catch { |e| handle_error(e) }
      end
    end

    def handle_error(e)
      Rails.logger.error(e)
      Rails.logger.error(e.backtrace.join("\n"))
      GraphQL::ExecutionError.new(e.message)
    end

@rmosolgo
Copy link
Owner

rmosolgo commented Sep 2, 2019

I just merged some new error handling for the interpreter that properly catches errors in promises: #2458

If you give it a try and have a problem, please open 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

3 participants