Retrofit: FATAL EXCEPTION: OkHttp Dispatcher - coroutines throwing exception from interceptor

Created on 15 Nov 2019  路  1Comment  路  Source: square/retrofit

Hi,

I am using Retrofit 2.6.2 and the latest version of coroutines. I am trying to throw an exception from an Interceptor and trying to catch it but each time it crashes the app. I am using the following app as an example https://github.com/harmittaa/KoinExample I modified the code to throw an exception in the AuthInterceptor.

I've tried a lot of different scopes such as the ones below however i am unable to catch the exception thrown from the interceptor and it crashes the app.

    viewModelScope.launch(Dispatchers.IO) {
        weather.postValue(Resource.loading(null))
        try {
            val data = weatherRepo.getWeather(input)
            weather.postValue(data)
        } catch (throwable : Throwable) {
        }
    }

viewModelScope.launch(Dispatchers.IO) {
        weather.postValue(Resource.loading(null))
        try {
           val deferred = async {
               weatherRepo.getWeather(input)
           }
            weather.postValue(deferred.await())
        } catch (throwable : Throwable) {
        }
    }

var weather = location.switchMap { location ->
    liveData(Dispatchers.IO) {
        emit(Resource.loading(null))
        try {
            emit(weatherRepo.getWeather(location))
        } catch (throwable : Throwable) {

        }
    }
}

Most helpful comment

The error was with the exception not extending IOException.

>All comments

The error was with the exception not extending IOException.

Was this page helpful?
0 / 5 - 0 ratings