Skip to content

Commit

Permalink
fix(core): Error propagation mismatch (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseLion committed Feb 11, 2024
1 parent 30d6342 commit 36eae26
Show file tree
Hide file tree
Showing 6 changed files with 396 additions and 168 deletions.
19 changes: 13 additions & 6 deletions src/main/java/io/github/joselion/maybe/EffectHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public <X extends Throwable> EffectHandler<E> doOnError(final Class<X> ofType, f
* @param effect a consumer function that recieves the caught error
* @return the same handler to continue chainning operations
*/
public EffectHandler<E> doOnError(final Consumer<? super E> effect) {
public EffectHandler<E> doOnError(final Consumer<Throwable> effect) {
this.error.ifPresent(effect);

return this;
Expand Down Expand Up @@ -143,7 +143,7 @@ public <X extends Throwable> EffectHandler<E> catchError(final Class<X> ofType,
* @return an empty handler if the error is present. The same handler
* instance otherwise
*/
public EffectHandler<E> catchError(final Consumer<? super E> handler) {
public EffectHandler<E> catchError(final Consumer<Throwable> handler) {
return this.error
.map(caught -> {
handler.accept(caught);
Expand All @@ -164,10 +164,17 @@ public EffectHandler<E> catchError(final Consumer<? super E> handler) {
*/
public <X extends Throwable> EffectHandler<X> effect(
final ThrowingRunnable<? extends X> onSuccess,
final ThrowingConsumer<? super E, ? extends X> onError
final ThrowingConsumer<Throwable, ? extends X> onError
) {
return this.error
.map(Maybe.<E, X>partial(onError))
.map(e -> {
try {
onError.accept(e);
return EffectHandler.failure(Commons.<X>cast(e));
} catch (Throwable x) { // NOSONAR
return EffectHandler.failure(Commons.<X>cast(x));
}
})
.orElseGet(() -> Maybe.from(onSuccess));
}

Expand All @@ -191,7 +198,7 @@ public <X extends Throwable> EffectHandler<X> effect(final ThrowingRunnable<? ex
*
* @param effect a consumer function that receives the caught error
*/
public void orElse(final Consumer<? super E> effect) {
public void orElse(final Consumer<Throwable> effect) {
this.error.ifPresent(effect);
}

Expand All @@ -214,7 +221,7 @@ public void orThrow() throws E {
* @param mapper a function that maps the new exception to throw
* @throws X a mapped exception
*/
public <X extends Throwable> void orThrow(final Function<? super E, ? extends X> mapper) throws X {
public <X extends Throwable> void orThrow(final Function<Throwable, ? extends X> mapper) throws X {
if (this.error.isPresent()) {
throw mapper.apply(this.error.get());
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/github/joselion/maybe/Maybe.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public static <R extends Closeable, E extends Throwable> CloseableHandler<R, E>
return Maybe
.from(supplier)
.map(CloseableHandler::<R, E>from)
.orElse(CloseableHandler::failure);
.orElse(x -> CloseableHandler.failure(Commons.cast(x)));
}

/**
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/io/github/joselion/maybe/SolveHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public <X extends Throwable> SolveHandler<T, E> doOnError(final Class<X> ofType,
* @param effect a consumer function that receives the caught error
* @return the same handler to continue chainning operations
*/
public SolveHandler<T, E> doOnError(final Consumer<? super E> effect) {
public SolveHandler<T, E> doOnError(final Consumer<Throwable> effect) {
this.value.doOnLeft(effect);

return this;
Expand Down Expand Up @@ -164,7 +164,7 @@ public <X extends Throwable> SolveHandler<T, E> catchError(
* @return a handler containing a new value if an error was caught. The same
* handler instance otherwise
*/
public SolveHandler<T, E> catchError(final Function<? super E, ? extends T> handler) {
public SolveHandler<T, E> catchError(final Function<Throwable, ? extends T> handler) {
return this.value
.mapLeft(handler)
.mapLeft(SolveHandler::<T, E>from)
Expand All @@ -186,7 +186,7 @@ public SolveHandler<T, E> catchError(final Function<? super E, ? extends T> hand
* @return a new handler with either the solved value or the error
*/
public <X extends Throwable> SolveHandler<T, X> onErrorSolve(
final ThrowingFunction<? super E, ? extends T, ? extends X> solver
final ThrowingFunction<Throwable, ? extends T, ? extends X> solver
) {
return this.value
.unwrap(
Expand All @@ -202,21 +202,21 @@ public <X extends Throwable> SolveHandler<T, X> onErrorSolve(
* <p>This is helpful to try to solve values in different ways, like when
* nesting a try-catch in another catch block, but more functional.
*
* @param <X> the type of the error the new solver may throw
* @param <C> the type of the error to catch
* @param <X> the type of the error the new solver may throw
* @param ofType a class instance of the error type to catch
* @param solver a throwing function that receives the previous error and
* solves another value
* @return a new handler with either the solved value or the error
*/
public <X extends Throwable, C extends Throwable> SolveHandler<T, X> onErrorSolve(
final Class<? extends C> ofType,
final ThrowingFunction<? super E, ? extends T, ? extends X> solver
public <C extends Throwable, X extends Throwable> SolveHandler<T, X> onErrorSolve(
final Class<C> ofType,
final ThrowingFunction<? super C, ? extends T, ? extends X> solver
) {
return this.value
.unwrap(
x -> ofType.isInstance(x)
? Maybe.of(x).solve(solver)
? Maybe.of(x).map(Commons::<C>cast).solve(solver)
: SolveHandler.failure(Commons.cast(x)),
SolveHandler::from
);
Expand All @@ -241,7 +241,7 @@ public <X extends Throwable, C extends Throwable> SolveHandler<T, X> onErrorSolv
*/
public <S, X extends Throwable> SolveHandler<S, X> solve(
final ThrowingFunction<? super T, ? extends S, ? extends X> onSuccess,
final ThrowingFunction<? super E, ? extends S, ? extends X> onError
final ThrowingFunction<Throwable, ? extends S, ? extends X> onError
) {
return this.value.unwrap(
Maybe.partial(onError),
Expand All @@ -251,7 +251,7 @@ public <S, X extends Throwable> SolveHandler<S, X> solve(

/**
* Chain another solver function if the value was solved. Otherwise,
* returns a handler containing the error so it can be propagated upstream.
* returns a handler containing the error so it can be propagated downstream.
*
* @param <S> the type of value returned by the next operation
* @param <X> the type of exception the new solver may throw
Expand Down Expand Up @@ -282,7 +282,7 @@ public <S, X extends Throwable> SolveHandler<S, X> solve(
*/
public <X extends Throwable> EffectHandler<X> effect(
final ThrowingConsumer<? super T, ? extends X> onSuccess,
final ThrowingConsumer<? super E, ? extends X> onError
final ThrowingConsumer<Throwable, ? extends X> onError
) {
return this.value.unwrap(
Maybe.partial(onError),
Expand All @@ -293,7 +293,7 @@ public <X extends Throwable> EffectHandler<X> effect(
/**
* Chain the previous operation to an effect if the value was solved.
* Otherwise, returns a handler containing the error so it can be propagated
* upstream.
* downstream.
*
* @param <X> the type of the error the effect may throw
* @param effect a consume checked function to run in case of succeess
Expand Down Expand Up @@ -384,7 +384,7 @@ public T orElse(final T fallback) {
* another value
* @return the solved value if present. Another value otherwise
*/
public T orElse(final Function<? super E, ? extends T> mapper) {
public T orElse(final Function<Throwable, ? extends T> mapper) {
return this.value.unwrap(mapper, Function.identity());
}

Expand Down Expand Up @@ -434,15 +434,15 @@ public T orThrow() throws E {
}

/**
* Returns the value solved/handled if present. Throws another error otherwise.
* Returns the solved value if present. Throws another error otherwise.
*
* @param <X> the new error type
* @param mapper a function that receives the caught error and produces
* another exception
* @return the solved/handled value if present
* @throws X a mapped exception
*/
public <X extends Throwable> T orThrow(final Function<? super E, ? extends X> mapper) throws X {
public <X extends Throwable> T orThrow(final Function<Throwable, ? extends X> mapper) throws X {
return this.value
.rightToOptional()
.orElseThrow(() -> mapper.apply(this.value.leftOrNull()));
Expand Down Expand Up @@ -536,7 +536,7 @@ public <R extends AutoCloseable, X extends Throwable> CloseableHandler<R, X> sol
.of(prev)
.solve(solver)
.map(CloseableHandler::<R, X>from)
.orElse(CloseableHandler::failure)
.orElse(x -> CloseableHandler.failure(Commons.cast(x)))
);
}
}
2 changes: 1 addition & 1 deletion src/main/java17/io/github/joselion/maybe/Maybe.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public static <R extends Closeable, E extends Throwable> CloseableHandler<R, E>
return Maybe
.from(supplier)
.map(CloseableHandler::<R, E>from)
.orElse(CloseableHandler::failure);
.orElse(x -> CloseableHandler.failure(Commons.cast(x)));
}

/**
Expand Down

0 comments on commit 36eae26

Please sign in to comment.