Simplify error handling
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
package gq.kirmanak.mealient.data.baseurl
|
||||
|
||||
import gq.kirmanak.mealient.data.network.NetworkError
|
||||
import gq.kirmanak.mealient.data.network.ServiceFactory
|
||||
import gq.kirmanak.mealient.extensions.mapToNetworkError
|
||||
import gq.kirmanak.mealient.extensions.runCatchingExceptCancel
|
||||
import gq.kirmanak.mealient.extensions.versionInfo
|
||||
import kotlinx.serialization.SerializationException
|
||||
import retrofit2.HttpException
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
@@ -17,21 +16,12 @@ class VersionDataSourceImpl @Inject constructor(
|
||||
override suspend fun getVersionInfo(baseUrl: String): VersionInfo {
|
||||
Timber.v("getVersionInfo() called with: baseUrl = $baseUrl")
|
||||
|
||||
val service = try {
|
||||
serviceFactory.provideService(baseUrl)
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e, "getVersionInfo: can't create service")
|
||||
throw NetworkError.MalformedUrl(e)
|
||||
}
|
||||
|
||||
val response = try {
|
||||
val service = serviceFactory.provideService(baseUrl)
|
||||
val response = runCatchingExceptCancel {
|
||||
service.getVersion()
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e, "getVersionInfo: can't request version")
|
||||
when (e) {
|
||||
is HttpException, is SerializationException -> throw NetworkError.NotMealie(e)
|
||||
else -> throw NetworkError.NoServerConnection(e)
|
||||
}
|
||||
}.getOrElse {
|
||||
Timber.e(it, "getVersionInfo: can't request version")
|
||||
throw it.mapToNetworkError()
|
||||
}
|
||||
|
||||
return response.versionInfo()
|
||||
|
||||
Reference in New Issue
Block a user