Last active
May 19, 2020 22:10
-
-
Save sasssass/0e2846a7c4021d416f81bf8bb0b1cbf8 to your computer and use it in GitHub Desktop.
privateApiCall
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
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