Skip to content

Commit

Permalink
Merge pull request #4 from vjames19/mapError
Browse files Browse the repository at this point in the history
preferring the reified version of mapError
  • Loading branch information
vjames19 authored Jul 11, 2017
2 parents 1a94b97 + 52daad8 commit 176667f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ mapError allows you only to transform the error types you are interested
import io.github.vjames19.futures.jdk8.*

val failed = Future<String> { throw IllegalArgumentException() }
.mapError(IllegalArgumentException::class) {
.mapError { e: IllegalArgumentException ->
// handle the IllegalArgumentException here and return a more pertinent exception
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import java.util.function.BiConsumer
import java.util.function.BiFunction
import java.util.function.Function
import java.util.function.Supplier
import kotlin.reflect.KClass

// Creation
inline fun <A> Future(executor: Executor = ForkJoinExecutor, crossinline block: () -> A): CompletableFuture<A> =
Expand Down Expand Up @@ -52,10 +51,10 @@ inline fun <A> CompletableFuture<A>.recoverWith(executor: Executor = ForkJoinExe
return future
}

inline fun <A, E : Throwable> CompletableFuture<A>.mapError(clazz: KClass<E>, crossinline f: (E) -> Throwable): CompletableFuture<A> = exceptionally {
inline fun <A, reified E : Throwable> CompletableFuture<A>.mapError(crossinline f: (E) -> Throwable): CompletableFuture<A> = exceptionally {
val throwable = it.cause ?: it
if (clazz.isInstance(throwable)) {
throw f(clazz.java.cast(throwable))
if (throwable is E) {
throw f(throwable)
} else {
throw throwable
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,26 +115,26 @@ object FutureSpec : Spek({
describe("mapError") {
given("a successful future") {
it("it shouldn't have to map the error") {
success.mapError(IllegalArgumentException::class) { IllegalStateException() }.get() shouldEqual 1
success.mapError { _: IllegalArgumentException -> IllegalStateException() }.get() shouldEqual 1
}
}

given("a failed future") {
on("an exception type that its willing to handle") {
it("should map the error") {
{ failed.mapError(IllegalArgumentException::class) { UnsupportedOperationException() }.get() } shouldThrowTheException AnyException withCause (UnsupportedOperationException::class)
{ failed.mapError { _: IllegalArgumentException -> UnsupportedOperationException() }.get() } shouldThrowTheException AnyException withCause (UnsupportedOperationException::class)
}
}

on("an exception type that it didn't register for") {
it("should throw the original error") {
{ failed.mapError(IllegalStateException::class) { UnsupportedOperationException() }.get() } shouldThrowTheException AnyException withCause (IllegalArgumentException::class)
{ failed.mapError { _: ClassNotFoundException -> UnsupportedOperationException() }.get() } shouldThrowTheException AnyException withCause (IllegalArgumentException::class)
}
}

on("handling the supertype Throwable") {
it("should map the error") {
{ failed.mapError(Throwable::class) { UnsupportedOperationException() }.get() } shouldThrowTheException AnyException withCause (UnsupportedOperationException::class)
{ failed.mapError { _: Throwable -> UnsupportedOperationException() }.get() } shouldThrowTheException AnyException withCause (UnsupportedOperationException::class)
}
}
}
Expand Down

0 comments on commit 176667f

Please sign in to comment.