Skip to content

Commit

Permalink
avniproject/avni-client#1458 | Log constraint violation specifics dur…
Browse files Browse the repository at this point in the history
…ing entity upsert
  • Loading branch information
himeshr committed Jul 12, 2024
1 parent 9fb6e9d commit 77116c8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.avni.server.dao;

import org.avni.server.domain.CHSEntity;
import org.avni.server.service.exception.ConstraintViolationExceptionAcrossOrganisations;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.SliceImpl;
Expand All @@ -9,9 +11,11 @@
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;

public class CustomJpaRepositoryImpl<T extends CHSEntity, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements CustomCHSJpaRepository<T, ID> {

Expand All @@ -37,4 +41,17 @@ private Slice<T> readSlice(TypedQuery<T> query, Pageable pageable, Specification
}
return new SliceImpl<>(content, pageable, hasNextPage);
}


@Override
public <S extends T> S save(S entity) {
try {
return super.save(entity);
} catch (PersistenceException pe) {
if (Objects.isNull(entity.getId()) && pe.getCause() != null && pe.getCause().getClass().equals(ConstraintViolationException.class)) {
throw new ConstraintViolationExceptionAcrossOrganisations(String.format("Entity=> ID: %d, UUID: %s, Type:%s, User:%s, Msg: %s", entity.getId(), entity.getUuid(), entity.getClass().getCanonicalName(), entity.getLastModifiedByName(), pe.getMessage()), (ConstraintViolationException) pe.getCause());
}
throw pe;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.avni.server.service.exception;

import org.hibernate.exception.ConstraintViolationException;

public class ConstraintViolationExceptionAcrossOrganisations extends ConstraintViolationException {
public ConstraintViolationExceptionAcrossOrganisations(String message, ConstraintViolationException cve) {
super(message, cve.getSQLException(), cve.getSQL(), cve.getConstraintName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.avni.server.domain.accessControl.AvniAccessException;
import org.avni.server.domain.accessControl.AvniNoUserSessionException;
import org.avni.server.framework.rest.RestControllerErrorResponse;
import org.avni.server.service.exception.ConstraintViolationExceptionAcrossOrganisations;
import org.avni.server.util.BadRequestError;
import org.avni.server.util.BugsnagReporter;
import org.avni.server.web.util.ErrorBodyBuilder;
Expand Down Expand Up @@ -91,6 +92,14 @@ public ResponseEntity fileUploadSizeLimitExceededError(Exception e) {
return ResponseEntity.badRequest().body(String.format("Maximum upload file size exceeded; ensure file size is less than %s.", maxFileSize));
}

@ExceptionHandler(value = {ConstraintViolationExceptionAcrossOrganisations.class})
public ResponseEntity entityUpsertErrorDueToConstraintViolationAcrossOrganisations(Exception e) {
bugsnagReporter.logAndReportToBugsnag(e);
return ResponseEntity.status(HttpStatus.CONFLICT).body(
String.format("Entity create or update failed due to constraint violation across organisations: %s",
errorBodyBuilder.getErrorMessageBody(e)));
}

@ExceptionHandler(value = {Exception.class})
public ResponseEntity unknownException(Exception e) {
if (e instanceof BadRequestError) {
Expand Down

0 comments on commit 77116c8

Please sign in to comment.