Catch Exception instead of Throwable in Result.of

This commit is contained in:
Michael Bull 2018-01-11 18:25:06 +00:00
parent cfaa57dd1f
commit 76870ef78a
2 changed files with 7 additions and 7 deletions

View File

@ -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)
}
}
}

View File

@ -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
)
}