Docs
Launch GraphOS Studio

Error handling


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

Network errors

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

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

Causes

Possible causes of a network error include (but are not limited to):

  • 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 for more detailed information about the actual error.

GraphQL errors

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

For example, the following 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 successfully returned the title of the film: A New Hope. In general, any error while executing an

to the next nullable . In this case, person is nullable. In the worst case, response.data can be null if everything else is non-nullable.

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 returns a non-nullable data and throws 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 has a nullable type in the . There are ways to override this in codegen. For more details, learn about

.

Previous
GraphQL variables
Next
Custom scalars
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company