Java Code Examples for org.postgresql.util.ServerErrorMessage

The following examples show how to use org.postgresql.util.ServerErrorMessage. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@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);
}
 
Example 2
@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.");
  //noinspection ThrowableResultOfMethodCallIgnored

  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);
}
 
Example 3
@Test
@SneakyThrows
public void assertExecuteReturnErrorResponsePacket() {
    PostgreSQLComQueryExecutor postgreSQLComQueryExecutor = new PostgreSQLComQueryExecutor(mock(PostgreSQLComQueryPacket.class), null);
    FieldSetter.setField(postgreSQLComQueryExecutor, PostgreSQLComQueryExecutor.class.getDeclaredField("textProtocolBackendHandler"), textProtocolBackendHandler);
    ErrorResponse errorResponse = new ErrorResponse(new PSQLException(mock(ServerErrorMessage.class)));
    when(textProtocolBackendHandler.execute()).thenReturn(errorResponse);
    Assert.assertThat(postgreSQLComQueryExecutor.execute().iterator().next(), Matchers.instanceOf(PostgreSQLErrorResponsePacket.class));
    Assert.assertThat(postgreSQLComQueryExecutor.isErrorResponse(), Matchers.is(true));
}
 
Example 4
@Test
@SneakyThrows
public void assertExecuteHasError() {
    PostgreSQLComBindExecutor postgreSQLComBindExecutor = new PostgreSQLComBindExecutor(mock(PostgreSQLComBindPacket.class), null);
    FieldSetter.setField(postgreSQLComBindExecutor, PostgreSQLComBindExecutor.class.getDeclaredField("databaseCommunicationEngine"), databaseCommunicationEngine);
    ErrorResponse errorResponse = new ErrorResponse(new PSQLException(mock(ServerErrorMessage.class)));
    when(databaseCommunicationEngine.execute()).thenReturn(errorResponse);
    Assert.assertThat(((LinkedList) postgreSQLComBindExecutor.execute()).get(1), Matchers.instanceOf(PostgreSQLErrorResponsePacket.class));
    Assert.assertThat(postgreSQLComBindExecutor.isErrorResponse(), Matchers.is(true));
}
 
Example 5
/** Package private for testability */
EntityTypeReferencedException translateDependentObjectsStillExist(
    Throwable sourceThrowable, PSQLException pSqlException) {
  ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage();
  String detail = serverErrorMessage.getDetail();
  Matcher matcher =
      Pattern.compile(
              "constraint (.+) on table \"?([^\"]+)\"? depends on table \"?([^\"]+)\"?\n?")
          .matcher(detail);

  Map<String, Set<String>> entityTypeDependencyMap = new LinkedHashMap<>();
  while (matcher.find()) {
    String tableName = matcher.group(2);
    String dependentTableName = matcher.group(3);

    String entityTypeName = tryGetEntityTypeName(tableName).orElse(null);
    String dependentEntityTypeName = tryGetEntityTypeName(dependentTableName).orElse(null);
    Set<String> dependentTableNames =
        entityTypeDependencyMap.computeIfAbsent(
            dependentEntityTypeName, k -> new LinkedHashSet<>());
    dependentTableNames.add(entityTypeName);
  }

  if (entityTypeDependencyMap.isEmpty()) // no matches
  {
    LOG.error(ERROR_TRANSLATING_POSTGRES_EXC_MSG, pSqlException);
    throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException);
  }

  return new EntityTypeReferencedException(entityTypeDependencyMap, sourceThrowable);
}
 
Example 6
/** Package private for testability */
static InvalidValueTypeException translateInvalidIntegerException(
    Throwable sourceThrowable, PSQLException pSqlException) {
  ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage();
  String message = serverErrorMessage.getMessage();
  Matcher matcher =
      Pattern.compile("invalid input syntax for \\b(?:type )?\\b(.+?): \"(.*?)\"")
          .matcher(message);
  boolean matches = matcher.matches();
  if (!matches) {
    throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException);
  }
  String postgreSqlType = matcher.group(1);

  // convert PostgreSQL data type to attribute type:
  String type;
  switch (postgreSqlType) {
    case "boolean":
      type = BOOL.toString();
      break;
    case "date":
      type = DATE.toString();
      break;
    case "timestamp with time zone":
      type = DATE_TIME.toString();
      break;
    case "double precision":
      type = DECIMAL.toString();
      break;
    case "integer":
      type = INT.toString() + " or " + LONG.toString();
      break;
    default:
      type = postgreSqlType;
      break;
  }
  String value = matcher.group(2);

  return new InvalidValueTypeException(value, type, sourceThrowable);
}
 
Example 7
private String getRefTableFromForeignKeyPsqlException(PSQLException pSqlException) {
  ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage();
  Matcher messageMatcher =
      Pattern.compile(
              "update or delete on table \"(.*)\" violates foreign key constraint \"(.*)\" on table \"(.*)\"")
          .matcher(serverErrorMessage.getMessage());
  if (!messageMatcher.matches()) {
    LOG.error(ERROR_TRANSLATING_POSTGRES_EXC_MSG, pSqlException);
    throw new RuntimeException(ERROR_TRANSLATING_EXCEPTION_MSG, pSqlException);
  }
  return messageMatcher.group(1);
}
 
Example 8
/** Package private for testability */
UnknownEnumValueException translateCheckConstraintViolation(
    Throwable sourceThrowable, PSQLException pSqlException) {
  ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage();
  String tableName = serverErrorMessage.getTable();
  String constraintName = serverErrorMessage.getConstraint();
  // constraint name: <tableName>_<columnName>_chk
  String columnName =
      constraintName.substring(tableName.length() + 1, constraintName.length() - 4);
  String entityTypeId = tryGetEntityTypeName(tableName).orElse(null);
  String attributeName = tryGetAttributeName(tableName, columnName).orElse(null);
  return new UnknownEnumValueException(entityTypeId, attributeName, sourceThrowable);
}
 
Example 9
/** Package private for testability */
static MolgenisValidationException translateUndefinedColumnException(
    PSQLException pSqlException) {
  ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage();
  String message = serverErrorMessage.getMessage(); // FIXME exposes internal message
  ConstraintViolation constraintViolation = new ConstraintViolation(message);
  return new MolgenisValidationException(singleton(constraintViolation));
}
 
Example 10
@Test
void translateValueTooLongViolation() {
  ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
  when(serverErrorMessage.getMessage())
      .thenReturn("ERROR: value too long for type character varying(255)");
  //noinspection ThrowableResultOfMethodCallIgnored
  ValueLengthExceededException e =
      postgreSqlExceptionTranslator.translateValueTooLongViolation(mock(Throwable.class));
  assertNull(e.getMessage());
}
 
Example 11
@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");
  //noinspection ThrowableResultOfMethodCallIgnored
  ReadonlyValueException e =
      postgreSqlExceptionTranslator.translateReadonlyViolation(
          mock(Throwable.class), new PSQLException(serverErrorMessage));
  assertEquals("entityTypeId:myEntity attributeName:myAttr entityId:abc", e.getMessage());
}
 
Example 12
@Test
void translateReadonlyViolationNoDoubleQuotes() {
  ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
  when(serverErrorMessage.getMessage())
      .thenReturn(
          "Updating read-only column myColumn of table myTable with id [abc] is not allowed");
  //noinspection ThrowableResultOfMethodCallIgnored

  ReadonlyValueException e =
      postgreSqlExceptionTranslator.translateReadonlyViolation(
          mock(Throwable.class), new PSQLException(serverErrorMessage));
  assertEquals("entityTypeId:myEntity attributeName:myAttr entityId:abc", e.getMessage());
}
 
Example 13
@Test
void translateDependentObjectsStillExistOneDependentTableSingleDependency() {
  ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
  when(serverErrorMessage.getSQLState()).thenReturn("2BP01");
  when(serverErrorMessage.getDetail())
      .thenReturn(
          "constraint my_foreign_key_constraint on table \"myTable\" depends on table \"myDependentTable\"");
  //noinspection ThrowableResultOfMethodCallIgnored

  EntityTypeReferencedException e =
      postgreSqlExceptionTranslator.translateDependentObjectsStillExist(
          mock(Throwable.class), new PSQLException(serverErrorMessage));
  assertEquals("dependencies:myRefEntity=myEntity", e.getMessage());
}
 
Example 14
@Test
void translateDependentObjectsStillExistOneDependentTableMultipleDependencies() {
  ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
  when(serverErrorMessage.getSQLState()).thenReturn("2BP01");
  when(serverErrorMessage.getDetail())
      .thenReturn(
          "constraint my_foreign_key_constraint on table \"myTable\" depends on table \"myDependentTable\"\nconstraint myOther_foreign_key_constraint on table \"myTable\" depends on table \"myDependentTable\"");
  //noinspection ThrowableResultOfMethodCallIgnored

  EntityTypeReferencedException e =
      postgreSqlExceptionTranslator.translateDependentObjectsStillExist(
          mock(Throwable.class), new PSQLException(serverErrorMessage));
  assertEquals("dependencies:myRefEntity=myEntity", e.getMessage());
}
 
Example 15
@Test
void translateDependentObjectsStillExistMultipleDependentTables() {
  ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
  when(serverErrorMessage.getSQLState()).thenReturn("2BP01");
  when(serverErrorMessage.getDetail())
      .thenReturn(
          "constraint my_foreign_key_constraint on table \"myTable\" depends on table \"myDependentTable\"\nconstraint myOther_foreign_key_constraint on table \"myTable\" depends on table \"myOtherDependentTable\"");
  //noinspection ThrowableResultOfMethodCallIgnored

  EntityTypeReferencedException e =
      postgreSqlExceptionTranslator.translateDependentObjectsStillExist(
          mock(Throwable.class), new PSQLException(serverErrorMessage));
  assertEquals("dependencies:myRefEntity=myEntity;myOtherRefEntity=myEntity", e.getMessage());
}
 
Example 16
@Test
void translateDependentObjectsStillExistNoDoubleQuotes() {
  ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
  when(serverErrorMessage.getSQLState()).thenReturn("2BP01");
  when(serverErrorMessage.getDetail())
      .thenReturn(
          "constraint my_foreign_key_constraint on table myTable depends on table myDependentTable");
  //noinspection ThrowableResultOfMethodCallIgnored

  EntityTypeReferencedException e =
      postgreSqlExceptionTranslator.translateDependentObjectsStillExist(
          mock(Throwable.class), new PSQLException(serverErrorMessage));
  assertEquals("dependencies:myRefEntity=myEntity", e.getMessage());
}
 
Example 17
@Test
void translateNotNullViolation() {
  ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
  when(serverErrorMessage.getSQLState()).thenReturn("23502");
  when(serverErrorMessage.getTable()).thenReturn("myTable");
  when(serverErrorMessage.getMessage())
      .thenReturn("null value in column \"myColumn\" violates not-null constraint");
  //noinspection ThrowableResultOfMethodCallIgnored

  Exception e =
      postgreSqlExceptionTranslator.translateNotNullViolation(
          mock(Throwable.class), new PSQLException(serverErrorMessage));
  assertEquals("entityTypeId:myEntity attributeName:myAttr", e.getMessage());
  assertTrue(e instanceof ValueRequiredException);
}
 
Example 18
@Test
void translateNotNullViolationBadMessage() {
  ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
  when(serverErrorMessage.getSQLState()).thenReturn("23502");
  when(serverErrorMessage.getTable()).thenReturn("mytable");
  when(serverErrorMessage.getMessage()).thenReturn("xxxyyyzzzz");
  //noinspection ThrowableResultOfMethodCallIgnored
  assertThrows(
      RuntimeException.class,
      () ->
          postgreSqlExceptionTranslator.translateNotNullViolation(
              mock(Throwable.class), new PSQLException(serverErrorMessage)));
}
 
Example 19
@Test
void translateNotNullViolationNoDoubleQuotes() {
  ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
  when(serverErrorMessage.getSQLState()).thenReturn("23502");
  when(serverErrorMessage.getTable()).thenReturn("myTable");
  when(serverErrorMessage.getMessage())
      .thenReturn("null value in column myColumn violates not-null constraint");
  //noinspection ThrowableResultOfMethodCallIgnored

  Exception e =
      postgreSqlExceptionTranslator.translateNotNullViolation(
          mock(Throwable.class), new PSQLException(serverErrorMessage));
  assertEquals("entityTypeId:myEntity attributeName:myAttr", e.getMessage());
  assertTrue(e instanceof ValueRequiredException);
}
 
Example 20
@Test
void translateForeignKeyViolation() {
  ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
  when(serverErrorMessage.getSQLState()).thenReturn("23503");
  when(serverErrorMessage.getTable()).thenReturn("myTable");
  when(serverErrorMessage.getDetail()).thenReturn("... (myColumn) ... (myValue) ...");
  //noinspection ThrowableResultOfMethodCallIgnored

  Exception e =
      postgreSqlExceptionTranslator.translateForeignKeyViolation(
          mock(Throwable.class), new PSQLException(serverErrorMessage));
  assertEquals("entityTypeId:myEntity attributeName:myAttr value:myValue", e.getMessage());
  assertTrue(e instanceof UnknownValueReferenceException);
}
 
Example 21
@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\"");
  //noinspection ThrowableResultOfMethodCallIgnored

  Exception e =
      postgreSqlExceptionTranslator.translateForeignKeyViolation(
          mock(Throwable.class), new PSQLException(serverErrorMessage));
  assertEquals("entityTypeId:myEntity attributeName:myAttr value:myValue", e.getMessage());
  assertTrue(e instanceof UnknownValueReferenceException);
}
 
Example 22
@Test
void translateForeignKeyViolationBadMessage() {
  ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
  when(serverErrorMessage.getSQLState()).thenReturn("23503");
  when(serverErrorMessage.getTable()).thenReturn("mytable");
  when(serverErrorMessage.getDetail()).thenReturn("xxxyyyyzzzz");
  //noinspection ThrowableResultOfMethodCallIgnored
  assertThrows(
      RuntimeException.class,
      () ->
          postgreSqlExceptionTranslator.translateForeignKeyViolation(
              mock(Throwable.class), new PSQLException(serverErrorMessage)));
}
 
Example 23
@Test
void translateForeignKeyViolationCannotResolveAttribute() {
  ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
  when(serverErrorMessage.getSQLState()).thenReturn("23503");
  when(serverErrorMessage.getTable()).thenReturn("myTable");
  when(serverErrorMessage.getDetail()).thenReturn("... (myUnknownColumn) ... (myValue) ...");
  //noinspection ThrowableResultOfMethodCallIgnored

  Exception e =
      postgreSqlExceptionTranslator.translateForeignKeyViolation(
          mock(Throwable.class), new PSQLException(serverErrorMessage));
  assertEquals("entityTypeId:myEntity attributeName:null value:myValue", e.getMessage());
  assertTrue(e instanceof UnknownValueReferenceException);
}
 
Example 24
@Test
void translateForeignKeyViolationCannotResolveEntityType() {
  ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
  when(serverErrorMessage.getSQLState()).thenReturn("23503");
  when(serverErrorMessage.getTable()).thenReturn("myUnknownTable");
  when(serverErrorMessage.getDetail()).thenReturn("... (myColumn) ... (myValue) ...");
  //noinspection ThrowableResultOfMethodCallIgnored

  Exception e =
      postgreSqlExceptionTranslator.translateForeignKeyViolation(
          mock(Throwable.class), new PSQLException(serverErrorMessage));
  assertEquals("entityTypeId:null attributeName:null value:myValue", e.getMessage());
  assertTrue(e instanceof UnknownValueReferenceException);
}
 
Example 25
@Test
void translateUniqueKeyViolation() {
  ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
  when(serverErrorMessage.getSQLState()).thenReturn("23505");
  when(serverErrorMessage.getTable()).thenReturn("myTable");
  when(serverErrorMessage.getDetail()).thenReturn("Key (myColumn)=(myValue) already exists.");
  //noinspection ThrowableResultOfMethodCallIgnored

  Exception e =
      postgreSqlExceptionTranslator.translateUniqueKeyViolation(
          mock(Throwable.class), new PSQLException(serverErrorMessage));
  assertEquals("entityTypeId:myEntity attributeName:myAttr value:myValue", e.getMessage());
  assertTrue(e instanceof ValueAlreadyExistsException);
}
 
Example 26
@Test
void translateUniqueKeyViolationDoubleQuotes() {
  ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
  when(serverErrorMessage.getSQLState()).thenReturn("23505");
  when(serverErrorMessage.getTable()).thenReturn("myTable");
  when(serverErrorMessage.getDetail()).thenReturn("Key (\"myColumn\")=(myValue) already exists.");
  //noinspection ThrowableResultOfMethodCallIgnored

  Exception e =
      postgreSqlExceptionTranslator.translateUniqueKeyViolation(
          mock(Throwable.class), new PSQLException(serverErrorMessage));
  assertEquals("entityTypeId:myEntity attributeName:myAttr value:myValue", e.getMessage());
  assertTrue(e instanceof ValueAlreadyExistsException);
}
 
Example 27
@Test
void translateUniqueKeyViolationKeyIsDuplicated() {
  ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
  when(serverErrorMessage.getSQLState()).thenReturn("23505");
  when(serverErrorMessage.getTable()).thenReturn("myTable");
  when(serverErrorMessage.getDetail()).thenReturn("Key (myColumn)=(myValue) is duplicated.");
  //noinspection ThrowableResultOfMethodCallIgnored

  Exception e =
      postgreSqlExceptionTranslator.translateUniqueKeyViolation(
          mock(Throwable.class), new PSQLException(serverErrorMessage));
  assertEquals("entityTypeId:myEntity attributeName:myAttr value:myValue", e.getMessage());
  assertTrue(e instanceof DuplicateValueException);
}
 
Example 28
@Test
void translateUniqueKeyViolationKeyIsDuplicatedDoubleQuotes() {
  ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
  when(serverErrorMessage.getSQLState()).thenReturn("23505");
  when(serverErrorMessage.getTable()).thenReturn("myTable");
  when(serverErrorMessage.getDetail()).thenReturn("Key (\"myColumn\")=(myValue) is duplicated.");
  //noinspection ThrowableResultOfMethodCallIgnored

  Exception e =
      postgreSqlExceptionTranslator.translateUniqueKeyViolation(
          mock(Throwable.class), new PSQLException(serverErrorMessage));
  assertEquals("entityTypeId:myEntity attributeName:myAttr value:myValue", e.getMessage());
  assertTrue(e instanceof DuplicateValueException);
}
 
Example 29
@Test
void translateUniqueKeyViolationBadMessage() {
  ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
  when(serverErrorMessage.getSQLState()).thenReturn("23505");
  when(serverErrorMessage.getTable()).thenReturn("mytable");
  when(serverErrorMessage.getDetail()).thenReturn("xxxyyyyzzz");
  //noinspection ThrowableResultOfMethodCallIgnored
  assertThrows(
      RuntimeException.class,
      () ->
          postgreSqlExceptionTranslator.translateUniqueKeyViolation(
              mock(Throwable.class), new PSQLException(serverErrorMessage)));
}
 
Example 30
@Test
void translateInvalidIntegerExceptionInteger() {
  ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
  when(serverErrorMessage.getMessage()).thenReturn("invalid input syntax for integer: \"str1\"");
  //noinspection ThrowableResultOfMethodCallIgnored

  InvalidValueTypeException e =
      PostgreSqlExceptionTranslator.translateInvalidIntegerException(
          mock(Throwable.class), new PSQLException(serverErrorMessage));
  assertEquals("value:str1 type:INT or LONG", e.getMessage());
}