You are viewing documentation for a previous version of this software.

Switch to the latest stable version.

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: a GraphQL response wasn't received because an error occurred while communicating with your GraphQL server. This might be an SSL error, a socket error because your app is offline, or a 500 or any other HTTP error. When a network error occurs, no data is returned.

  • GraphQL errors: a GraphQL response is received, and it contains a non-empty errors field. This means the server wasn't able to completely process the query. The response might include partial data if the server was able to process some of the query.

Network errors

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

Kotlin
1try {
2  val response = apolloClient.query(query).execute()
3} catch(exception: ApolloException) {
4  // handle exception here
5}

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 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:

GraphQL
1query FilmAndPersonQuery {
2  film(id: "ZmlsbXM6MQ==") {
3    title
4  }
5  person(id: "badId") {
6    name
7  }
8}

The server will send the following response:

JSON
1{
2  "data": {
3    "film": {
4      "title": "A New Hope"
5    },
6    "person": null
7  },
8  "errors": [
9    {
10      "message": "No entry in local cache for https://swapi.dev/api/people/m�H/",
11      "locations": [
12        {
13          "line": 35,
14          "column": 3
15        }
16      ],
17      "path": [
18        "person"
19      ]
20    }
21  ]
22}

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:

Kotlin
1val response = try {
2  apolloClient.query(query).execute()
3} catch(exception: ApolloException) {
4  // Network error, not much to do
5  throw exception
6}
7
8// It's possible to display the film title
9val title = response.data?.film?.title
10if (title != null) {
11  println(title)
12}
13
14// The person triggered an error
15val person = response.data?.person?.name
16if (person != null) {
17  // do something with response.errors
18}

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:

Text
1val data = try {
2  apolloClient.query(query).execute().dataAssertNoErrors
3} catch(exception: ApolloException) {
4  // All network or GraphQL errors are handled here
5  throw exception
6}
7
8// No need for safe calls on data
9val 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, learn about the @nonnull directive.

Feedback

Edit on GitHub

Forums