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

View File

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