Skip to content

Instantly share code, notes, and snippets.

@kardolus
Created June 6, 2024 22:15
Show Gist options
  • Save kardolus/2452ceeb563790c3f4874658de670600 to your computer and use it in GitHub Desktop.
Save kardolus/2452ceeb563790c3f4874658de670600 to your computer and use it in GitHub Desktop.
@Override
public synchronized void init( ServletConfig servletConfig ) throws ServletException {
GatewayConfig gatewayConfig = (GatewayConfig) servletConfig.getServletContext().getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE);
isErrorMessageSanitizationEnabled = gatewayConfig.isErrorMessageSanitizationEnabled();
errorMessageSanitizationPattern = gatewayConfig.getErrorMessageSanitizationPattern();
try {
if( filter == null ) {
filter = createFilter( servletConfig );
}
filterConfig = new FilterConfigAdapter( servletConfig );
if( filter != null ) {
filter.init( filterConfig );
}
} catch( ServletException | RuntimeException e ) {
throw logAndSanitizeException(e);
}
}
// Compilation fails: java: unreported exception java.lang.Exception; must be caught or declared to be thrown
private <T extends Exception> T logAndSanitizeException(Exception e) {
LOG.failedToExecuteFilter(e);
return (T) sanitizeException(e);
}
// Compiles but tests fail. IntelliJ warns us that we don't have to declare that we throw T
private <T extends Exception> T logAndSanitizeException(Exception e) throws T {
LOG.failedToExecuteFilter(e);
return (T) sanitizeException(e);
}
// this works
private <T extends Exception> T logAndSanitizeException(Exception e) throws T {
LOG.failedToExecuteFilter(e);
throw (T) sanitizeException(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment