Search in sources :

Example 1 with DuplicateRecordValidationError

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));
}
Also used : FieldValidationException(org.summerb.validation.FieldValidationException) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) DuplicateRecordValidationError(org.summerb.validation.errors.DuplicateRecordValidationError)

Example 2 with DuplicateRecordValidationError

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();
}
Also used : PSQLException(org.postgresql.util.PSQLException) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) ValidationContext(org.summerb.validation.ValidationContext) DuplicateRecordValidationError(org.summerb.validation.errors.DuplicateRecordValidationError)

Aggregations

DuplicateKeyException (org.springframework.dao.DuplicateKeyException)2 DuplicateRecordValidationError (org.summerb.validation.errors.DuplicateRecordValidationError)2 PSQLException (org.postgresql.util.PSQLException)1 FieldValidationException (org.summerb.validation.FieldValidationException)1 ValidationContext (org.summerb.validation.ValidationContext)1