@Test void translateNotNullViolationBadMessage() { ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class); when(serverErrorMessage.getSQLState()).thenReturn("23502"); when(serverErrorMessage.getTable()).thenReturn("mytable"); when(serverErrorMessage.getMessage()).thenReturn("xxxyyyzzzz"); assertThrows( RuntimeException.class, () -> postgreSqlExceptionTranslator.translateNotNullViolation( mock(Throwable.class), new PSQLException(serverErrorMessage))); }
ErrorCodedDataAccessException translateNotNullViolation( Throwable sourceThrowable, PSQLException pSqlException) { ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage(); String tableName = serverErrorMessage.getTable(); String message = serverErrorMessage.getMessage(); Matcher matcher = Pattern.compile("null value in column \"?(.*?)\"? violates not-null constraint") .matcher(message); boolean matches = matcher.matches(); if (matches) { String columnName = matcher.group(1); EntityTypeDescription entityTypeDescription = entityTypeRegistry.getEntityTypeDescription(tableName); entityTypeDescription.getAttributeDescriptionMap().get(columnName); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); return new ValueRequiredException(entityTypeId, attributeName, sourceThrowable); } else { matcher = Pattern.compile("column \"(.*?)\" contains null values").matcher(message); matches = matcher.matches(); if (!matches) { throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } String columnName = matcher.group(1); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); return new ExistingNullValueException(entityTypeId, attributeName, sourceThrowable); } }
PostgreSqlExceptionTranslator extends SQLErrorCodeSQLExceptionTranslator implements TransactionExceptionTranslator { ErrorCodedDataAccessException translateNotNullViolation( Throwable sourceThrowable, PSQLException pSqlException) { ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage(); String tableName = serverErrorMessage.getTable(); String message = serverErrorMessage.getMessage(); Matcher matcher = Pattern.compile("null value in column \"?(.*?)\"? violates not-null constraint") .matcher(message); boolean matches = matcher.matches(); if (matches) { String columnName = matcher.group(1); EntityTypeDescription entityTypeDescription = entityTypeRegistry.getEntityTypeDescription(tableName); entityTypeDescription.getAttributeDescriptionMap().get(columnName); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); return new ValueRequiredException(entityTypeId, attributeName, sourceThrowable); } else { matcher = Pattern.compile("column \"(.*?)\" contains null values").matcher(message); matches = matcher.matches(); if (!matches) { throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } String columnName = matcher.group(1); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); return new ExistingNullValueException(entityTypeId, attributeName, sourceThrowable); } } }
PostgreSqlExceptionTranslator extends SQLErrorCodeSQLExceptionTranslator implements TransactionExceptionTranslator { ErrorCodedDataAccessException translateNotNullViolation( Throwable sourceThrowable, PSQLException pSqlException) { ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage(); String tableName = serverErrorMessage.getTable(); String message = serverErrorMessage.getMessage(); Matcher matcher = Pattern.compile("null value in column \"?(.*?)\"? violates not-null constraint") .matcher(message); boolean matches = matcher.matches(); if (matches) { String columnName = matcher.group(1); EntityTypeDescription entityTypeDescription = entityTypeRegistry.getEntityTypeDescription(tableName); entityTypeDescription.getAttributeDescriptionMap().get(columnName); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); return new ValueRequiredException(entityTypeId, attributeName, sourceThrowable); } else { matcher = Pattern.compile("column \"(.*?)\" contains null values").matcher(message); matches = matcher.matches(); if (!matches) { throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } String columnName = matcher.group(1); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); return new ExistingNullValueException(entityTypeId, attributeName, sourceThrowable); } } PostgreSqlExceptionTranslator(DataSource dataSource, EntityTypeRegistry entityTypeRegistry); }
PostgreSqlExceptionTranslator extends SQLErrorCodeSQLExceptionTranslator implements TransactionExceptionTranslator { ErrorCodedDataAccessException translateNotNullViolation( Throwable sourceThrowable, PSQLException pSqlException) { ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage(); String tableName = serverErrorMessage.getTable(); String message = serverErrorMessage.getMessage(); Matcher matcher = Pattern.compile("null value in column \"?(.*?)\"? violates not-null constraint") .matcher(message); boolean matches = matcher.matches(); if (matches) { String columnName = matcher.group(1); EntityTypeDescription entityTypeDescription = entityTypeRegistry.getEntityTypeDescription(tableName); entityTypeDescription.getAttributeDescriptionMap().get(columnName); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); return new ValueRequiredException(entityTypeId, attributeName, sourceThrowable); } else { matcher = Pattern.compile("column \"(.*?)\" contains null values").matcher(message); matches = matcher.matches(); if (!matches) { throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } String columnName = matcher.group(1); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); return new ExistingNullValueException(entityTypeId, attributeName, sourceThrowable); } } PostgreSqlExceptionTranslator(DataSource dataSource, EntityTypeRegistry entityTypeRegistry); @Override @Nullable @CheckForNull DataAccessException doTranslate(TransactionException transactionException); }
PostgreSqlExceptionTranslator extends SQLErrorCodeSQLExceptionTranslator implements TransactionExceptionTranslator { ErrorCodedDataAccessException translateNotNullViolation( Throwable sourceThrowable, PSQLException pSqlException) { ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage(); String tableName = serverErrorMessage.getTable(); String message = serverErrorMessage.getMessage(); Matcher matcher = Pattern.compile("null value in column \"?(.*?)\"? violates not-null constraint") .matcher(message); boolean matches = matcher.matches(); if (matches) { String columnName = matcher.group(1); EntityTypeDescription entityTypeDescription = entityTypeRegistry.getEntityTypeDescription(tableName); entityTypeDescription.getAttributeDescriptionMap().get(columnName); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); return new ValueRequiredException(entityTypeId, attributeName, sourceThrowable); } else { matcher = Pattern.compile("column \"(.*?)\" contains null values").matcher(message); matches = matcher.matches(); if (!matches) { throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException); } String columnName = matcher.group(1); String attributeName = tryGetAttributeName(tableName, columnName).orElse(null); String entityTypeId = tryGetEntityTypeName(tableName).orElse(null); return new ExistingNullValueException(entityTypeId, attributeName, sourceThrowable); } } PostgreSqlExceptionTranslator(DataSource dataSource, EntityTypeRegistry entityTypeRegistry); @Override @Nullable @CheckForNull DataAccessException doTranslate(TransactionException transactionException);  }