android crashes after receiving http exceptions in kotlin

Viewed 642

I am newer in android. My android app is crashing after get 404 or 500 errors

Error message: "retrofit2.HttpException: HTTP 404 Not Found at retrofit2.KotlinExtensions$await$2$2.onResponse(KotlinExtensions.kt:53)"

The same message after receiving 500 error.

I am using retrofit2 and okHttpClient. I have ApiModule class where are joining 2 interceptor: HttpLoggingInterceptor and my AuthErrorInterceptor where i check apiToken.

@Provides
@Singleton
@Named("default_http_client")
fun okHttpClient(
    authInterceptor: AuthInterceptor,
    authErrorInterceptor: AuthErrorInterceptor,
    settingsInteractor: SettingsInteractor,
    errorsInterceptor: ErrorsInterceptor
): OkHttpClient {
    val okHttpBuilder = OkHttpClient.Builder()
        .writeTimeout(DEFAULT_CLIENT_TIMEOUT, TimeUnit.SECONDS)
        .connectTimeout(DEFAULT_CLIENT_TIMEOUT, TimeUnit.SECONDS)
        .readTimeout(DEFAULT_CLIENT_TIMEOUT, TimeUnit.SECONDS)
    okHttpBuilder.addInterceptor(authInterceptor)
    val loggingInterceptor = HttpLoggingInterceptor(object : HttpLoggingInterceptor.Logger {
        override fun log(message: String) {
            okHttpLogger.debug(message)
        }
    })
    loggingInterceptor.level =
        if (settingsInteractor.getSetting(Settings.LOGS_ENABLED) || BuildUtils.isRelease())
            HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.BASIC
    okHttpBuilder.addInterceptor(loggingInterceptor)
    okHttpBuilder.addInterceptor(authErrorInterceptor)
    okHttpBuilder.addInterceptor(errorsInterceptor) <- for catching errors
    return okHttpBuilder.build()
}

And how can i understand, i need create new interceptor for other errors, but i don't understand what i need to do after check error code for not crashing application

My interceptor

override fun intercept(chain: Interceptor.Chain): Response {
    val request = chain.request()
    val response: Response
    try {
        response = chain.proceed(request)
        Log.d("try???", response.toString())
        if (response.code == 500) {
            Log.d("500Error???", response.toString())
        } else if (response.code == 404) {
            Log.d("404Error???", response.toString())
        } else if (response.code == 403) {
            Log.d("403Error???", response.toString())
        }
    } catch (e: Exception) {
        throw e
    }
    return response
}
0 Answers
Related