From e6da46730b92254ee94cff92e50fe1d0577c4c60 Mon Sep 17 00:00:00 2001 From: George Kalampokis Date: Tue, 21 Jul 2020 16:05:34 +0300 Subject: [PATCH] Improved Dataset error handling when retrieving data --- .../main/java/eu/eudat/controllers/Datasets.java | 14 ++++++-------- .../eu/eudat/logic/managers/DatasetManager.java | 6 +++++- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/dmp-backend/web/src/main/java/eu/eudat/controllers/Datasets.java b/dmp-backend/web/src/main/java/eu/eudat/controllers/Datasets.java index 00a7431b9..0385d0022 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/controllers/Datasets.java +++ b/dmp-backend/web/src/main/java/eu/eudat/controllers/Datasets.java @@ -149,17 +149,15 @@ public class Datasets extends BaseController { return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().status(ApiMessageCode.NO_MESSAGE).payload(dataset)); } } catch (Exception e) { + logger.error(e.getMessage(), e); if (e instanceof UnauthorisedException) { - if (e instanceof UnauthorisedException) { - return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new ResponseItem().status(ApiMessageCode.ERROR_MESSAGE)); - } else if (e instanceof NoResultException) { - return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ResponseItem().status(ApiMessageCode.ERROR_MESSAGE)); - } else { - throw e; - } + return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new ResponseItem().status(ApiMessageCode.ERROR_MESSAGE)); + } else if (e instanceof NoResultException) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ResponseItem().status(ApiMessageCode.ERROR_MESSAGE)); + } else { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ResponseItem().status(ApiMessageCode.ERROR_MESSAGE)); } } - return null; // ???? } @RequestMapping(method = RequestMethod.POST, value = {"/datasetProfilesUsedByDatasets/paged"}, produces = "application/json") diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DatasetManager.java b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DatasetManager.java index 725f130e8..70bbea35f 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DatasetManager.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DatasetManager.java @@ -299,7 +299,11 @@ public class DatasetManager { Stream sorted = profileVersionsIncluded.stream().sorted(Comparator.comparing(DatasetProfile::getVersion).reversed()); // Make the Stream into List and get the first item. - DatasetProfile profile = sorted.collect(Collectors.toList()).iterator().next(); + List profiles = sorted.collect(Collectors.toList()); + if (profiles.isEmpty()) + throw new NoSuchElementException("No profiles found for the specific Dataset"); + + DatasetProfile profile = profiles.get(0); // Check if the dataset is on the latest Version. boolean latestVersion = profile.getVersion().toString().equals(datasetEntity.getProfile().getVersion().toString());