@Test void translateUniqueKeyViolationCompositeKey() { ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class); when(serverErrorMessage.getSQLState()).thenReturn("23505"); when(serverErrorMessage.getTable()).thenReturn("myTable"); when(serverErrorMessage.getDetail()) .thenReturn("Key (myIdColumn, myColumn)=(myIdValue, myValue) already exists."); Exception e = postgreSqlExceptionTranslator.translateUniqueKeyViolation( mock(Throwable.class), new PSQLException(serverErrorMessage)); assertEquals( "entityTypeId:myEntity attributeName:myAttr entityId:myIdValue value:myValue", e.getMessage()); assertTrue(e instanceof ListValueAlreadyExistsException); }
ErrorCodedDataAccessException translateUniqueKeyViolation( Throwable sourceThrowable, PSQLException pSqlException) { ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage(); String tableName = serverErrorMessage.getTable(); String detailMessage = serverErrorMessage.getDetail(); Matcher matcher = Pattern.compile("Key \\(\"?(.*?)\"?\\)=\\((.*?)\\) already exists.").matcher(detailMessage); boolean matches = matcher.matches(); if (matches) { String[] columnNames = matcher.group(1).split(", "); if (columnNames.length == 1) { String columnName = columnNames[0]; String value = matcher.group(2); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); return new ValueAlreadyExistsException(entityTypeId, attributeName, value, sourceThrowable); } else { String columnName = columnNames[columnNames.length - 1]; String[] values = matcher.group(2).split(", "); String entityId = values[0]; String value = values[1]; String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); return new ListValueAlreadyExistsException( entityTypeId, attributeName, entityId, value, sourceThrowable); } } else { matcher = Pattern.compile("Key \\(\"?(.*?)\"?\\)=\\((.*?)\\) is duplicated.") .matcher(detailMessage); matches = matcher.matches(); if (matches) { String columnName = matcher.group(1); String value = matcher.group(2); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); return new DuplicateValueException(entityTypeId, attributeName, value, sourceThrowable); } else { LOG.error(ERROR_TRANSLATING_POSTGRES_EXC_MSG, pSqlException); throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } } }
PostgreSqlExceptionTranslator extends SQLErrorCodeSQLExceptionTranslator implements TransactionExceptionTranslator { ErrorCodedDataAccessException translateUniqueKeyViolation( Throwable sourceThrowable, PSQLException pSqlException) { ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage(); String tableName = serverErrorMessage.getTable(); String detailMessage = serverErrorMessage.getDetail(); Matcher matcher = Pattern.compile("Key \\(\"?(.*?)\"?\\)=\\((.*?)\\) already exists.").matcher(detailMessage); boolean matches = matcher.matches(); if (matches) { String[] columnNames = matcher.group(1).split(", "); if (columnNames.length == 1) { String columnName = columnNames[0]; String value = matcher.group(2); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); return new ValueAlreadyExistsException(entityTypeId, attributeName, value, sourceThrowable); } else { String columnName = columnNames[columnNames.length - 1]; String[] values = matcher.group(2).split(", "); String entityId = values[0]; String value = values[1]; String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); return new ListValueAlreadyExistsException( entityTypeId, attributeName, entityId, value, sourceThrowable); } } else { matcher = Pattern.compile("Key \\(\"?(.*?)\"?\\)=\\((.*?)\\) is duplicated.") .matcher(detailMessage); matches = matcher.matches(); if (matches) { String columnName = matcher.group(1); String value = matcher.group(2); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); return new DuplicateValueException(entityTypeId, attributeName, value, sourceThrowable); } else { LOG.error(ERROR_TRANSLATING_POSTGRES_EXC_MSG, pSqlException); throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } } } }
PostgreSqlExceptionTranslator extends SQLErrorCodeSQLExceptionTranslator implements TransactionExceptionTranslator { ErrorCodedDataAccessException translateUniqueKeyViolation( Throwable sourceThrowable, PSQLException pSqlException) { ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage(); String tableName = serverErrorMessage.getTable(); String detailMessage = serverErrorMessage.getDetail(); Matcher matcher = Pattern.compile("Key \\(\"?(.*?)\"?\\)=\\((.*?)\\) already exists.").matcher(detailMessage); boolean matches = matcher.matches(); if (matches) { String[] columnNames = matcher.group(1).split(", "); if (columnNames.length == 1) { String columnName = columnNames[0]; String value = matcher.group(2); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); return new ValueAlreadyExistsException(entityTypeId, attributeName, value, sourceThrowable); } else { String columnName = columnNames[columnNames.length - 1]; String[] values = matcher.group(2).split(", "); String entityId = values[0]; String value = values[1]; String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); return new ListValueAlreadyExistsException( entityTypeId, attributeName, entityId, value, sourceThrowable); } } else { matcher = Pattern.compile("Key \\(\"?(.*?)\"?\\)=\\((.*?)\\) is duplicated.") .matcher(detailMessage); matches = matcher.matches(); if (matches) { String columnName = matcher.group(1); String value = matcher.group(2); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); return new DuplicateValueException(entityTypeId, attributeName, value, sourceThrowable); } else { LOG.error(ERROR_TRANSLATING_POSTGRES_EXC_MSG, pSqlException); throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } } } PostgreSqlExceptionTranslator(DataSource dataSource, EntityTypeRegistry entityTypeRegistry); }
PostgreSqlExceptionTranslator extends SQLErrorCodeSQLExceptionTranslator implements TransactionExceptionTranslator { ErrorCodedDataAccessException translateUniqueKeyViolation( Throwable sourceThrowable, PSQLException pSqlException) { ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage(); String tableName = serverErrorMessage.getTable(); String detailMessage = serverErrorMessage.getDetail(); Matcher matcher = Pattern.compile("Key \\(\"?(.*?)\"?\\)=\\((.*?)\\) already exists.").matcher(detailMessage); boolean matches = matcher.matches(); if (matches) { String[] columnNames = matcher.group(1).split(", "); if (columnNames.length == 1) { String columnName = columnNames[0]; String value = matcher.group(2); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); return new ValueAlreadyExistsException(entityTypeId, attributeName, value, sourceThrowable); } else { String columnName = columnNames[columnNames.length - 1]; String[] values = matcher.group(2).split(", "); String entityId = values[0]; String value = values[1]; String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); return new ListValueAlreadyExistsException( entityTypeId, attributeName, entityId, value, sourceThrowable); } } else { matcher = Pattern.compile("Key \\(\"?(.*?)\"?\\)=\\((.*?)\\) is duplicated.") .matcher(detailMessage); matches = matcher.matches(); if (matches) { String columnName = matcher.group(1); String value = matcher.group(2); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); return new DuplicateValueException(entityTypeId, attributeName, value, sourceThrowable); } else { LOG.error(ERROR_TRANSLATING_POSTGRES_EXC_MSG, pSqlException); throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } } } PostgreSqlExceptionTranslator(DataSource dataSource, EntityTypeRegistry entityTypeRegistry); @Override @Nullable @CheckForNull DataAccessException doTranslate(TransactionException transactionException); }
PostgreSqlExceptionTranslator extends SQLErrorCodeSQLExceptionTranslator implements TransactionExceptionTranslator { ErrorCodedDataAccessException translateUniqueKeyViolation( Throwable sourceThrowable, PSQLException pSqlException) { ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage(); String tableName = serverErrorMessage.getTable(); String detailMessage = serverErrorMessage.getDetail(); Matcher matcher = Pattern.compile("Key \\(\"?(.*?)\"?\\)=\\((.*?)\\) already exists.").matcher(detailMessage); boolean matches = matcher.matches(); if (matches) { String[] columnNames = matcher.group(1).split(", "); if (columnNames.length == 1) { String columnName = columnNames[0]; String value = matcher.group(2); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); return new ValueAlreadyExistsException(entityTypeId, attributeName, value, sourceThrowable); } else { String columnName = columnNames[columnNames.length - 1]; String[] values = matcher.group(2).split(", "); String entityId = values[0]; String value = values[1]; String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); return new ListValueAlreadyExistsException( entityTypeId, attributeName, entityId, value, sourceThrowable); } } else { matcher = Pattern.compile("Key \\(\"?(.*?)\"?\\)=\\((.*?)\\) is duplicated.") .matcher(detailMessage); matches = matcher.matches(); if (matches) { String columnName = matcher.group(1); String value = matcher.group(2); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); return new DuplicateValueException(entityTypeId, attributeName, value, sourceThrowable); } else { LOG.error(ERROR_TRANSLATING_POSTGRES_EXC_MSG, pSqlException); throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } } } PostgreSqlExceptionTranslator(DataSource dataSource, EntityTypeRegistry entityTypeRegistry); @Override @Nullable @CheckForNull DataAccessException doTranslate(TransactionException transactionException);  }