Created
November 14, 2021 18:58
-
-
Save murattuzel/b2e6b39eafdac0a7772a78e2b2c0fc64 to your computer and use it in GitHub Desktop.
Error interceptor catches if an exception is thrown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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