use of org.postgresql.util.ServerErrorMessage in project molgenis by molgenis.
the class PostgreSqlExceptionTranslator method translateForeignKeyViolation.
/**
* Package private for testability
*
* @param pSqlException PostgreSQL exception
* @return translated validation exception
*/
MolgenisValidationException translateForeignKeyViolation(PSQLException pSqlException) {
ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage();
String tableName = serverErrorMessage.getTable();
String detailMessage = serverErrorMessage.getDetail();
Matcher m = Pattern.compile("\\((.*?)\\)").matcher(detailMessage);
if (!m.find()) {
LOG.error("Error translating postgres exception: ", pSqlException);
throw new RuntimeException("Error translating exception", pSqlException);
}
String colName = m.group(1);
if (!m.find()) {
LOG.error("Error translating postgres exception: ", pSqlException);
throw new RuntimeException("Error translating exception", pSqlException);
}
String value = m.group(1);
String constraintViolationMessageTemplate;
String attrName;
if (detailMessage.contains("still referenced from")) {
// ERROR: update or delete on table "x" violates foreign key constraint "y" on table "z"
// Detail: Key (k)=(v) is still referenced from table "x".
constraintViolationMessageTemplate = "Value '%s' for attribute '%s' is referenced by entity '%s'.";
String refTableName = getRefTableFromForeignKeyPsqlException(pSqlException);
attrName = getAttributeName(refTableName, colName);
} else {
// ERROR: insert or update on table "x" violates foreign key constraint "y"
// Detail: Key (k)=(v) is not present in table "z".
constraintViolationMessageTemplate = "Unknown xref value '%s' for attribute '%s' of entity '%s'.";
attrName = getAttributeName(tableName, colName);
}
ConstraintViolation constraintViolation = new ConstraintViolation(format(constraintViolationMessageTemplate, value, attrName, getEntityTypeName(tableName)), null);
return new MolgenisValidationException(singleton(constraintViolation));
}
use of org.postgresql.util.ServerErrorMessage in project molgenis by molgenis.
the class PostgreSqlExceptionTranslator method translateCheckConstraintViolation.
/**
* Package private for testability
*
* @param pSqlException PostgreSQL exception
* @return translated validation exception
*/
MolgenisValidationException translateCheckConstraintViolation(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);
ConstraintViolation constraintViolation = new ConstraintViolation(format("Unknown enum value for attribute '%s' of entity '%s'.", getAttributeName(tableName, columnName), getEntityTypeName(tableName)), null);
return new MolgenisValidationException(singleton(constraintViolation));
}
use of org.postgresql.util.ServerErrorMessage in project molgenis by molgenis.
the class PostgreSqlExceptionTranslator method translateInvalidIntegerException.
/**
* Package private for testability
*
* @param pSqlException PostgreSQL exception
* @return translated validation exception
*/
static MolgenisValidationException translateInvalidIntegerException(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", 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);
ConstraintViolation constraintViolation = new ConstraintViolation(format("Value [%s] of this entity attribute is not of type [%s].", value, type), null);
return new MolgenisValidationException(singleton(constraintViolation));
}
use of org.postgresql.util.ServerErrorMessage in project molgenis by molgenis.
the class PostgreSqlExceptionTranslatorTest method translateUniqueKeyViolationKeyIsDuplicatedDoubleQuotes.
@Test
public 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
MolgenisValidationException e = postgreSqlExceptionTranslator.translateUniqueKeyViolation(new PSQLException(serverErrorMessage));
assertEquals(e.getMessage(), "The attribute 'myAttr' of entity 'myEntity' contains duplicate value 'myValue'.");
}
use of org.postgresql.util.ServerErrorMessage in project molgenis by molgenis.
the class PostgreSqlExceptionTranslatorTest method translateInvalidIntegerExceptionBoolean.
@Test
public void translateInvalidIntegerExceptionBoolean() {
ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
when(serverErrorMessage.getMessage()).thenReturn("invalid input syntax for type boolean: \"str1\"");
// noinspection ThrowableResultOfMethodCallIgnored
MolgenisValidationException e = PostgreSqlExceptionTranslator.translateInvalidIntegerException(new PSQLException(serverErrorMessage));
assertEquals(e.getMessage(), "Value [str1] of this entity attribute is not of type [BOOL].");
}
Aggregations