Search in sources :

Example 6 with ServerErrorMessage

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

use of org.postgresql.util.ServerErrorMessage 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 8 with ServerErrorMessage

use of org.postgresql.util.ServerErrorMessage 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 9 with ServerErrorMessage

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

Example 10 with ServerErrorMessage

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

the class PostgreSqlExceptionTranslatorTest method translateInvalidIntegerExceptionDate.

@Test
public void translateInvalidIntegerExceptionDate() {
    ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
    when(serverErrorMessage.getMessage()).thenReturn("invalid input syntax for type date: \"str1\"");
    // noinspection ThrowableResultOfMethodCallIgnored
    MolgenisValidationException e = PostgreSqlExceptionTranslator.translateInvalidIntegerException(new PSQLException(serverErrorMessage));
    assertEquals(e.getMessage(), "Value [str1] of this entity attribute is not of type [DATE].");
}
Also used : ServerErrorMessage(org.postgresql.util.ServerErrorMessage) PSQLException(org.postgresql.util.PSQLException) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) Test(org.testng.annotations.Test)

Aggregations

ServerErrorMessage (org.postgresql.util.ServerErrorMessage)36 MolgenisValidationException (org.molgenis.data.validation.MolgenisValidationException)31 PSQLException (org.postgresql.util.PSQLException)28 Test (org.testng.annotations.Test)27 ConstraintViolation (org.molgenis.data.validation.ConstraintViolation)7 Matcher (java.util.regex.Matcher)6 EntityTypeDescription (org.molgenis.data.postgresql.identifier.EntityTypeDescription)2 String.format (java.lang.String.format)1 BatchUpdateException (java.sql.BatchUpdateException)1 SQLException (java.sql.SQLException)1 Collections.singleton (java.util.Collections.singleton)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Set (java.util.Set)1 Pattern (java.util.regex.Pattern)1 Collectors.joining (java.util.stream.Collectors.joining)1 Collectors.toCollection (java.util.stream.Collectors.toCollection)1 DataSource (javax.sql.DataSource)1