Error Handling in Mutation

Learn how to handle errors in mutation.

We'll cover the following

Handling mutation errors

We need to deal with errors that might occur in the execution of our mutations. We’ll cover two major strategies we can use to report errors to users. While we can’t always make them happy, we can at least let them know what went wrong.

Before we dig into error handling, we need to set up a good example. Let’s add a constraint in our PlateSlate application business logic so we can easily trigger an error when creating menu items.

Right now, in our PlateSlate application, we’re very permissive about menu item creation. Besides checking that names and prices are provided, we allow any menu item to be created. We even allow menu items with duplicate names. Let’s add a basic constraint in our database to prevent that.

Since we’re using Ecto for our PlateSlate project, we’ll use mix to generate a migration:

Get hands-on with 1400+ tech skills courses.