Search in sources :

Example 31 with PSQLException

use of org.postgresql.util.PSQLException in project molgenis by molgenis.

the class PostgreSqlExceptionTranslatorTest method translateUniqueKeyViolationDoubleQuotes.

@Test
public 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
    MolgenisValidationException e = postgreSqlExceptionTranslator.translateUniqueKeyViolation(new PSQLException(serverErrorMessage));
    assertEquals(e.getMessage(), "Duplicate value 'myValue' for unique attribute 'myAttr' from entity 'myEntity'.");
}
Also used : ServerErrorMessage(org.postgresql.util.ServerErrorMessage) PSQLException(org.postgresql.util.PSQLException) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) Test(org.testng.annotations.Test)

Example 32 with PSQLException

use of org.postgresql.util.PSQLException in project molgenis by molgenis.

the class PostgreSqlExceptionTranslatorTest method translateDependentObjectsStillExistMultipleDependentTables.

@Test
public 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
    MolgenisValidationException e = postgreSqlExceptionTranslator.translateDependentObjectsStillExist(new PSQLException(serverErrorMessage));
    assertEquals(e.getMessage(), "Cannot delete entity 'myRefEntity' because entity 'myEntity' depends on it..Cannot delete entity 'myOtherRefEntity' because entity 'myEntity' depends on it.");
}
Also used : ServerErrorMessage(org.postgresql.util.ServerErrorMessage) PSQLException(org.postgresql.util.PSQLException) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) Test(org.testng.annotations.Test)

Example 33 with PSQLException

use of org.postgresql.util.PSQLException in project molgenis by molgenis.

the class PostgreSqlExceptionTranslatorTest method translateReadonlyViolationNoDoubleQuotes.

@Test
public 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
    MolgenisValidationException e = postgreSqlExceptionTranslator.translateReadonlyViolation(new PSQLException(serverErrorMessage));
    assertEquals(e.getMessage(), "Updating read-only attribute 'myAttr' of entity 'myEntity' with id 'abc' is not allowed.");
}
Also used : ServerErrorMessage(org.postgresql.util.ServerErrorMessage) PSQLException(org.postgresql.util.PSQLException) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) Test(org.testng.annotations.Test)

Example 34 with PSQLException

use of org.postgresql.util.PSQLException in project molgenis by molgenis.

the class PostgreSqlExceptionTranslatorTest method translateDependentObjectsStillExistOneDependentTableSingleDependency.

@Test
public 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
    MolgenisValidationException e = postgreSqlExceptionTranslator.translateDependentObjectsStillExist(new PSQLException(serverErrorMessage));
    assertEquals(e.getMessage(), "Cannot delete entity 'myRefEntity' because entity 'myEntity' depends on it.");
}
Also used : ServerErrorMessage(org.postgresql.util.ServerErrorMessage) PSQLException(org.postgresql.util.PSQLException) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) Test(org.testng.annotations.Test)

Example 35 with PSQLException

use of org.postgresql.util.PSQLException in project cloudbreak by hortonworks.

the class RdsConnectionBuilder method createDb.

private void createDb(Connection conn, String clusterName, String service) {
    String createSQL = "CREATE DATABASE ?";
    try (PreparedStatement preparedStatement = conn.prepareStatement(createSQL)) {
        preparedStatement.setString(1, clusterName + service);
        preparedStatement.executeUpdate(createSQL);
    } catch (PSQLException ex) {
        if ("42P04".equals(ex.getSQLState())) {
            LOGGER.warn("The expected database already exist");
        } else {
            throw new BadRequestException("Failed to create database in RDS: " + ex.getMessage(), ex);
        }
    } catch (SQLException e) {
        throw new BadRequestException("Failed to connect to RDS: " + e.getMessage(), e);
    }
}
Also used : SQLException(java.sql.SQLException) PSQLException(org.postgresql.util.PSQLException) PSQLException(org.postgresql.util.PSQLException) BadRequestException(com.sequenceiq.cloudbreak.controller.BadRequestException) PreparedStatement(java.sql.PreparedStatement)

Aggregations

PSQLException (org.postgresql.util.PSQLException)36 ServerErrorMessage (org.postgresql.util.ServerErrorMessage)28 Test (org.testng.annotations.Test)26 MolgenisValidationException (org.molgenis.data.validation.MolgenisValidationException)24 SQLException (java.sql.SQLException)3 MolgenisDataException (org.molgenis.data.MolgenisDataException)3 BatchUpdateException (java.sql.BatchUpdateException)2 PGExceptionSorter (com.alibaba.druid.pool.vendor.PGExceptionSorter)1 BadRequestException (com.sequenceiq.cloudbreak.controller.BadRequestException)1 IOException (java.io.IOException)1 String.format (java.lang.String.format)1 ConnectException (java.net.ConnectException)1 ByteBuffer (java.nio.ByteBuffer)1 PreparedStatement (java.sql.PreparedStatement)1 SQLWarning (java.sql.SQLWarning)1 ArrayList (java.util.ArrayList)1 Collections.singleton (java.util.Collections.singleton)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1