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

API: Catch database errors and wrap them as errors

parent 3f003703
......@@ -54,6 +54,7 @@ import org.genesys2.spring.ResourceNotFoundException;
import org.genesys2.util.InvalidDOIException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.CannotAcquireLockException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
......@@ -244,7 +245,7 @@ public class AccessionController extends RestController {
List<AccessionOpResponse> response = null;
try {
response = batchRESTService.upsertAccessionData(institute, batch);
} catch (RESTApiException e) {
} catch (RESTApiException | DataIntegrityViolationException e) {
LOG.info("Retrying upsert one by one due to {}", e.getMessage());
response = upsertAccessionData1by1(institute, batch);
}
......@@ -280,19 +281,28 @@ public class AccessionController extends RestController {
AccessionOpResponse accessionResponse = batchRESTService.upsertAccessionData(institute, batchOfOne).get(0);
response.add(accessionResponse);
} catch (RESTApiException e) {
} catch (RESTApiException | DataIntegrityViolationException e) {
if (LOG.isInfoEnabled()) {
LOG.info("Error upserting {}: {}", acceJ.instCode, e.getMessage());
}
AccessionOpResponse accessionResponse = new AccessionOpResponse(acceJ.instCode, acceJ.acceNumb, acceJ.genus);
accessionResponse.setError(e.getMessage());
accessionResponse.setError(getDetailedErrorMessage(e));
response.add(accessionResponse);
}
}
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
*
......@@ -348,7 +358,7 @@ public class AccessionController extends RestController {
}
AccessionOpResponse accessionResponse = new AccessionOpResponse(acceJ.instCode, acceJ.acceNumb, acceJ.genus);
accessionResponse.setError(e.getMessage());
accessionResponse.setError(getDetailedErrorMessage(e));
response.add(accessionResponse);
}
}
......@@ -412,7 +422,7 @@ public class AccessionController extends RestController {
}
AccessionOpResponse accessionResponse = new AccessionOpResponse(acceJ.instCode, acceJ.acceNumb, acceJ.genus);
accessionResponse.setError(e.getMessage());
accessionResponse.setError(getDetailedErrorMessage(e));
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