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