Error Handling

Schema Validation errors

Any good server should be able to handle errors well. Otherwise, it becomes harder and harder to maintain. Thankfully, the tools we’ve been using before help on this area.

In fact, if you try right now to send an invalid request to the server, such as a request with a field that doesn’t exist, you’ll already get a pretty good error message back. For example:

Request with a field that doesn't exist error

Application errors

Some errors will be specific to the application though. For example, let’s say that createLink is called with the url field as a string, as specified by the schema, but its content doesn’t follow an expected url format. You’ll need to throw an error yourself in this case.

Luckily, all you need to do is to detect the problem and throw the error.

GraphQL Gem provides an exception for just these cases GraphQL::ExecutionError.

Now every link, requires to have a url and description attributes.

Now when you try to submit a link with invalid arguments you get an error.

a link with invalid arguments you get an error

You can learn more about GraphQL errors here.

Next Chapter


Learn best practices for implementing filters in a GraphQL API using query arguments with a Ruby GraphQL server.

Go to next chapter