Skip to content

SPR-16372 The FieldError class does not provide access to the exception that triggered the binding error. #1654

New issue

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void processPropertyAccessException(PropertyAccessException ex, BindingRe
}
bindingResult.addError(new FieldError(
bindingResult.getObjectName(), field, rejectedValue, true,
codes, arguments, ex.getLocalizedMessage()));
codes, arguments, ex.getLocalizedMessage(), ex));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,7 @@
*
* @author Rod Johnson
* @author Juergen Hoeller
* @author Mark Norkin
* @since 10.03.2003
* @see DefaultMessageCodesResolver
*/
Expand All @@ -42,6 +43,9 @@ public class FieldError extends ObjectError {

private final boolean bindingFailure;

@Nullable
private final Exception cause;


/**
* Create a new FieldError instance.
Expand All @@ -66,12 +70,29 @@ public FieldError(String objectName, String field, String defaultMessage) {
*/
public FieldError(String objectName, String field, @Nullable Object rejectedValue, boolean bindingFailure,
@Nullable String[] codes, @Nullable Object[] arguments, @Nullable String defaultMessage) {
this(objectName, field, rejectedValue, bindingFailure, codes, arguments, defaultMessage, null);
}

/**
* Create a new FieldError instance.
* @param objectName the name of the affected object
* @param field the affected field of the object
* @param rejectedValue the rejected field value
* @param bindingFailure whether this error represents a binding failure
* (like a type mismatch); else, it is a validation failure
* @param codes the codes to be used to resolve this message
* @param arguments the array of arguments to be used to resolve this message
* @param defaultMessage the default message to be used to resolve this message
* @param cause the cause
*/
public FieldError(String objectName, String field, @Nullable Object rejectedValue, boolean bindingFailure,
@Nullable String[] codes, @Nullable Object[] arguments, @Nullable String defaultMessage, @Nullable Exception cause) {
super(objectName, codes, arguments, defaultMessage);
Assert.notNull(field, "Field must not be null");
this.field = field;
this.rejectedValue = rejectedValue;
this.bindingFailure = bindingFailure;
this.cause = cause;
}


Expand All @@ -98,6 +119,13 @@ public boolean isBindingFailure() {
return this.bindingFailure;
}

/**
* Return the cause of the field error
*/
@Nullable
public Exception getCause() {
return this.cause;
}

@Override
public String toString() {
Expand All @@ -117,7 +145,8 @@ public boolean equals(@Nullable Object other) {
FieldError otherError = (FieldError) other;
return (getField().equals(otherError.getField()) &&
ObjectUtils.nullSafeEquals(getRejectedValue(), otherError.getRejectedValue()) &&
isBindingFailure() == otherError.isBindingFailure());
isBindingFailure() == otherError.isBindingFailure()) &&
ObjectUtils.nullSafeEquals(getCause(), otherError.getCause());
}

@Override
Expand All @@ -126,6 +155,7 @@ public int hashCode() {
hashCode = 29 * hashCode + getField().hashCode();
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getRejectedValue());
hashCode = 29 * hashCode + (isBindingFailure() ? 1 : 0);
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(getCause());
return hashCode;
}

Expand Down