From 02cf26b0e8a6532066d6e387c547e3e3a1a3045e Mon Sep 17 00:00:00 2001
From: Dominika Zemanovicova <xzemanov@fi.muni.cz>
Date: Sun, 7 May 2023 10:05:35 +0200
Subject: [PATCH] Separate errors for finding suitable resolvers

---
 .../RestResponseEntityExceptionHandler.java   | 19 +++++++++++++++++--
 .../RestResponseEntityExceptionHandler.java   | 18 +++++++++++++++++-
 .../RestResponseEntityExceptionHandler.java   | 18 +++++++++++++++++-
 3 files changed, 51 insertions(+), 4 deletions(-)

diff --git a/application/module-certificate/src/main/java/org/fuseri/modulecertificate/exceptions/RestResponseEntityExceptionHandler.java b/application/module-certificate/src/main/java/org/fuseri/modulecertificate/exceptions/RestResponseEntityExceptionHandler.java
index 17ba4421..6d3d2355 100644
--- a/application/module-certificate/src/main/java/org/fuseri/modulecertificate/exceptions/RestResponseEntityExceptionHandler.java
+++ b/application/module-certificate/src/main/java/org/fuseri/modulecertificate/exceptions/RestResponseEntityExceptionHandler.java
@@ -7,7 +7,6 @@ import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.http.converter.HttpMessageNotReadableException;
 import org.springframework.web.bind.MethodArgumentNotValidException;
-import org.springframework.web.bind.MissingServletRequestParameterException;
 import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.util.UrlPathHelper;
@@ -41,7 +40,7 @@ public class RestResponseEntityExceptionHandler {
      * @param request request
      * @return response entity
      */
-    @ExceptionHandler(value = {MethodArgumentNotValidException.class, HttpMessageNotReadableException.class})
+    @ExceptionHandler(value = {MethodArgumentNotValidException.class})
     public ResponseEntity<ApiError> handleValidationErrors(MethodArgumentNotValidException ex, HttpServletRequest request) {
         List<ApiSubError> subErrors = ex.getBindingResult().getFieldErrors()
                 .stream()
@@ -55,6 +54,22 @@ public class RestResponseEntityExceptionHandler {
         return buildResponseEntity(error);
     }
 
+    /**
+     * Handle MessageNotReadable exceptions
+     *
+     * @param ex      exception
+     * @param request request
+     * @return response entity
+     */
+    @ExceptionHandler(value = {HttpMessageNotReadableException.class})
+    public ResponseEntity<ApiError> handleMessageNotReadableErrors(HttpMessageNotReadableException ex, HttpServletRequest request) {
+        ApiError error = new ApiError(
+                HttpStatus.BAD_REQUEST,
+                ex,
+                URL_PATH_HELPER.getRequestUri(request));
+        return buildResponseEntity(error);
+    }
+
     /**
      * Handle exceptions not matched by above handler methods
      *
diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exceptions/RestResponseEntityExceptionHandler.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exceptions/RestResponseEntityExceptionHandler.java
index 04b43812..614139b6 100644
--- a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exceptions/RestResponseEntityExceptionHandler.java
+++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exceptions/RestResponseEntityExceptionHandler.java
@@ -40,7 +40,7 @@ public class RestResponseEntityExceptionHandler {
      * @param request request
      * @return response entity
      */
-    @ExceptionHandler(value = {MethodArgumentNotValidException.class, HttpMessageNotReadableException.class})
+    @ExceptionHandler(value = {MethodArgumentNotValidException.class})
     public ResponseEntity<ApiError> handleValidationErrors(MethodArgumentNotValidException ex, HttpServletRequest request) {
         List<ApiSubError> subErrors = ex.getBindingResult().getFieldErrors()
                 .stream()
@@ -54,6 +54,22 @@ public class RestResponseEntityExceptionHandler {
         return buildResponseEntity(error);
     }
 
+    /**
+     * Handle MessageNotReadable exceptions
+     *
+     * @param ex      exception
+     * @param request request
+     * @return response entity
+     */
+    @ExceptionHandler(value = {HttpMessageNotReadableException.class})
+    public ResponseEntity<ApiError> handleMessageNotReadableErrors(HttpMessageNotReadableException ex, HttpServletRequest request) {
+        ApiError error = new ApiError(
+                HttpStatus.BAD_REQUEST,
+                ex,
+                URL_PATH_HELPER.getRequestUri(request));
+        return buildResponseEntity(error);
+    }
+
     /**
      * Handle exceptions not matched by above handler methods
      *
diff --git a/application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/exceptions/RestResponseEntityExceptionHandler.java b/application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/exceptions/RestResponseEntityExceptionHandler.java
index 3d5ae95b..dd55d243 100644
--- a/application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/exceptions/RestResponseEntityExceptionHandler.java
+++ b/application/module-language-school/src/main/java/org/fuseri/modulelanguageschool/exceptions/RestResponseEntityExceptionHandler.java
@@ -90,7 +90,7 @@ public class RestResponseEntityExceptionHandler {
      * @param request request
      * @return response entity
      */
-    @ExceptionHandler(value = {MethodArgumentNotValidException.class, HttpMessageNotReadableException.class})
+    @ExceptionHandler(value = {MethodArgumentNotValidException.class})
     public ResponseEntity<ApiError> handleValidationErrors(MethodArgumentNotValidException ex, HttpServletRequest request) {
         List<ApiSubError> subErrors = ex.getBindingResult().getFieldErrors()
                 .stream()
@@ -104,6 +104,22 @@ public class RestResponseEntityExceptionHandler {
         return buildResponseEntity(error);
     }
 
+    /**
+     * Handle MessageNotReadable exceptions
+     *
+     * @param ex      exception
+     * @param request request
+     * @return response entity
+     */
+    @ExceptionHandler(value = {HttpMessageNotReadableException.class})
+    public ResponseEntity<ApiError> handleMessageNotReadableErrors(HttpMessageNotReadableException ex, HttpServletRequest request) {
+        ApiError error = new ApiError(
+                HttpStatus.BAD_REQUEST,
+                ex,
+                URL_PATH_HELPER.getRequestUri(request));
+        return buildResponseEntity(error);
+    }
+
     /**
      * Handle exceptions not matched by above handler methods
      *
-- 
GitLab