We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Throwables currently has
public static <X extends Throwable> X getCauseAs(Throwable throwable, Class<X> expectedCauseType);
Can we add
public static Optional<X> getCauseAsOptional(Throwable throwable, Class<X> expectedCauseType){ if(expectedCauseType.isInstance(throwable.getCause())){ return Optional.of(expectedCauseType.cast(throwable.getCause());); } else { return Optional.empty(); } }
This enables clean conditional exception handling which can replace code this:
catch (ExecutionException e) { if ((e.getCause() instanceof AuthException)) { AuthException exception = (AuthException) e.getCause(); if (exception.getCode().equals(AuthException.Code.USER_NOT_FOUND)) { // ignore the exception } else { throw e; } } else { throw e; } }
with code like this:
catch (ExecutionException e) { getCauseAsOptional(e, AuthException.class) .map(authExp -> authExp.getCode()) .filter(AuthException.Code.USER_NOT_FOUND::equals) .orElseThrow(() -> e); }
The existing getCauseAs can be refactored to call this method internally.
getCauseAs
The text was updated successfully, but these errors were encountered:
Can this be implemented?
Sorry, something went wrong.
No branches or pull requests
Throwables currently has
Can we add
This enables clean conditional exception handling which can replace code this:
with code like this:
The existing
getCauseAs
can be refactored to call this method internally.The text was updated successfully, but these errors were encountered: