Catch Exception instead of Throwable in Result.of
This commit is contained in:
parent
cfaa57dd1f
commit
76870ef78a
@ -11,14 +11,14 @@ sealed class Result<out V, out E> {
|
||||
companion object {
|
||||
|
||||
/**
|
||||
* Invokes a [function] and wraps it in a [Result], returning an [Err] if a [Throwable]
|
||||
* Invokes a [function] and wraps it in a [Result], returning an [Err] if an [Exception]
|
||||
* was thrown, otherwise [Ok].
|
||||
*/
|
||||
inline fun <T> of(function: () -> T): Result<T, Throwable> {
|
||||
inline fun <T> of(function: () -> T): Result<T, Exception> {
|
||||
return try {
|
||||
Ok(function.invoke())
|
||||
} catch (t: Throwable) {
|
||||
Err(t)
|
||||
} catch (ex: Exception) {
|
||||
Err(ex)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,12 +18,12 @@ internal class ResultTest {
|
||||
|
||||
@Test
|
||||
internal fun returnsErrIfInvocationFails() {
|
||||
val throwable = IllegalArgumentException("throw me")
|
||||
val callback = { throw throwable }
|
||||
val exception = IllegalArgumentException("throw me")
|
||||
val callback = { throw exception }
|
||||
val error = Result.of(callback).getError()!!
|
||||
|
||||
assertSame(
|
||||
expected = throwable,
|
||||
expected = exception,
|
||||
actual = error
|
||||
)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user