Created
June 22, 2021 08:40
-
-
Save abos3d/fe5bd83be3cef7d800bdbbe787d8ae75 to your computer and use it in GitHub Desktop.
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
package com.cleanarchitectkotlinflowhiltsimplestway.utils | |
import com.cleanarchitectkotlinflowhiltsimplestway.domain.AuthenticationException | |
import com.cleanarchitectkotlinflowhiltsimplestway.domain.NetworkErrorException | |
import com.cleanarchitectkotlinflowhiltsimplestway.domain.State | |
import retrofit2.HttpException | |
import java.net.ConnectException | |
import java.net.SocketTimeoutException | |
import java.net.UnknownHostException | |
class Utils { | |
companion object{ | |
fun resolveError(e: Exception): State.ErrorState { | |
var error = e | |
when (e) { | |
is SocketTimeoutException -> { | |
error = NetworkErrorException(errorMessage = "connection error!") | |
} | |
is ConnectException -> { | |
error = NetworkErrorException(errorMessage = "no internet access!") | |
} | |
is UnknownHostException -> { | |
error = NetworkErrorException(errorMessage = "no internet access!") | |
} | |
} | |
if(e is HttpException){ | |
when(e.code()){ | |
502 -> { | |
error = NetworkErrorException(e.code(), "internal error!") | |
} | |
401 -> { | |
throw AuthenticationException("authentication error!") | |
} | |
400 -> { | |
error = NetworkErrorException.parseException(e) | |
} | |
} | |
} | |
return State.ErrorState(error) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment