From 31d4b57239bfd3b66cd1c4bb0e34052cc8afd8a6 Mon Sep 17 00:00:00 2001 From: Mark Norkin Date: Fri, 26 Jan 2018 12:21:55 +0200 Subject: [PATCH] SPR-16372 Provide access to the actual exception that triggered the binding error --- .../DefaultBindingErrorProcessor.java | 2 +- .../validation/FieldError.java | 34 +++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/validation/DefaultBindingErrorProcessor.java b/spring-context/src/main/java/org/springframework/validation/DefaultBindingErrorProcessor.java index de823582d4d8..9f1ece684888 100644 --- a/spring-context/src/main/java/org/springframework/validation/DefaultBindingErrorProcessor.java +++ b/spring-context/src/main/java/org/springframework/validation/DefaultBindingErrorProcessor.java @@ -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)); } /** diff --git a/spring-context/src/main/java/org/springframework/validation/FieldError.java b/spring-context/src/main/java/org/springframework/validation/FieldError.java index c72529a0f48f..8ec37c6b4e2d 100644 --- a/spring-context/src/main/java/org/springframework/validation/FieldError.java +++ b/spring-context/src/main/java/org/springframework/validation/FieldError.java @@ -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. @@ -29,6 +29,7 @@ * * @author Rod Johnson * @author Juergen Hoeller + * @author Mark Norkin * @since 10.03.2003 * @see DefaultMessageCodesResolver */ @@ -42,6 +43,9 @@ public class FieldError extends ObjectError { private final boolean bindingFailure; + @Nullable + private final Exception cause; + /** * Create a new FieldError instance. @@ -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; } @@ -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() { @@ -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 @@ -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; }