Skip to content

Pass custom exception handlers in unchecked() and lift() #28

Description

@hanslovsky

Both the lift() and uncheck() methods would benefit from adding a signature that passes a custom exception handler, e.g:

   default BiFunction<T1, T2, R> unchecked(Function<Exception, R> handler) {
        return (arg1, arg2) -> {
            try {
                return apply(arg1, arg2);
            } catch (final Exception e) {
                return handler.apply(e);
            }
        };
    }

    default BiFunction<T1, T2, Optional<R>> lift(Function<Exception, Optional<R>> handler) {
        return (arg1, arg2) -> {
            try {
                return Optional.of(apply(arg1, arg2));
            } catch (Exception e) {
                return handler.apply(e);
            }
        };
    }

(code not tested but I hope it conveys the idea)
The original signatures could just delegate to these appropriately.
I created similar classes/methods for a UI package (it was never good enough for a separate package like yours), and I found it very useful to be able to pass a handler, e.g. for displaying information to the user.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions