https://kotlinlang.org logo
Title
m

maxgdn

02/12/2022, 7:41 PM
How might custom exceptions be thrown from an okhttp interceptor? I would like the exceptions to surface past all of the interceptors so that they can be handled elsewhere. More in thread ->
Currently the following exists.
val apolloOkHttpBuilder = OkHttpClient.Builder()
   .connectTimeout(connectTimeout,TimeUnit.SECONDS)
   .writeTimeout(writeTimeout,TimeUnit.SECONDS)
   .readTimeout(readTimeout,TimeUnit.SECONDS)
   .addInterceptor(ErrorInterceptor(context))
   .addInterceptor(AuthorizationInterceptor())
   .addInterceptor(MetadataInterceptor())

…

val apolloClient = ApolloClient.Builder()
   .serverUrl(url)
   .normalizedCache(
       cacheFactory,
       cacheKeyGenerator = cacheKeyGenerator,
       cacheResolver = cacheKeyResolver,
   )
   .storePartialResponses(true)
   .fetchPolicy(FetchPolicy.CacheFirst)
   .okHttpClient(
       apolloOkHttpBuilder.build()
   )
   .interceptors(listOf(ApolloErrorInterceptor()))
   .build()
ErrorInterceptor is based off of https://stackoverflow.com/a/65469952/13215280 If an error exists, it returns an http status code 999 and adds a header with the content of it. It retrieves the msg in the ApolloErrorInterceptor
class ApolloErrorInterceptor: ApolloInterceptor {
   override fun <D : Operation.Data> intercept(
       request: ApolloRequest<D>,
       chain: ApolloInterceptorChain
   ): Flow<ApolloResponse<D>> {
       val response = chain.proceed(request)
       return response.catch { e ->
           when(e) {
               is ApolloHttpException -> {
                   val header = e.headers.find { it.name == EXCEPTION_HEADER_NAME }
                   header?.let { throw ApolloNetworkException(it.value) }
               }
           }
       }
   }
}
This currently allows us to get the error but for some reason the cache is now breaking and optimisticUpdates are failing to be applied. Specifically
java.lang.IllegalStateException: no cache configured
I found a similar thread but I don’t understand it completely. https://github.com/apollographql/apollo-kotlin/issues/2968 Is it possible to accomplish this? Any resources/docs/links? Many Thanks!
Oh! also currently using
apollo_version = '3.0.0'
which is awesome! I was able to perform a refactor and remove a ton of code.
🎉 2
m

mbonnin

02/12/2022, 9:44 PM
https://github.com/apollographql/apollo-kotlin/issues/2968 is about HTTP cache so looks like you're hitting something different
.interceptors(listOf(ApolloErrorInterceptor()))
Looks like this is the issue. This is going to reset the ApolloCacheInterceptor that is internally set by
normalizedCache
Maybe we should hide
interceptors()
actually. This is a dangerous API 🤔
:blob-thinking-upside-down: 2
In all cases, use
addInterceptor()
instead of
interceptors()
and you should be good
👍 1
m

maxgdn

02/12/2022, 9:53 PM
Thank you kindly! Switching the
interceptors()
to
addInterceptor()
has mended the
java.lang.IllegalStateException: no cache configured
from throwing.
🎉 1