Show summary of validation errors
This way the stack trace will have more information when a ValidationException happens. FEA: ItEr71S04BugFixing
This commit is contained in:
parent
9e93533ade
commit
4a0eb44b41
2 changed files with 41 additions and 4 deletions
|
|
@ -111,8 +111,7 @@ public abstract class BaseEntity implements INewObject {
|
|||
@Override
|
||||
public String toString() {
|
||||
try {
|
||||
return super.toString() + "[ id: " + getId() + ", newObject: "
|
||||
+ isNewObject() + "]";
|
||||
return super.toString() + getExtraInformation();
|
||||
} catch (Exception e) {
|
||||
final String message = "error doing toString";
|
||||
LOG.error(message, e);
|
||||
|
|
@ -120,4 +119,9 @@ public abstract class BaseEntity implements INewObject {
|
|||
}
|
||||
}
|
||||
|
||||
public String getExtraInformation() {
|
||||
return "[ id: " + getId() + ", newObject: "
|
||||
+ isNewObject() + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ package org.navalplanner.business.common.exceptions;
|
|||
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.hibernate.validator.InvalidValue;
|
||||
import org.navalplanner.business.common.BaseEntity;
|
||||
|
||||
/**
|
||||
* Encapsulates some validation failure <br />
|
||||
|
|
@ -30,6 +31,38 @@ import org.hibernate.validator.InvalidValue;
|
|||
*/
|
||||
public class ValidationException extends RuntimeException {
|
||||
|
||||
private static String getValidationErrorSummary(
|
||||
InvalidValue... invalidValues) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (InvalidValue each : invalidValues) {
|
||||
builder.append(summaryFor(each));
|
||||
builder.append("; ");
|
||||
}
|
||||
if (invalidValues.length > 0) {
|
||||
builder.delete(builder.length() - 2, builder.length());
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private static String summaryFor(InvalidValue invalidValue) {
|
||||
return "at " + asString(invalidValue.getBean()) + " "
|
||||
+ invalidValue.getPropertyPath() + ": "
|
||||
+ invalidValue.getMessage();
|
||||
}
|
||||
|
||||
private static String asString(Object bean) {
|
||||
if (bean == null) {
|
||||
// this shouldn't happen, just in case
|
||||
return "null";
|
||||
}
|
||||
if (bean instanceof BaseEntity) {
|
||||
BaseEntity entity = (BaseEntity) bean;
|
||||
return bean.getClass().getSimpleName() + " "
|
||||
+ entity.getExtraInformation();
|
||||
}
|
||||
return bean.toString();
|
||||
}
|
||||
|
||||
private InvalidValue[] invalidValues;
|
||||
|
||||
public InvalidValue[] getInvalidValues() {
|
||||
|
|
@ -37,7 +70,7 @@ public class ValidationException extends RuntimeException {
|
|||
}
|
||||
|
||||
public ValidationException(InvalidValue invalidValue) {
|
||||
super();
|
||||
super(getValidationErrorSummary(invalidValue));
|
||||
storeInvalidValues(toArray(invalidValue));
|
||||
}
|
||||
|
||||
|
|
@ -48,7 +81,7 @@ public class ValidationException extends RuntimeException {
|
|||
}
|
||||
|
||||
public ValidationException(InvalidValue[] invalidValues) {
|
||||
super();
|
||||
super(getValidationErrorSummary(invalidValues));
|
||||
storeInvalidValues(invalidValues);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue