Skip to content

Instantly share code, notes, and snippets.

@sasssass
Last active May 19, 2020 22:10
Show Gist options
  • Save sasssass/0e2846a7c4021d416f81bf8bb0b1cbf8 to your computer and use it in GitHub Desktop.
Save sasssass/0e2846a7c4021d416f81bf8bb0b1cbf8 to your computer and use it in GitHub Desktop.
privateApiCall
private suspend fun <T> privateApiCall(emitter: RemoteErrorEmitter, responseFunction: suspend () -> T): T? {
try{
return withTimeout(5000){
responseFunction()
}
}catch (e: Exception){
withContext(Dispatchers.Main){
e.printStackTrace()
Log.e("ApiCalls", "Call error: ${e.localizedMessage}", e.cause)
when(e){
is HttpException -> {
val body = e.response()?.errorBody()
emitter.onError(getErrorMessage(body))
}
is TimeoutCancellationException -> emitter.onError(ErrorType.TIMEOUT)
is IOException -> emitter.onError(ErrorType.NETWORK)
else -> emitter.onError(ErrorType.UNKNOWN)
}
}
return null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment