Search in sources :

Example 46 with PSQLException

use of org.postgresql.util.PSQLException in project midpoint by Evolveum.

the class AddObjectContext method execute.

/**
 * Inserts the object provided to the constructor and returns its OID.
 */
public String execute() throws SchemaException, ObjectAlreadyExistsException {
    try (JdbcSession jdbcSession = repositoryContext.newJdbcSession().startTransaction()) {
        String oid = execute(jdbcSession);
        jdbcSession.commit();
        return oid;
    } catch (QueryException e) {
        // Querydsl exception, not ours
        Throwable cause = e.getCause();
        if (cause instanceof PSQLException) {
            SqaleUtils.handlePostgresException((PSQLException) cause);
        }
        throw e;
    }
}
Also used : JdbcSession(com.evolveum.midpoint.repo.sqlbase.JdbcSession) QueryException(com.querydsl.core.QueryException) PSQLException(org.postgresql.util.PSQLException)

Example 47 with PSQLException

use of org.postgresql.util.PSQLException 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

PSQLException (org.postgresql.util.PSQLException)47 ServerErrorMessage (org.postgresql.util.ServerErrorMessage)28 Test (org.testng.annotations.Test)26 MolgenisValidationException (org.molgenis.data.validation.MolgenisValidationException)24 Test (org.junit.Test)6 SQLException (java.sql.SQLException)4 AbstractGriffinTest (io.questdb.griffin.AbstractGriffinTest)3 Connection (java.sql.Connection)3 MolgenisDataException (org.molgenis.data.MolgenisDataException)3 IOException (java.io.IOException)2 BatchUpdateException (java.sql.BatchUpdateException)2 Properties (java.util.Properties)2 BaseConnection (org.postgresql.core.BaseConnection)2 PGExceptionSorter (com.alibaba.druid.pool.vendor.PGExceptionSorter)1 JdbcSession (com.evolveum.midpoint.repo.sqlbase.JdbcSession)1 ObjectAlreadyExistsException (com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)1 QueryException (com.querydsl.core.QueryException)1 BadRequestException (com.sequenceiq.cloudbreak.controller.BadRequestException)1 StringSink (io.questdb.std.str.StringSink)1 String.format (java.lang.String.format)1