Docs
Try Apollo Studio

Error handling


Whenever you execute a GraphQL operation with Apollo Kotlin (or any other GraphQL client), two high-level types of errors can occur:

Network errors

Network errors throw an ApolloException. To handle them, wrap your query in a try/catch block:

try {
val response = apolloClient.query(query).execute()
} catch(exception: ApolloException) {
// handle exception here
}

Non-exhaustive list of possible causes:

  • The app is offline or doesn't have access to the network.
  • A DNS error occurred, making it impossible to look up the host.
  • An SSL error occurred (e.g., the server certificate isn't trusted).
  • The connection was closed.
  • The server responded with a non-successful HTTP code.
  • The server didn't respond with valid JSON.
  • The response JSON doesn't satisfy the schema and cannot be parsed.
  • A request was specified as CacheOnly but the data wasn't cached.
  • ...

Examine the exception to get more detailed information about the actual error happening.

GraphQL errors

Because GraphQL errors might still contain data, they do not throw. Instead, they return a Response with a Response.errors field indicating the errors that occurred.

For example, the following query uses an invalid id to look up a Person:

query FilmAndPersonQuery {
film(id: "ZmlsbXM6MQ==") {
title
}
person(id: "badId") {
name
}
}

The server will send the following response:

{
"data": {
"film": {
"title": "A New Hope"
},
"person": null
},
"errors": [
{
"message": "No entry in local cache for https://swapi.dev/api/people/m�H/",
"locations": [
{
"line": 35,
"column": 3
}
],
"path": [
"person"
]
}
]
}

Note that while there are errors, the query successfully returned the title of the film: A New Hope. In general, any error while executing an operation bubbles up to the next nullable field. In this case, person is nullable. In the worst case, response.data can be null if everything else is non-nullable.

Apollo Kotlin gives you access to both the data and the errors in the Response class:

val response = try {
apolloClient.query(query).execute()
} catch(exception: ApolloException) {
// Network error, not much to do
throw exception
}
// It's possible to display the film title
val title = response.data?.film?.title
if (title != null) {
println(title)
}
// The person triggered an error
val person = response.data?.person?.name
if (person != null) {
// do something with response.errors
}

Ignoring partial data

If you don't want to handle partial responses, you can simplify your error handling logic. ApolloResponse.dataAssertNoErrors will return a non-nullable data and throw if there are any errors. This way, you can handle all errors in a single catch {} block:

val data = try {
apolloClient.query(query).execute().dataAssertNoErrors
} catch(exception: ApolloException) {
// All network or GraphQL errors are handled here
throw exception
}
// No need for safe calls on data
val title = data.film?.title

Note that the safe call is still required on film because this field has a nullable type in the GraphQL schema. There are ways to override this in codegen. For more details, see @nonnull.

Edit on GitHub
Previous
GraphQL variables
Next
Custom scalars