Skip to content

Instantly share code, notes, and snippets.

@murattuzel
Created November 14, 2021 18:58
Show Gist options
  • Save murattuzel/b2e6b39eafdac0a7772a78e2b2c0fc64 to your computer and use it in GitHub Desktop.
Save murattuzel/b2e6b39eafdac0a7772a78e2b2c0fc64 to your computer and use it in GitHub Desktop.
Error interceptor catches if an exception is thrown.
class ErrorHandlingInterceptor(
private val networkHandler: NetworkHandler
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
if (!networkHandler.isConnected) {
throw NoConnectivityError
}
return try {
chain.proceed(chain.request())
} catch (e: Exception) {
throw when (e) {
is UnknownHostException -> UnknownHostError
is HttpException -> HttpError(e.code(), e.message())
is SocketTimeoutException -> TimeOutError(e.message)
else -> IOException(e)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment