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 {
|
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].
|
* was thrown, otherwise [Ok].
|
||||||
*/
|
*/
|
||||||
inline fun <T> of(function: () -> T): Result<T, Throwable> {
|
inline fun <T> of(function: () -> T): Result<T, Exception> {
|
||||||
return try {
|
return try {
|
||||||
Ok(function.invoke())
|
Ok(function.invoke())
|
||||||
} catch (t: Throwable) {
|
} catch (ex: Exception) {
|
||||||
Err(t)
|
Err(ex)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,12 +18,12 @@ internal class ResultTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
internal fun returnsErrIfInvocationFails() {
|
internal fun returnsErrIfInvocationFails() {
|
||||||
val throwable = IllegalArgumentException("throw me")
|
val exception = IllegalArgumentException("throw me")
|
||||||
val callback = { throw throwable }
|
val callback = { throw exception }
|
||||||
val error = Result.of(callback).getError()!!
|
val error = Result.of(callback).getError()!!
|
||||||
|
|
||||||
assertSame(
|
assertSame(
|
||||||
expected = throwable,
|
expected = exception,
|
||||||
actual = error
|
actual = error
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user