Apollo-client: How to catch actual error in ApolloClient

Created on 16 Oct 2018  路  4Comments  路  Source: apollographql/apollo-client

ApolloClient version 2.0 uses Observable instead of Promise. This is how I handle token refreshing. My TokenService.refreshToken can throw a custom TokenRefreshException

const client = new ApolloClient({
  uri: 'https://www.myapp.no/api/',
  request: async (operation) => {
    const token = await TokenStorage.fetchToken()
    updateOperation(operation, token)
  },
  onError: (error) => {
    return handleAuthError(error)
  }
})

const handleAuthError = (error) => {
  return new Observable((observer) => {
    TokenService.refreshToken()
      .then((token) => {
        updateOperation(error.operation, token)
      })
      .then(() => {
        const subscriber = {
          next: observer.next.bind(observer),
          error: observer.error.bind(observer),
          complete: observer.complete.bind(observer)
        }

        error.forward(error.operation).subscribe(subscriber)
      })
      .catch((e) => {
        // 馃槵馃槵馃槵馃槵馃槵馃槵
        // e here is an instance of TokenRefreshException
        observer.error(e)
      })
  })
}

However, when I use my client, the e in catch is a Network error undefined

try {
  const a = await client.mutate({
    mutation: myQuery,
    variables: myVariables
  })
} catch(e) {
  // 馃槺馃槺馃槺馃槺馃槺
  // e here is Network error undefined
  throw e
}

How can I get my actual custom TokenRefreshException?

Versions
px: installed 1 in 2.09s

System:
OS: macOS 10.14
Binaries:
Node: 8.11.4 - ~/.nodenv/versions/8.11.4/bin/node
npm: 6.4.1 - ~/.nodenv/versions/8.11.4/bin/npm
Browsers:
Chrome: 69.0.3497.100
Firefox: 62.0.3
Safari: 12.0
npmPackages:
apollo-boost: ^0.1.16 => 0.1.16

Most helpful comment

I use ApolloClient mostly for its caching, which does not work either. I stopped using this library.

All 4 comments

Having the same issue here. Is there really no way to handle errors locally? I feel strongly that there should be (though the documentation is mostly silent).

I use ApolloClient mostly for its caching, which does not work either. I stopped using this library.

@onmyway133 would love to know what you're using as a replacement for a client side library for executing queries and mutations. just plain XHR via something like axios?

@jakedowns Ja I use something like axios, with homegrown caching

Was this page helpful?
0 / 5 - 0 ratings