Search in sources :

Example 1 with DuplicateRecordValidationError

use of org.summerb.approaches.validation.errors.DuplicateRecordValidationError in project summerb by skarpushin.

the class EasyCrudDaoMySqlImpl method processDuplicateEntryException.

private void processDuplicateEntryException(Throwable t) throws FieldValidationException {
    DuplicateKeyException dke = ExceptionUtils.findExceptionOfType(t, DuplicateKeyException.class);
    if (dke == null) {
        return;
    }
    String constraint = DaoExceptionUtils.findViolatedConstraintName(dke);
    // Handle case when uuid is duplicated
    if (DaoExceptionUtils.MYSQL_CONSTRAINT_PRIMARY.equals(constraint)) {
        throw new IllegalArgumentException("Row with same primary key already exists", dke);
    }
    if (!constraint.contains(DaoExceptionUtils.MYSQL_CONSTRAINT_UNIQUE)) {
        throw new IllegalArgumentException("Constraint violation " + constraint, dke);
    }
    String fieldName = constraint.substring(0, constraint.indexOf(DaoExceptionUtils.MYSQL_CONSTRAINT_UNIQUE));
    if (fieldName.contains("_")) {
        fieldName = JdbcUtils.convertUnderscoreNameToPropertyName(fieldName);
    }
    throw new FieldValidationException(new DuplicateRecordValidationError(fieldName));
}
Also used : FieldValidationException(org.summerb.approaches.validation.FieldValidationException) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) DuplicateRecordValidationError(org.summerb.approaches.validation.errors.DuplicateRecordValidationError)

Aggregations

DuplicateKeyException (org.springframework.dao.DuplicateKeyException)1 FieldValidationException (org.summerb.approaches.validation.FieldValidationException)1 DuplicateRecordValidationError (org.summerb.approaches.validation.errors.DuplicateRecordValidationError)1