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].");
}
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);
}
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));
}
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));
}
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));
}
Aggregations