Skip to content

Instantly share code, notes, and snippets.

@davidbilik
Created June 28, 2018 14:03
Show Gist options
  • Save davidbilik/0dbddbfc78682e270299e27b2997063d to your computer and use it in GitHub Desktop.
Save davidbilik/0dbddbfc78682e270299e27b2997063d to your computer and use it in GitHub Desktop.
fun <T> Single<T>.mapApiExceptions(errorCodeMapper: ((HttpException) -> Exception?)? = null): Single<T> {
return onErrorResumeNext { err: Throwable ->
val exception = when (err) {
is HttpException -> {
errorCodeMapper?.invoke(err) ?: GeneralServerException(err.code())
}
is IOException -> NoInternetConnectionException()
else -> UnexpectedException(err)
}
Single.error<T>(exception)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment