Created
December 17, 2020 15:17
-
-
Save mcungl/3bec372087b5f07d23e378ef7e024658 to your computer and use it in GitHub Desktop.
rx result handling
This file contains hidden or 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
| fun <T> Observable<T>.toResult(): Observable<Result<T>> { | |
| return map { Result.fromData(it) } | |
| .onErrorResumeNext { | |
| Observable.just(Result.fromError(it)) | |
| } | |
| } | |
| fun <T> Observable<Result<T>>.onlySuccess(): Observable<T> { | |
| return filter { it.isSuccess() } | |
| .filter { it.data != null } | |
| .map { it.data!! } | |
| } | |
| fun <T> Observable<Result<T>>.onlyError(): Observable<Throwable> { | |
| return filter { it.isError() } | |
| .map { it.error!! } | |
| } | |
| fun <T> Observable<Result<T>>.onlyHttpException(): Observable<HttpException> { | |
| return filter { it.isError() && it.error is HttpException } | |
| .map { it.error as HttpException } | |
| } | |
| fun <T> Observable<Result<T>>.onlyHttpException(code: Int): Observable<HttpException> { | |
| return onlyHttpException() | |
| .filter { it.code() == code } | |
| } | |
| fun <T> Observable<Result<T>>.onlyHttpExceptionExcluding(vararg codes: Int): Observable<HttpException> { | |
| return onlyHttpException() | |
| .filter { codes.contains(it.code()) } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment