Search in sources :

Example 1 with ValueReferencedException

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);
    }
}
Also used : UnknownValueReferenceException(org.molgenis.data.UnknownValueReferenceException) Matcher(java.util.regex.Matcher) ServerErrorMessage(org.postgresql.util.ServerErrorMessage) ValueReferencedException(org.molgenis.data.ValueReferencedException)

Example 2 with ValueReferencedException

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);
}
Also used : ServerErrorMessage(org.postgresql.util.ServerErrorMessage) PSQLException(org.postgresql.util.PSQLException) ValueReferencedException(org.molgenis.data.ValueReferencedException) ValueRequiredException(org.molgenis.data.ValueRequiredException) UnknownEnumValueException(org.molgenis.data.UnknownEnumValueException) PSQLException(org.postgresql.util.PSQLException) ValueReferencedException(org.molgenis.data.ValueReferencedException) UnknownValueReferenceException(org.molgenis.data.UnknownValueReferenceException) InvalidValueTypeException(org.molgenis.data.InvalidValueTypeException) ValueAlreadyExistsException(org.molgenis.data.ValueAlreadyExistsException) DuplicateValueException(org.molgenis.data.DuplicateValueException) ValueLengthExceededException(org.molgenis.data.ValueLengthExceededException) ListValueAlreadyExistsException(org.molgenis.data.ListValueAlreadyExistsException) EntityTypeReferencedException(org.molgenis.data.EntityTypeReferencedException) ReadonlyValueException(org.molgenis.data.ReadonlyValueException) Test(org.junit.jupiter.api.Test)

Aggregations

UnknownValueReferenceException (org.molgenis.data.UnknownValueReferenceException)2 ValueReferencedException (org.molgenis.data.ValueReferencedException)2 ServerErrorMessage (org.postgresql.util.ServerErrorMessage)2 Matcher (java.util.regex.Matcher)1 Test (org.junit.jupiter.api.Test)1 DuplicateValueException (org.molgenis.data.DuplicateValueException)1 EntityTypeReferencedException (org.molgenis.data.EntityTypeReferencedException)1 InvalidValueTypeException (org.molgenis.data.InvalidValueTypeException)1 ListValueAlreadyExistsException (org.molgenis.data.ListValueAlreadyExistsException)1 ReadonlyValueException (org.molgenis.data.ReadonlyValueException)1 UnknownEnumValueException (org.molgenis.data.UnknownEnumValueException)1 ValueAlreadyExistsException (org.molgenis.data.ValueAlreadyExistsException)1 ValueLengthExceededException (org.molgenis.data.ValueLengthExceededException)1 ValueRequiredException (org.molgenis.data.ValueRequiredException)1 PSQLException (org.postgresql.util.PSQLException)1