Skip to content

Commit

Permalink
fix: update hibernate constraints exception (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras authored Nov 15, 2023
1 parent b188c85 commit 660760c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
import jakarta.persistence.EntityGraph;
import jakarta.persistence.EntityManager;
import jakarta.persistence.LockModeType;
import jakarta.persistence.PersistenceException;
import jakarta.persistence.Query;
import jakarta.persistence.TypedQuery;
import jakarta.persistence.criteria.CriteriaDelete;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.CriteriaUpdate;
import jakarta.transaction.Transactional;

import org.hibernate.exception.ConstraintViolationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tkit.quarkus.jpa.exceptions.ConstraintException;
Expand Down Expand Up @@ -540,29 +540,17 @@ protected void lock(T entity, LockModeType lockMode) {
*/
@SuppressWarnings("squid:S1872")
protected DAOException handleConstraint(Exception ex, Enum<?> key) {
if (ex instanceof ConstraintException) {
return (ConstraintException) ex;
if (ex instanceof ConstraintException ce) {
return ce;
}
if (ex instanceof PersistenceException) {
PersistenceException e = (PersistenceException) ex;
if (e.getCause() != null) {

Throwable providerException = e.getCause();
// Hibernate constraint violation exception
if ("org.hibernate.exception.ConstraintViolationException".equals(providerException.getClass().getName())) {

// for the org.postgresql.util.PSQLException get the constraints message.
String msg = providerException.getMessage();
if (providerException.getCause() != null) {
msg = providerException.getCause().getMessage();
if (msg != null) {
msg = msg.replaceAll("\n", "");
}
}
// throw own constraints exception.
return new ConstraintException(msg, key, e, entityName);
}
if (ex instanceof ConstraintViolationException cve) {
var msg = cve.getErrorMessage();
if (msg != null) {
msg = msg.replaceAll("\n", "").replaceAll("\"", "'");
}
var re = new ConstraintException(msg, key, ex, entityName);
re.addParameter("constraintName", cve.getConstraintName());
return re;
}
return new DAOException(key, ex, entityName);
}
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

<properties>
<!-- Quarkus -->
<quarkus.version>3.5.0</quarkus.version>
<quarkus.version>3.5.1</quarkus.version>
<quarkus.hibernate-orm.version>6.3.1.Final</quarkus.hibernate-orm.version>
<quarkiverse.mockserver.version>1.2.0</quarkiverse.mockserver.version>
<quarkus.jandex-plugin.version>3.1.5</quarkus.jandex-plugin.version>
Expand Down

0 comments on commit 660760c

Please sign in to comment.