diff --git a/src/main/java/org/springframework/samples/petclinic/rest/advice/ExceptionControllerAdvice.java b/src/main/java/org/springframework/samples/petclinic/rest/advice/ExceptionControllerAdvice.java index a49745d00..b1aa4600e 100644 --- a/src/main/java/org/springframework/samples/petclinic/rest/advice/ExceptionControllerAdvice.java +++ b/src/main/java/org/springframework/samples/petclinic/rest/advice/ExceptionControllerAdvice.java @@ -18,6 +18,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.dao.DataIntegrityViolationException; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -33,23 +34,41 @@ import static org.springframework.http.HttpStatus.BAD_REQUEST; /** + * Global Exception handler for REST controllers. + *
+ * This class handles exceptions thrown by REST controllers and returns
+ * appropriate HTTP responses to the client.
+ *
* @author Vitaliy Fedoriv
+ * @author Alexander Dudkin
*/
-
@ControllerAdvice
public class ExceptionControllerAdvice {
- @ExceptionHandler(Exception.class)
- public ResponseEntity
+ * This record encapsulates the class name and message of the exception.
+ *
+ * @param className The name of the exception class
+ * @param exMessage The message of the exception
+ */
+ private record ErrorInfo(String className, String exMessage) {
+ public ErrorInfo(Exception ex) {
+ this(ex.getClass().getName(), ex.getLocalizedMessage());
}
- return ResponseEntity.badRequest().body(respJSONstring);
+ }
+
+ /**
+ * Handles all general exceptions by returning a 500 Internal Server Error status with error details.
+ *
+ * @param e The exception to be handled
+ * @return A {@link ResponseEntity} containing the error information and a 500 Internal Server Error status
+ */
+ @ExceptionHandler(Exception.class)
+ public ResponseEntity