Custom Validation Error Handling
Whether you are using the default validators or have implemented custom ones, you can customize the behavior that occurs when validation fails, as well as create custom errors.
IValidationErrorHandler
Customize the validation fail behavior through the IValidationErrorHandler
interface:
public interface IValidationErrorHandler<T extends IValidated & ILimitsManagedObject> {
/**
* Handle a validation failure (i.e. a validation error not caused by an exception)
*
* @param source the entity that failed validation
* @param action recommended action to take to resolve the error
* @param reason human-readable cause of the error
*/
void handleValidationFailure(T source, String action, String reason);
/**
* Handle a validation error (i.e. a validation error that is caused by an exception)
*
* @param source the entity that failed validation
* @param action recommended action to take to resolve the error
* @param ex the exception that caused the error
*/
void handleValidationError(T source, String action, Exception ex);
/**
* Handle a validation error
*
* @param validationError the validation error to handle
*/
void handleValidationError(IValidationError<T> validationError);
/**
* @return the list of validation errors
*/
List<IValidationError<T>> getValidationErrors();
}
This interface is extended by ILimitStructureValidationErrorHandler
, ILimitValidationErrorHandler
, and IIncidentValidationErrorHandler
.
The default implementation of IValidationErrorHandler
(shared by all 3 sub-interfaces) is DefaultValidationErrorHandler
and is generic for all types of components that are validated.
This default implementation collects all errors that occur during validation and an error report is printed in the logs from the postValidation()
method
of the validator. If throwException
is true
when reset()
is called, a LimitsDefinitionalValidationRuntimeException
is thrown. The exception is
returned as a ProblemDetail
object that contains all the validation errors in its validationErrors
property, including the line number and column index for each error
if the source is a file.
To implement a custom Limit error handler that logs all errors as they occur and continues processing, you can do the following:
1. Create the Spring Bean
@Component
@Slf4j
public class MyLimitValidationErrorHandler implements ILimitValidationErrorHandler {
private final List<IValidationError<Limit>> validationErrors = new ArrayList<>();
@Override
public void handleValidationFailure(Limit source, String action, String reason) {
validationErrors.add(new ValidationError<>(source, reason, action, null));
log.error("Validation failed for " + source + ". Reason: " + reason);
}
@Override
public void handleValidationError(Limit source, String action, Exception ex) {
validationErrors.add(new ValidationError<>(source, ex.getMessage(), action, ex));
log.error("Validation error for " + source, ex);
}
@Override
public void handleValidationError(IValidationError<Limit> validationError) {
validationErrors.add(validationError);
log.error("Validation error for " + validationError.getSource() + ". Reason: " + validationError.getReason());
}
@Override
public List<IValidationError<Limit>> getValidationErrors() {
return validationErrors;
}
}
2. Import the Spring Bean
Once the bean is created, you need to import it to the project. Once done, Spring will use the custom bean for handling validation errors.
IValidationError
In addition to customizing the behavior when a validation error occurs, you can also customize the validation errors themselves.
The IValidationError
interface allows you to define a custom validation error:
public interface IValidationError<T> {
/**
* @return the DTO object that was the source of the error
*/
T getSource();
/**
* @return the human-readable cause of the error
*/
String getReason();
/**
* @return the human-readable action to take to resolve the error
*/
String getAction();
/**
* @return the underlying exception that caused the error
*/
Throwable getException();
/**
* @return the line number in the source file where the error occurred
*/
int getLineNumber();
/**
* @return the column index in the source file where the error occurred
*/
int getColumnIndex();
}
The default implementation of IValidationError
is DefaultValidationError
. This default is generic for all types of components that
are validated, and uses Lombok’s @AllArgsConstructor
and @Getter
annotations to provide its functionality.
To create a custom validation error specific to incidents that had some structured formatting and default action, you can do the following:
public class MyIncidentValidationError implements IValidationError<Incident> {
public static final String REASON_TEMPLATE = "Validation failed for Incident [%s] due to: %s";
public static final String DEFAULT_ACTION = "Please check the incident details and try again.";
protected Incident source;
protected String action;
protected String reason;
protected Throwable exception;
protected int lineNumber;
protected int columnIndex;
@Override
public Incident getSource() {
return source;
}
@Override
public String getReason() {
return String.format(REASON_TEMPLATE, source, reason);
}
@Override
public String getAction() {
return action == null ? DEFAULT_ACTION : action;
}
@Override
public Throwable getException() {
return exception;
}
@Override
public int getLineNumber() {
return lineNumber;
}
@Override
public int getColumnIndex() {
return columnIndex;
}
}
note
Note that IValidationError
is not managed by Spring, so there is no need specify @Component
or @Primary
annotations.
You can create as many custom implementations of IValidationError
as you need.