From 4103ba2e69f2f5af5fb5ec2ee535e19ac98caf9a Mon Sep 17 00:00:00 2001 From: "chris.ditcher" Date: Mon, 9 Sep 2024 13:33:06 -0700 Subject: [PATCH] Updated EntityNotFound handler --- .../api/trax/config/RestErrorHandler.java | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/api/src/main/java/ca/bc/gov/educ/api/trax/config/RestErrorHandler.java b/api/src/main/java/ca/bc/gov/educ/api/trax/config/RestErrorHandler.java index b3584f72..39fd4f06 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/trax/config/RestErrorHandler.java +++ b/api/src/main/java/ca/bc/gov/educ/api/trax/config/RestErrorHandler.java @@ -59,18 +59,6 @@ protected ResponseEntity handleMethodArgumentNotValid(MethodArgumentNotV return buildResponseEntity(apiError); } - /** - * Handles the exception thrown by not found and translates to 404 response - * @param ex the exception - * @return a 404 with message - */ - @ExceptionHandler(value = { EntityNotFoundException.class }) - protected ResponseEntity handleEntityNotFoundException(EntityNotFoundException ex){ - ApiError apiError = new ApiError(NOT_FOUND); - apiError.setMessage(ex.getMessage()); - return buildResponseEntity(apiError); - } - @ExceptionHandler(value = { IllegalArgumentException.class, IllegalStateException.class }) protected ResponseEntity handleConflict(RuntimeException ex) { log.error("Illegal argument ERROR IS: {}", ex.getClass().getName(), ex); @@ -81,11 +69,15 @@ protected ResponseEntity handleConflict(RuntimeException ex) { return new ResponseEntity<>(response, HttpStatus.UNPROCESSABLE_ENTITY); } - @ExceptionHandler(value = { JpaObjectRetrievalFailureException.class, DataRetrievalFailureException.class }) + @ExceptionHandler(value = { JpaObjectRetrievalFailureException.class, DataRetrievalFailureException.class, EntityNotFoundException.class }) protected ResponseEntity handleEntityNotFound(RuntimeException ex) { - log.error("JPA ERROR IS: {}", ex.getClass().getName(), ex); - validation.clear(); - return new ResponseEntity<>(ApiResponseModel.ERROR(null, ex.getLocalizedMessage()), HttpStatus.BAD_REQUEST); + // no need to log EntityNotFoundExceptions as they are expected + if(!(ex instanceof EntityNotFoundException)){ + log.error("JPA ERROR IS: {}", ex.getClass().getName(), ex); + } + ApiError apiError = new ApiError(NOT_FOUND); + apiError.setMessage(ex.getMessage()); + return buildResponseEntity(apiError); } @ExceptionHandler(value = { AccessDeniedException.class })