Skip to content

Instantly share code, notes, and snippets.

@chenzhang2006
Last active April 1, 2022 17:33
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 chenzhang2006/6b04095ca5fd76ce4470ca01d93dc8af to your computer and use it in GitHub Desktop.
Save chenzhang2006/6b04095ca5fd76ce4470ca01d93dc8af to your computer and use it in GitHub Desktop.
Full helper to handle exceptions
internal suspend inline fun <T> getResult(block: () -> T): Result<T> = try {
block().let { Result.success(it) }
} catch (e: Exception) {
if (e is CancellationException) throw e
Timber.e("Error from coroutine (${coroutineContext[CoroutineName]?.name}): " +
"${e.stackTraceToString()}")
Result.failure(e)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment