Commit 552e6723 authored by Matija Obreza's avatar Matija Obreza
Browse files

API: Catch database errors and wrap them as errors

parent 3f003703
...@@ -54,6 +54,7 @@ import org.genesys2.spring.ResourceNotFoundException; ...@@ -54,6 +54,7 @@ import org.genesys2.spring.ResourceNotFoundException;
import org.genesys2.util.InvalidDOIException; import org.genesys2.util.InvalidDOIException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.CannotAcquireLockException; import org.springframework.dao.CannotAcquireLockException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
...@@ -244,7 +245,7 @@ public class AccessionController extends RestController { ...@@ -244,7 +245,7 @@ public class AccessionController extends RestController {
List<AccessionOpResponse> response = null; List<AccessionOpResponse> response = null;
try { try {
response = batchRESTService.upsertAccessionData(institute, batch); response = batchRESTService.upsertAccessionData(institute, batch);
} catch (RESTApiException e) { } catch (RESTApiException | DataIntegrityViolationException e) {
LOG.info("Retrying upsert one by one due to {}", e.getMessage()); LOG.info("Retrying upsert one by one due to {}", e.getMessage());
response = upsertAccessionData1by1(institute, batch); response = upsertAccessionData1by1(institute, batch);
} }
...@@ -280,19 +281,28 @@ public class AccessionController extends RestController { ...@@ -280,19 +281,28 @@ public class AccessionController extends RestController {
AccessionOpResponse accessionResponse = batchRESTService.upsertAccessionData(institute, batchOfOne).get(0); AccessionOpResponse accessionResponse = batchRESTService.upsertAccessionData(institute, batchOfOne).get(0);
response.add(accessionResponse); response.add(accessionResponse);
} catch (RESTApiException e) { } catch (RESTApiException | DataIntegrityViolationException e) {
if (LOG.isInfoEnabled()) { if (LOG.isInfoEnabled()) {
LOG.info("Error upserting {}: {}", acceJ.instCode, e.getMessage()); LOG.info("Error upserting {}: {}", acceJ.instCode, e.getMessage());
} }
AccessionOpResponse accessionResponse = new AccessionOpResponse(acceJ.instCode, acceJ.acceNumb, acceJ.genus); AccessionOpResponse accessionResponse = new AccessionOpResponse(acceJ.instCode, acceJ.acceNumb, acceJ.genus);
accessionResponse.setError(e.getMessage()); accessionResponse.setError(getDetailedErrorMessage(e));
response.add(accessionResponse); response.add(accessionResponse);
} }
} }
return response; return response;
} }
private String getDetailedErrorMessage(Throwable e) {
StringBuffer sb=new StringBuffer(e.getMessage());
Throwable r = e;
while ((r = r.getCause()) !=null) {
sb.append("\n" + r.getMessage());
}
return sb.toString();
}
/** /**
* Update accessions in the system * Update accessions in the system
* *
...@@ -348,7 +358,7 @@ public class AccessionController extends RestController { ...@@ -348,7 +358,7 @@ public class AccessionController extends RestController {
} }
AccessionOpResponse accessionResponse = new AccessionOpResponse(acceJ.instCode, acceJ.acceNumb, acceJ.genus); AccessionOpResponse accessionResponse = new AccessionOpResponse(acceJ.instCode, acceJ.acceNumb, acceJ.genus);
accessionResponse.setError(e.getMessage()); accessionResponse.setError(getDetailedErrorMessage(e));
response.add(accessionResponse); response.add(accessionResponse);
} }
} }
...@@ -412,7 +422,7 @@ public class AccessionController extends RestController { ...@@ -412,7 +422,7 @@ public class AccessionController extends RestController {
} }
AccessionOpResponse accessionResponse = new AccessionOpResponse(acceJ.instCode, acceJ.acceNumb, acceJ.genus); AccessionOpResponse accessionResponse = new AccessionOpResponse(acceJ.instCode, acceJ.acceNumb, acceJ.genus);
accessionResponse.setError(e.getMessage()); accessionResponse.setError(getDetailedErrorMessage(e));
response.add(accessionResponse); response.add(accessionResponse);
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment