use of org.summerb.validation.errors.DuplicateRecordValidationError in project summerb by skarpushin.
the class DaoExceptionToFveTranslatorMySqlImpl method throwIfDuplicate.
private void throwIfDuplicate(Throwable t) throws FieldValidationException {
DuplicateKeyException dke = ExceptionUtils.findExceptionOfType(t, DuplicateKeyException.class);
if (dke == null) {
return;
}
String constraint = findViolatedConstraintName(dke);
if (constraint == null) {
// should log it
return;
}
// Handle case when uuid is duplicated
if (DaoExceptionToFveTranslatorMySqlImpl.MYSQL_CONSTRAINT_PRIMARY.equals(constraint)) {
throw new FieldValidationException(new DuplicateRecordValidationError(HasId.FN_ID));
}
if (!constraint.contains(DaoExceptionToFveTranslatorMySqlImpl.MYSQL_CONSTRAINT_UNIQUE)) {
throw new IllegalArgumentException("Constraint violation " + constraint, dke);
}
String fieldName = constraint.substring(0, constraint.indexOf(DaoExceptionToFveTranslatorMySqlImpl.MYSQL_CONSTRAINT_UNIQUE));
if (fieldName.contains("_")) {
fieldName = JdbcUtils.convertUnderscoreNameToPropertyName(fieldName);
}
throw new FieldValidationException(new DuplicateRecordValidationError(fieldName));
}
use of org.summerb.validation.errors.DuplicateRecordValidationError in project summerb by skarpushin.
the class DaoExceptionToFveTranslatorPostgresImpl method throwIfDuplicate.
private void throwIfDuplicate(Throwable t) throws FieldValidationException {
DuplicateKeyException dke = ExceptionUtils.findExceptionOfType(t, DuplicateKeyException.class);
if (dke == null) {
return;
}
PSQLException dkep = ExceptionUtils.findExceptionOfType(t, PSQLException.class);
if (dkep == null) {
return;
}
String detail = dkep.getServerErrorMessage().getDetail();
if (!StringUtils.hasText(detail)) {
return;
}
if (!detail.startsWith("Key (")) {
return;
}
String fieldsStr = detail.substring(5, detail.indexOf(")"));
String[] fields = fieldsStr.split(",");
ValidationContext ctx = new ValidationContext();
for (String field : fields) {
ctx.add(new DuplicateRecordValidationError(JdbcUtils.convertUnderscoreNameToPropertyName(field.trim())));
}
ctx.throwIfHasErrors();
}
Aggregations