@Test void translateForeignKeyViolationNotPresent() { ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class); when(serverErrorMessage.getSQLState()).thenReturn("23503"); when(serverErrorMessage.getTable()).thenReturn("myTable"); when(serverErrorMessage.getDetail()) .thenReturn("Key (myColumn)=(myValue) is not present in table \"myTable\""); Exception e = postgreSqlExceptionTranslator.translateForeignKeyViolation( mock(Throwable.class), new PSQLException(serverErrorMessage)); assertEquals("entityTypeId:myEntity attributeName:myAttr value:myValue", e.getMessage()); assertTrue(e instanceof UnknownValueReferenceException); }
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")) { String refTableName = getRefTableFromForeignKeyPsqlException(pSqlException); String attrName = tryGetAttributeName(refTableName, colName).orElse(null); return new ValueReferencedException(entityTypeId, attrName, value, sourceThrowable); } else { String attrName = tryGetAttributeName(tableName, colName).orElse(null); return new UnknownValueReferenceException(entityTypeId, attrName, value, sourceThrowable); } }
PostgreSqlExceptionTranslator extends SQLErrorCodeSQLExceptionTranslator implements TransactionExceptionTranslator { 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")) { String refTableName = getRefTableFromForeignKeyPsqlException(pSqlException); String attrName = tryGetAttributeName(refTableName, colName).orElse(null); return new ValueReferencedException(entityTypeId, attrName, value, sourceThrowable); } else { String attrName = tryGetAttributeName(tableName, colName).orElse(null); return new UnknownValueReferenceException(entityTypeId, attrName, value, sourceThrowable); } } }
PostgreSqlExceptionTranslator extends SQLErrorCodeSQLExceptionTranslator implements TransactionExceptionTranslator { 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")) { String refTableName = getRefTableFromForeignKeyPsqlException(pSqlException); String attrName = tryGetAttributeName(refTableName, colName).orElse(null); return new ValueReferencedException(entityTypeId, attrName, value, sourceThrowable); } else { String attrName = tryGetAttributeName(tableName, colName).orElse(null); return new UnknownValueReferenceException(entityTypeId, attrName, value, sourceThrowable); } } PostgreSqlExceptionTranslator(DataSource dataSource, EntityTypeRegistry entityTypeRegistry); }
PostgreSqlExceptionTranslator extends SQLErrorCodeSQLExceptionTranslator implements TransactionExceptionTranslator { 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")) { String refTableName = getRefTableFromForeignKeyPsqlException(pSqlException); String attrName = tryGetAttributeName(refTableName, colName).orElse(null); return new ValueReferencedException(entityTypeId, attrName, value, sourceThrowable); } else { String attrName = tryGetAttributeName(tableName, colName).orElse(null); return new UnknownValueReferenceException(entityTypeId, attrName, value, sourceThrowable); } } PostgreSqlExceptionTranslator(DataSource dataSource, EntityTypeRegistry entityTypeRegistry); @Override @Nullable @CheckForNull DataAccessException doTranslate(TransactionException transactionException); }
PostgreSqlExceptionTranslator extends SQLErrorCodeSQLExceptionTranslator implements TransactionExceptionTranslator { 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")) { String refTableName = getRefTableFromForeignKeyPsqlException(pSqlException); String attrName = tryGetAttributeName(refTableName, colName).orElse(null); return new ValueReferencedException(entityTypeId, attrName, value, sourceThrowable); } else { String attrName = tryGetAttributeName(tableName, colName).orElse(null); return new UnknownValueReferenceException(entityTypeId, attrName, value, sourceThrowable); } } PostgreSqlExceptionTranslator(DataSource dataSource, EntityTypeRegistry entityTypeRegistry); @Override @Nullable @CheckForNull DataAccessException doTranslate(TransactionException transactionException);  }