Search in sources :

Example 26 with MolgenisValidationException

use of org.molgenis.data.validation.MolgenisValidationException in project molgenis by molgenis.

the class PostgreSqlExceptionTranslatorTest method translateInvalidIntegerExceptionDateTime.

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

Example 27 with MolgenisValidationException

use of org.molgenis.data.validation.MolgenisValidationException in project molgenis by molgenis.

the class PostgreSqlRepositoryTest method testMrefValueTooLong.

@Test(expectedExceptions = MolgenisValidationException.class, expectedExceptionsMessageRegExp = "One of the mref values in entity type \\[test_entity\\] attribute \\[mref_attr\\] is too long.")
public void testMrefValueTooLong() {
    Attribute attr = mock(Attribute.class);
    when(attr.getName()).thenReturn("mref_attr");
    Attribute idAttr = mock(Attribute.class);
    when(idAttr.getName()).thenReturn("id");
    when(entityType.getIdAttribute()).thenReturn(idAttr);
    when(entityType.getId()).thenReturn("test_entity");
    MolgenisValidationException mve = new MolgenisValidationException(new ConstraintViolation(VALUE_TOO_LONG_MSG));
    when(jdbcTemplate.batchUpdate(any(), any(BatchPreparedStatementSetter.class))).thenThrow(mve);
    HashMap<String, Object> value = newHashMap();
    value.put("mref_attr", "TOOLONG");
    postgreSqlRepo.addMrefs(newArrayList(value), attr);
}
Also used : Attribute(org.molgenis.data.meta.model.Attribute) ConstraintViolation(org.molgenis.data.validation.ConstraintViolation) BatchPreparedStatementSetter(org.springframework.jdbc.core.BatchPreparedStatementSetter) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) Test(org.testng.annotations.Test)

Example 28 with MolgenisValidationException

use of org.molgenis.data.validation.MolgenisValidationException 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 29 with MolgenisValidationException

use of org.molgenis.data.validation.MolgenisValidationException 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 30 with MolgenisValidationException

use of org.molgenis.data.validation.MolgenisValidationException 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)

Aggregations

MolgenisValidationException (org.molgenis.data.validation.MolgenisValidationException)43 ServerErrorMessage (org.postgresql.util.ServerErrorMessage)31 Test (org.testng.annotations.Test)25 PSQLException (org.postgresql.util.PSQLException)24 ConstraintViolation (org.molgenis.data.validation.ConstraintViolation)19 Matcher (java.util.regex.Matcher)6 Attribute (org.molgenis.data.meta.model.Attribute)4 String.format (java.lang.String.format)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 MolgenisDataException (org.molgenis.data.MolgenisDataException)2 RepositoryCollection (org.molgenis.data.RepositoryCollection)2 AttributeType (org.molgenis.data.meta.AttributeType)2 EntityType (org.molgenis.data.meta.model.EntityType)2 EntityTypeDescription (org.molgenis.data.postgresql.identifier.EntityTypeDescription)2 Component (org.springframework.stereotype.Component)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 Multimap (com.google.common.collect.Multimap)1 File (java.io.File)1