From 197be103d02db808b0e6bf8a1d1369e3d7928c03 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Sun, 30 Mar 2025 20:48:22 -0400 Subject: Modified controllers to return error text when catching errors. Also added additional error checking to CupboardService for physical needs. --- .../api/ufundapi/controller/AuthController.java | 6 +-- .../ufundapi/controller/CupboardController.java | 54 +++++++++++----------- .../api/ufundapi/controller/UserController.java | 26 +++++------ 3 files changed, 43 insertions(+), 43 deletions(-) (limited to 'ufund-api/src/main/java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java index aa99a90..82b2c67 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/AuthController.java @@ -43,10 +43,10 @@ public class AuthController { return new ResponseEntity<>(key, HttpStatus.OK); } catch (IllegalAccessException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.UNAUTHORIZED); } catch (IOException ex) { LOG.log(Level.SEVERE, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -64,7 +64,7 @@ public class AuthController { return new ResponseEntity<>(HttpStatus.OK); } catch (IOException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java index d426aee..cce016c 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java @@ -51,7 +51,7 @@ public class CupboardController { * INTERNAL_SERVER_ERROR otherwise */ @PostMapping("") - public ResponseEntity createNeed(@RequestBody Map params, @RequestHeader("jelly-api-key") String key) { + public ResponseEntity createNeed(@RequestBody Map params, @RequestHeader("jelly-api-key") String key) { LOG.log(Level.INFO, "POST /cupboard body={0}", params); String name = (String) params.get("name"); @@ -66,16 +66,16 @@ public class CupboardController { return new ResponseEntity<>(need, HttpStatus.OK); } catch (DuplicateKeyException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.CONFLICT); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.CONFLICT); } catch (IllegalArgumentException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST); } catch (IllegalAccessException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.UNAUTHORIZED); } catch (IOException ex) { LOG.log(Level.SEVERE, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -88,15 +88,15 @@ public class CupboardController { * ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise */ @GetMapping("") - public ResponseEntity getNeeds() { + public ResponseEntity getNeeds() { LOG.info("GET /cupboard"); try { Need[] needs = cupboardService.getNeeds(); return new ResponseEntity<>(needs, HttpStatus.OK); - } catch (IOException e) { - LOG.log(Level.SEVERE, e.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } catch (IOException ex) { + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -112,15 +112,15 @@ public class CupboardController { *

*/ @GetMapping("/") - public ResponseEntity searchNeeds(@RequestParam String name) { + public ResponseEntity searchNeeds(@RequestParam String name) { LOG.info("GET /cupboard/?name="+name); try { Need[] needs = cupboardService.searchNeeds(name); return new ResponseEntity<>(needs, HttpStatus.OK); - } catch (IOException e) { - LOG.log(Level.SEVERE,e.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } catch (IOException ex) { + LOG.log(Level.SEVERE,ex.getLocalizedMessage()); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -133,7 +133,7 @@ public class CupboardController { * ResponseEntity with HTTP status of NOT_FOUND if not found
*/ @GetMapping("/{id}") - public ResponseEntity getNeed(@PathVariable int id) { + public ResponseEntity getNeed(@PathVariable int id) { LOG.log(Level.INFO, "GET /cupboard/{0}", id); try { @@ -143,9 +143,9 @@ public class CupboardController { } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } - } catch (IOException e) { - LOG.log(Level.SEVERE, e.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } catch (IOException ex) { + LOG.log(Level.SEVERE, ex.getLocalizedMessage()); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -157,7 +157,7 @@ public class CupboardController { * @return OK response and the need if it was successful, or INTERNAL_SERVER_ERROR if there was an issue */ @PutMapping("/{id}") - public ResponseEntity updateNeed(@RequestBody Need need, @PathVariable int id, @RequestHeader("jelly-api-key") String key) { + public ResponseEntity updateNeed(@RequestBody Need need, @PathVariable int id, @RequestHeader("jelly-api-key") String key) { LOG.log(Level.INFO, "PUT /cupboard/{0} body={1}", of(id, need)); try { authService.keyHasAccessToCupboard(key); @@ -169,13 +169,13 @@ public class CupboardController { } } catch (IllegalArgumentException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST); } catch (IllegalAccessException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.UNAUTHORIZED); } catch (IOException ex) { LOG.log(Level.SEVERE, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -196,13 +196,13 @@ public class CupboardController { return new ResponseEntity<>(HttpStatus.OK); } catch (IllegalArgumentException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST); } catch (IllegalAccessException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.UNAUTHORIZED); } catch (IOException ex) { LOG.log(Level.SEVERE, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -213,7 +213,7 @@ public class CupboardController { * @return OK if the need was deleted, NOT_FOUND if the need was not found, or INTERNAL_SERVER_ERROR if an error occurred */ @DeleteMapping("/{id}") - public ResponseEntity deleteNeed(@PathVariable int id, @RequestHeader("jelly-api-key") String key) { + public ResponseEntity deleteNeed(@PathVariable int id, @RequestHeader("jelly-api-key") String key) { LOG.log(Level.INFO, "DELETE /cupboard/{0}", id); try { authService.keyHasAccessToCupboard(key); @@ -225,10 +225,10 @@ public class CupboardController { } } catch (IllegalAccessException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.UNAUTHORIZED); } catch (IOException ex) { LOG.log(Level.SEVERE, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java index 33d2e4f..a34e891 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java @@ -41,7 +41,7 @@ public class UserController { * otherwise */ @PostMapping("") - public ResponseEntity createUser(@RequestBody Map params) { + public ResponseEntity createUser(@RequestBody Map params) { LOG.log(Level.INFO, "POST /users body={0}", params); String username = params.get("username"); String password = params.get("password"); @@ -55,10 +55,10 @@ public class UserController { } } catch (DuplicateKeyException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.CONFLICT); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.CONFLICT); } catch (IOException ex) { LOG.log(Level.SEVERE, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -73,7 +73,7 @@ public class UserController { * ResponseEntity with HTTP status of INTERNAL_SERVER_ERROR otherwise */ @GetMapping("/{username}") - public ResponseEntity getUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) { + public ResponseEntity getUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) { LOG.log(Level.INFO, "GET /user/{0} key={1}", of(username, key)); try { @@ -86,10 +86,10 @@ public class UserController { } } catch (IllegalAccessException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.UNAUTHORIZED); } catch (IOException ex) { LOG.log(Level.SEVERE, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -104,7 +104,7 @@ public class UserController { * INTERNAL_SERVER_ERROR if there was an issue */ @PutMapping("/{username}") - public ResponseEntity updateUser(@RequestBody User user, @PathVariable String username, @RequestHeader("jelly-api-key") String key) { + public ResponseEntity updateUser(@RequestBody User user, @PathVariable String username, @RequestHeader("jelly-api-key") String key) { LOG.log(Level.INFO,"PUT /users/{0} body={1} key={2}", of(username, user, key)); try { authService.keyHasAccessToUser(username, key); @@ -116,13 +116,13 @@ public class UserController { } } catch (IllegalArgumentException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST); } catch (IllegalAccessException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.UNAUTHORIZED); } catch (IOException ex) { LOG.log(Level.SEVERE, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -135,7 +135,7 @@ public class UserController { * INTERNAL_SERVER_ERROR if an error occurred */ @DeleteMapping("/{username}") - public ResponseEntity deleteUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) { + public ResponseEntity deleteUser(@PathVariable String username, @RequestHeader("jelly-api-key") String key) { LOG.log(Level.INFO, "DELETE /users/{0} id={1}", of(username, key)); try { @@ -147,10 +147,10 @@ public class UserController { } } catch (IllegalAccessException ex) { LOG.log(Level.WARNING, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.UNAUTHORIZED); } catch (IOException ex) { LOG.log(Level.SEVERE, ex.getLocalizedMessage()); - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } -- cgit v1.2.3 From eb09f05ca697a3a6d3587f9278e332056bfd6f66 Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Sun, 30 Mar 2025 20:48:32 -0400 Subject: Modified controllers to return error text when catching errors. Also added additional error checking to CupboardService for physical needs. --- .../src/main/java/com/ufund/api/ufundapi/service/CupboardService.java | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'ufund-api/src/main/java') diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java index 4dcfcad..a86fe28 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java @@ -35,6 +35,8 @@ public class CupboardService { if (maxGoal <= 0) { throw new IllegalArgumentException("Max Goal must be greater than zero"); + } else if (goalType.equals(Need.GoalType.PHYSICAL) && maxGoal % 1 != 0) { + throw new IllegalArgumentException("Cannot have non whole number value for physical goal"); } for (Need searchNeed : cupboardDAO.getNeeds()) { @@ -95,6 +97,8 @@ public class CupboardService { } if (need.getMaxGoal() <= 0) { throw new IllegalArgumentException("Goal must be greater than 0"); + } else if (need.getType().equals(Need.GoalType.PHYSICAL) && need.getMaxGoal() % 1 != 0) { + throw new IllegalArgumentException("Cannot have non whole number value for physical goal"); } return cupboardDAO.updateNeed(need); } -- cgit v1.2.3