use of org.postgresql.util.PSQLException in project molgenis by molgenis.
the class PostgreSqlExceptionTranslator method doTranslate.
private MolgenisDataException doTranslate(DataAccessException dataAccessException) {
Throwable cause = dataAccessException.getCause();
if (!(cause instanceof PSQLException)) {
throw new RuntimeException(format("Unexpected exception class [%s]", cause.getClass().getSimpleName()));
}
PSQLException pSqlException = (PSQLException) cause;
MolgenisDataException molgenisDataException = doTranslate(pSqlException);
if (molgenisDataException == null) {
molgenisDataException = new MolgenisDataException(dataAccessException);
}
return molgenisDataException;
}
use of org.postgresql.util.PSQLException 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.PSQLException 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.PSQLException 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.PSQLException 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.");
}
Aggregations