use of org.molgenis.data.ValueReferencedException in project molgenis by molgenis.
the class PostgreSqlExceptionTranslator method translateForeignKeyViolation.
/**
* Package private for testability
*/
ErrorCodedDataAccessException translateForeignKeyViolation(Throwable sourceThrowable, 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_EXC_MSG, pSqlException);
throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException);
}
String colName = m.group(1);
if (!m.find()) {
LOG.error(ERROR_TRANSLATING_POSTGRES_EXC_MSG, pSqlException);
throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException);
}
String value = m.group(1);
String entityTypeId = tryGetEntityTypeName(tableName).orElse(null);
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".
String refTableName = getRefTableFromForeignKeyPsqlException(pSqlException);
String attrName = tryGetAttributeName(refTableName, colName).orElse(null);
return new ValueReferencedException(entityTypeId, attrName, value, sourceThrowable);
} else {
// ERROR: insert or update on table "x" violates foreign key constraint "y"
// Detail: Key (k)=(v) is not present in table "z".
String attrName = tryGetAttributeName(tableName, colName).orElse(null);
return new UnknownValueReferenceException(entityTypeId, attrName, value, sourceThrowable);
}
}
use of org.molgenis.data.ValueReferencedException in project molgenis by molgenis.
the class PostgreSqlExceptionTranslatorTest method translateForeignKeyViolationStillReferenced.
@Test
void translateForeignKeyViolationStillReferenced() {
ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
when(serverErrorMessage.getSQLState()).thenReturn("23503");
when(serverErrorMessage.getTable()).thenReturn("myTable");
when(serverErrorMessage.getMessage()).thenReturn("update or delete on table \"myDependentTable\" violates foreign key constraint \"myTable_myAttr_fkey\" on table \"myTable\"");
when(serverErrorMessage.getDetail()).thenReturn("Key (myColumn)=(myValue) is still referenced from table \"myTable\"");
// noinspection ThrowableResultOfMethodCallIgnored
Exception e = postgreSqlExceptionTranslator.translateForeignKeyViolation(mock(Throwable.class), new PSQLException(serverErrorMessage));
assertEquals("entityTypeId:myEntity attributeName:myAttr value:myValue", e.getMessage());
assertTrue(e instanceof ValueReferencedException);
}
Aggregations