Skip to content

Instantly share code, notes, and snippets.

@abos3d
Created June 22, 2021 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abos3d/fe5bd83be3cef7d800bdbbe787d8ae75 to your computer and use it in GitHub Desktop.
Save abos3d/fe5bd83be3cef7d800bdbbe787d8ae75 to your computer and use it in GitHub Desktop.
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