Search in sources :

Example 6 with PSQLException

use of org.postgresql.util.PSQLException in project molgenis by molgenis.

the class PostgreSqlExceptionTranslator method doTranslate.

private MolgenisDataException doTranslate(DataAccessException dataAccessException) {
    Throwable cause = dataAccessException.getCause();
    if (!(cause instanceof PSQLException)) {
        throw new RuntimeException(format("Unexpected exception class [%s]", cause.getClass().getSimpleName()));
    }
    PSQLException pSqlException = (PSQLException) cause;
    MolgenisDataException molgenisDataException = doTranslate(pSqlException);
    if (molgenisDataException == null) {
        molgenisDataException = new MolgenisDataException(dataAccessException);
    }
    return molgenisDataException;
}
Also used : MolgenisDataException(org.molgenis.data.MolgenisDataException) PSQLException(org.postgresql.util.PSQLException)

Example 7 with PSQLException

use of org.postgresql.util.PSQLException in project molgenis by molgenis.

the class PostgreSqlExceptionTranslator method translateDependentObjectsStillExist.

/**
 * Package private for testability
 *
 * @param pSqlException PostgreSQL exception
 * @return translated validation exception
 */
MolgenisValidationException translateDependentObjectsStillExist(PSQLException pSqlException) {
    ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage();
    String detail = serverErrorMessage.getDetail();
    Matcher matcher = Pattern.compile("constraint (.+) on table \"?([^\"]+)\"? depends on table \"?([^\"]+)\"?\n?").matcher(detail);
    Map<String, Set<String>> entityTypeDependencyMap = new LinkedHashMap<>();
    while (matcher.find()) {
        String tableName = matcher.group(2);
        String dependentTableName = matcher.group(3);
        String entityTypeName = getEntityTypeName(tableName);
        String dependentEntityTypeName = getEntityTypeName(dependentTableName);
        Set<String> dependentTableNames = entityTypeDependencyMap.computeIfAbsent(dependentEntityTypeName, k -> new LinkedHashSet<>());
        dependentTableNames.add(entityTypeName);
    }
    if (// no matches
    entityTypeDependencyMap.isEmpty()) {
        LOG.error("Error translating postgres exception: ", pSqlException);
        throw new RuntimeException("Error translating exception", pSqlException);
    }
    Set<ConstraintViolation> constraintViolations = entityTypeDependencyMap.entrySet().stream().map(entry -> {
        String message;
        if (entry.getValue().size() == 1) {
            message = format("Cannot delete entity '%s' because entity '%s' depends on it.", entry.getKey(), entry.getValue().iterator().next());
        } else {
            message = format("Cannot delete entity '%s' because entities '%s' depend on it.", entry.getKey(), entry.getValue().stream().collect(joining(", ")));
        }
        return new ConstraintViolation(message, null);
    }).collect(toCollection(LinkedHashSet::new));
    return new MolgenisValidationException(constraintViolations);
}
Also used : EntityTypeDescription(org.molgenis.data.postgresql.identifier.EntityTypeDescription) DataAccessException(org.springframework.dao.DataAccessException) BatchUpdateException(java.sql.BatchUpdateException) ConstraintViolation(org.molgenis.data.validation.ConstraintViolation) ServerErrorMessage(org.postgresql.util.ServerErrorMessage) LoggerFactory(org.slf4j.LoggerFactory) PSQLException(org.postgresql.util.PSQLException) LinkedHashMap(java.util.LinkedHashMap) Collectors.toCollection(java.util.stream.Collectors.toCollection) EntityTypeRegistry(org.molgenis.data.postgresql.identifier.EntityTypeRegistry) SQLException(java.sql.SQLException) Matcher(java.util.regex.Matcher) Collections.singleton(java.util.Collections.singleton) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) DataSource(javax.sql.DataSource) LinkedHashSet(java.util.LinkedHashSet) SQLErrorCodeSQLExceptionTranslator(org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator) TransactionExceptionTranslator(org.molgenis.data.transaction.TransactionExceptionTranslator) AttributeType(org.molgenis.data.meta.AttributeType) Logger(org.slf4j.Logger) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) Set(java.util.Set) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) Component(org.springframework.stereotype.Component) TransactionException(org.springframework.transaction.TransactionException) Pattern(java.util.regex.Pattern) MolgenisDataException(org.molgenis.data.MolgenisDataException) AttributeDescription(org.molgenis.data.postgresql.identifier.AttributeDescription) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) Matcher(java.util.regex.Matcher) ServerErrorMessage(org.postgresql.util.ServerErrorMessage) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) LinkedHashMap(java.util.LinkedHashMap) ConstraintViolation(org.molgenis.data.validation.ConstraintViolation)

Example 8 with PSQLException

use of org.postgresql.util.PSQLException in project molgenis by molgenis.

the class PostgreSqlExceptionTranslatorTest method translateForeignKeyViolation.

@Test
public void translateForeignKeyViolation() {
    ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
    when(serverErrorMessage.getSQLState()).thenReturn("23503");
    when(serverErrorMessage.getTable()).thenReturn("myTable");
    when(serverErrorMessage.getDetail()).thenReturn("... (myColumn) ... (myValue) ...");
    // noinspection ThrowableResultOfMethodCallIgnored
    MolgenisValidationException e = postgreSqlExceptionTranslator.translateForeignKeyViolation(new PSQLException(serverErrorMessage));
    assertEquals(e.getMessage(), "Unknown xref value 'myValue' for attribute 'myAttr' of entity 'myEntity'.");
}
Also used : ServerErrorMessage(org.postgresql.util.ServerErrorMessage) PSQLException(org.postgresql.util.PSQLException) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) Test(org.testng.annotations.Test)

Example 9 with PSQLException

use of org.postgresql.util.PSQLException in project molgenis by molgenis.

the class PostgreSqlExceptionTranslatorTest method translateReadonlyViolation.

@Test
public void translateReadonlyViolation() {
    ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
    when(serverErrorMessage.getMessage()).thenReturn("Updating read-only column \"myColumn\" of table \"myTable\" with id [abc] is not allowed");
    // noinspection ThrowableResultOfMethodCallIgnored
    MolgenisValidationException e = postgreSqlExceptionTranslator.translateReadonlyViolation(new PSQLException(serverErrorMessage));
    assertEquals(e.getMessage(), "Updating read-only attribute 'myAttr' of entity 'myEntity' with id 'abc' is not allowed.");
}
Also used : ServerErrorMessage(org.postgresql.util.ServerErrorMessage) PSQLException(org.postgresql.util.PSQLException) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) Test(org.testng.annotations.Test)

Example 10 with PSQLException

use of org.postgresql.util.PSQLException in project molgenis by molgenis.

the class PostgreSqlExceptionTranslatorTest method translateNotNullViolation.

@Test
public void translateNotNullViolation() {
    ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
    when(serverErrorMessage.getSQLState()).thenReturn("23502");
    when(serverErrorMessage.getTable()).thenReturn("myTable");
    when(serverErrorMessage.getMessage()).thenReturn("null value in column \"myColumn\" violates not-null constraint");
    // noinspection ThrowableResultOfMethodCallIgnored
    MolgenisValidationException e = postgreSqlExceptionTranslator.translateNotNullViolation(new PSQLException(serverErrorMessage));
    assertEquals(e.getMessage(), "The attribute 'myAttr' of entity 'myEntity' can not be null.");
}
Also used : ServerErrorMessage(org.postgresql.util.ServerErrorMessage) PSQLException(org.postgresql.util.PSQLException) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) Test(org.testng.annotations.Test)

Aggregations

PSQLException (org.postgresql.util.PSQLException)36 ServerErrorMessage (org.postgresql.util.ServerErrorMessage)28 Test (org.testng.annotations.Test)26 MolgenisValidationException (org.molgenis.data.validation.MolgenisValidationException)24 SQLException (java.sql.SQLException)3 MolgenisDataException (org.molgenis.data.MolgenisDataException)3 BatchUpdateException (java.sql.BatchUpdateException)2 PGExceptionSorter (com.alibaba.druid.pool.vendor.PGExceptionSorter)1 BadRequestException (com.sequenceiq.cloudbreak.controller.BadRequestException)1 IOException (java.io.IOException)1 String.format (java.lang.String.format)1 ConnectException (java.net.ConnectException)1 ByteBuffer (java.nio.ByteBuffer)1 PreparedStatement (java.sql.PreparedStatement)1 SQLWarning (java.sql.SQLWarning)1 ArrayList (java.util.ArrayList)1 Collections.singleton (java.util.Collections.singleton)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1