Skip to content

Commit

Permalink
refactor(base/Result): enhance Result object with getOrThrow for runC…
Browse files Browse the repository at this point in the history
…atching
  • Loading branch information
benjohnde committed Aug 26, 2020
1 parent 52b7391 commit c566048
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions auth/src/commonMain/kotlin/com/liftric/base/Result.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ class Result<out T> constructor(val value: Any?) {
}
}

fun Result<*>.throwOnFailure() {
if (value is Result.Failure) throw value.exception
}

inline fun <T> Result<T>.getOrThrow(): T {
throwOnFailure()
return value as T
}

inline fun <T> Result<T>.onFailure(action: (exception: Throwable) -> Unit): Result<T> {
exceptionOrNull()?.let { action(it) }
return this
Expand Down

0 comments on commit c566048

Please sign in to comment.