Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update hibernate constraints exception #105

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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