Error handling should be designed to provide clear, actionable information to developers while avoiding redundancy. Follow these practices:
// BAD:
log.errorf("Error unzipping import file %s: %s", fileName, e.getMessage());
throw new IllegalStateException(String.format("Error unzipping import file %s: %s",
fileName, e.getMessage()), e);
// GOOD:
throw new IllegalStateException(String.format("Error unzipping import file %s: %s",
fileName, e.getMessage()), e);
// VAGUE:
throw new DeploymentException(String.format("Class %s has no fields.", className));
// BETTER:
throw new DeploymentException(String.format("Class %s has no fields. Parameters containers are only supported if they have at least one annotated field.", className));
try {
options.setUseAlpn(sslOptions.isUseAlpn());
} catch (UnsupportedOperationException e) {
log.warn("ALPN configuration not supported by implementation: %s. ALPN setting will be ignored."
.formatted(options.getClass().getName()));
}
// NOT IDEAL:
if (hasBasic && hasApiKey) {
LOG.warn("Multiple authentication methods configured. Defaulting to Basic Authentication.");
return EsAuth.BASIC;
}
// BETTER:
if (hasBasic && hasApiKey) {
throw new IllegalArgumentException("Multiple authentication methods configured. Please specify only one authentication method.");
}
Enter the URL of a public GitHub repository