@Test 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"); ReadonlyValueException e = postgreSqlExceptionTranslator.translateReadonlyViolation( mock(Throwable.class), new PSQLException(serverErrorMessage)); assertEquals("entityTypeId:myEntity attributeName:myAttr entityId:abc", e.getMessage()); }
ReadonlyValueException translateReadonlyViolation( Throwable sourceThrowable, PSQLException pSqlException) { Matcher matcher = Pattern.compile( "Updating read-only column \"?(.*?)\"? of table \"?(.*?)\"? with id \\[(.*?)] is not allowed") .matcher(pSqlException.getServerErrorMessage().getMessage()); boolean matches = matcher.matches(); if (!matches) { LOG.error(ERROR_TRANSLATING_POSTGRES_EXC_MSG, pSqlException); throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } String colName = matcher.group(1); String tableName = matcher.group(2); String id = matcher.group(3); String attributeName = tryGetAttributeName(tableName, colName).orElse(null); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); return new ReadonlyValueException(entityTypeId, attributeName, id, sourceThrowable); }
PostgreSqlExceptionTranslator extends SQLErrorCodeSQLExceptionTranslator implements TransactionExceptionTranslator { ReadonlyValueException translateReadonlyViolation( Throwable sourceThrowable, PSQLException pSqlException) { Matcher matcher = Pattern.compile( "Updating read-only column \"?(.*?)\"? of table \"?(.*?)\"? with id \\[(.*?)] is not allowed") .matcher(pSqlException.getServerErrorMessage().getMessage()); boolean matches = matcher.matches(); if (!matches) { LOG.error(ERROR_TRANSLATING_POSTGRES_EXC_MSG, pSqlException); throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } String colName = matcher.group(1); String tableName = matcher.group(2); String id = matcher.group(3); String attributeName = tryGetAttributeName(tableName, colName).orElse(null); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); return new ReadonlyValueException(entityTypeId, attributeName, id, sourceThrowable); } }
PostgreSqlExceptionTranslator extends SQLErrorCodeSQLExceptionTranslator implements TransactionExceptionTranslator { ReadonlyValueException translateReadonlyViolation( Throwable sourceThrowable, PSQLException pSqlException) { Matcher matcher = Pattern.compile( "Updating read-only column \"?(.*?)\"? of table \"?(.*?)\"? with id \\[(.*?)] is not allowed") .matcher(pSqlException.getServerErrorMessage().getMessage()); boolean matches = matcher.matches(); if (!matches) { LOG.error(ERROR_TRANSLATING_POSTGRES_EXC_MSG, pSqlException); throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } String colName = matcher.group(1); String tableName = matcher.group(2); String id = matcher.group(3); String attributeName = tryGetAttributeName(tableName, colName).orElse(null); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); return new ReadonlyValueException(entityTypeId, attributeName, id, sourceThrowable); } PostgreSqlExceptionTranslator(DataSource dataSource, EntityTypeRegistry entityTypeRegistry); }
PostgreSqlExceptionTranslator extends SQLErrorCodeSQLExceptionTranslator implements TransactionExceptionTranslator { ReadonlyValueException translateReadonlyViolation( Throwable sourceThrowable, PSQLException pSqlException) { Matcher matcher = Pattern.compile( "Updating read-only column \"?(.*?)\"? of table \"?(.*?)\"? with id \\[(.*?)] is not allowed") .matcher(pSqlException.getServerErrorMessage().getMessage()); boolean matches = matcher.matches(); if (!matches) { LOG.error(ERROR_TRANSLATING_POSTGRES_EXC_MSG, pSqlException); throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } String colName = matcher.group(1); String tableName = matcher.group(2); String id = matcher.group(3); String attributeName = tryGetAttributeName(tableName, colName).orElse(null); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); return new ReadonlyValueException(entityTypeId, attributeName, id, sourceThrowable); } PostgreSqlExceptionTranslator(DataSource dataSource, EntityTypeRegistry entityTypeRegistry); @Override @Nullable @CheckForNull DataAccessException doTranslate(TransactionException transactionException); }
PostgreSqlExceptionTranslator extends SQLErrorCodeSQLExceptionTranslator implements TransactionExceptionTranslator { ReadonlyValueException translateReadonlyViolation( Throwable sourceThrowable, PSQLException pSqlException) { Matcher matcher = Pattern.compile( "Updating read-only column \"?(.*?)\"? of table \"?(.*?)\"? with id \\[(.*?)] is not allowed") .matcher(pSqlException.getServerErrorMessage().getMessage()); boolean matches = matcher.matches(); if (!matches) { LOG.error(ERROR_TRANSLATING_POSTGRES_EXC_MSG, pSqlException); throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } String colName = matcher.group(1); String tableName = matcher.group(2); String id = matcher.group(3); String attributeName = tryGetAttributeName(tableName, colName).orElse(null); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); return new ReadonlyValueException(entityTypeId, attributeName, id, sourceThrowable); } PostgreSqlExceptionTranslator(DataSource dataSource, EntityTypeRegistry entityTypeRegistry); @Override @Nullable @CheckForNull DataAccessException doTranslate(TransactionException transactionException);  }