Search in sources :

Example 26 with ServerErrorMessage

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

the class PostgreSqlExceptionTranslator method translateForeignKeyViolation.

/**
 * Package private for testability
 *
 * @param pSqlException PostgreSQL exception
 * @return translated validation exception
 */
MolgenisValidationException translateForeignKeyViolation(PSQLException pSqlException) {
    ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage();
    String tableName = serverErrorMessage.getTable();
    String detailMessage = serverErrorMessage.getDetail();
    Matcher m = Pattern.compile("\\((.*?)\\)").matcher(detailMessage);
    if (!m.find()) {
        LOG.error("Error translating postgres exception: ", pSqlException);
        throw new RuntimeException("Error translating exception", pSqlException);
    }
    String colName = m.group(1);
    if (!m.find()) {
        LOG.error("Error translating postgres exception: ", pSqlException);
        throw new RuntimeException("Error translating exception", pSqlException);
    }
    String value = m.group(1);
    String constraintViolationMessageTemplate;
    String attrName;
    if (detailMessage.contains("still referenced from")) {
        // ERROR: update or delete on table "x" violates foreign key constraint "y" on table "z"
        // Detail: Key (k)=(v) is still referenced from table "x".
        constraintViolationMessageTemplate = "Value '%s' for attribute '%s' is referenced by entity '%s'.";
        String refTableName = getRefTableFromForeignKeyPsqlException(pSqlException);
        attrName = getAttributeName(refTableName, colName);
    } else {
        // ERROR: insert or update on table "x" violates foreign key constraint "y"
        // Detail: Key (k)=(v) is not present in table "z".
        constraintViolationMessageTemplate = "Unknown xref value '%s' for attribute '%s' of entity '%s'.";
        attrName = getAttributeName(tableName, colName);
    }
    ConstraintViolation constraintViolation = new ConstraintViolation(format(constraintViolationMessageTemplate, value, attrName, getEntityTypeName(tableName)), null);
    return new MolgenisValidationException(singleton(constraintViolation));
}
Also used : Matcher(java.util.regex.Matcher) ServerErrorMessage(org.postgresql.util.ServerErrorMessage) ConstraintViolation(org.molgenis.data.validation.ConstraintViolation) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException)

Example 27 with ServerErrorMessage

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

the class PostgreSqlExceptionTranslator method translateCheckConstraintViolation.

/**
 * Package private for testability
 *
 * @param pSqlException PostgreSQL exception
 * @return translated validation exception
 */
MolgenisValidationException translateCheckConstraintViolation(PSQLException pSqlException) {
    ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage();
    String tableName = serverErrorMessage.getTable();
    String constraintName = serverErrorMessage.getConstraint();
    // constraint name: <tableName>_<columnName>_chk
    String columnName = constraintName.substring(tableName.length() + 1, constraintName.length() - 4);
    ConstraintViolation constraintViolation = new ConstraintViolation(format("Unknown enum value for attribute '%s' of entity '%s'.", getAttributeName(tableName, columnName), getEntityTypeName(tableName)), null);
    return new MolgenisValidationException(singleton(constraintViolation));
}
Also used : ServerErrorMessage(org.postgresql.util.ServerErrorMessage) ConstraintViolation(org.molgenis.data.validation.ConstraintViolation) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException)

Example 28 with ServerErrorMessage

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

the class PostgreSqlExceptionTranslator method translateInvalidIntegerException.

/**
 * Package private for testability
 *
 * @param pSqlException PostgreSQL exception
 * @return translated validation exception
 */
static MolgenisValidationException translateInvalidIntegerException(PSQLException pSqlException) {
    ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage();
    String message = serverErrorMessage.getMessage();
    Matcher matcher = Pattern.compile("invalid input syntax for \\b(?:type )?\\b(.+?): \"(.*?)\"").matcher(message);
    boolean matches = matcher.matches();
    if (!matches) {
        throw new RuntimeException("Error translating exception", pSqlException);
    }
    String postgreSqlType = matcher.group(1);
    // convert PostgreSQL data type to attribute type:
    String type;
    switch(postgreSqlType) {
        case "boolean":
            type = BOOL.toString();
            break;
        case "date":
            type = DATE.toString();
            break;
        case "timestamp with time zone":
            type = DATE_TIME.toString();
            break;
        case "double precision":
            type = DECIMAL.toString();
            break;
        case "integer":
            type = INT.toString() + " or " + LONG.toString();
            break;
        default:
            type = postgreSqlType;
            break;
    }
    String value = matcher.group(2);
    ConstraintViolation constraintViolation = new ConstraintViolation(format("Value [%s] of this entity attribute is not of type [%s].", value, type), null);
    return new MolgenisValidationException(singleton(constraintViolation));
}
Also used : Matcher(java.util.regex.Matcher) ServerErrorMessage(org.postgresql.util.ServerErrorMessage) ConstraintViolation(org.molgenis.data.validation.ConstraintViolation) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException)

Example 29 with ServerErrorMessage

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

the class PostgreSqlExceptionTranslatorTest method translateUniqueKeyViolationKeyIsDuplicatedDoubleQuotes.

@Test
public void translateUniqueKeyViolationKeyIsDuplicatedDoubleQuotes() {
    ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
    when(serverErrorMessage.getSQLState()).thenReturn("23505");
    when(serverErrorMessage.getTable()).thenReturn("myTable");
    when(serverErrorMessage.getDetail()).thenReturn("Key (\"myColumn\")=(myValue) is duplicated.");
    // noinspection ThrowableResultOfMethodCallIgnored
    MolgenisValidationException e = postgreSqlExceptionTranslator.translateUniqueKeyViolation(new PSQLException(serverErrorMessage));
    assertEquals(e.getMessage(), "The attribute 'myAttr' of entity 'myEntity' contains duplicate value 'myValue'.");
}
Also used : ServerErrorMessage(org.postgresql.util.ServerErrorMessage) PSQLException(org.postgresql.util.PSQLException) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) Test(org.testng.annotations.Test)

Example 30 with ServerErrorMessage

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

the class PostgreSqlExceptionTranslatorTest method translateInvalidIntegerExceptionBoolean.

@Test
public void translateInvalidIntegerExceptionBoolean() {
    ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
    when(serverErrorMessage.getMessage()).thenReturn("invalid input syntax for type boolean: \"str1\"");
    // noinspection ThrowableResultOfMethodCallIgnored
    MolgenisValidationException e = PostgreSqlExceptionTranslator.translateInvalidIntegerException(new PSQLException(serverErrorMessage));
    assertEquals(e.getMessage(), "Value [str1] of this entity attribute is not of type [BOOL].");
}
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