From e322ddfafa8be2caff9d84eddabad3f987023d7f Mon Sep 17 00:00:00 2001 From: vikhyat187 Date: Mon, 25 Dec 2023 22:42:18 +0530 Subject: [PATCH] Added exception handling for MethodArgumentNotValidException and fixed integration tests --- .../utils/GlobalExceptionHandler.java | 22 +++++++++++++++++-- .../RDS/skilltree/SkillsIntegrationTests.java | 9 +++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/skill-tree/src/main/java/com/RDS/skilltree/utils/GlobalExceptionHandler.java b/skill-tree/src/main/java/com/RDS/skilltree/utils/GlobalExceptionHandler.java index 2c6b464f..718283b2 100644 --- a/skill-tree/src/main/java/com/RDS/skilltree/utils/GlobalExceptionHandler.java +++ b/skill-tree/src/main/java/com/RDS/skilltree/utils/GlobalExceptionHandler.java @@ -6,9 +6,13 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.validation.FieldError; +import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; +import java.util.List; + @ControllerAdvice @Slf4j public class GlobalExceptionHandler { @@ -33,7 +37,21 @@ public ResponseEntity> handleRuntimeException(RuntimeExc log.error("Runtime Exception - Error : {}", ex.getMessage(), ex); return ResponseEntity .status(HttpStatus.INTERNAL_SERVER_ERROR) - .body(new GenericResponse<>(null, "Something went wrong, please try communicating on `#wg-skill-tree discord` channel" )); + .body(new GenericResponse<>(null, "Something went wrong, please try again." )); + } + + @ExceptionHandler({MethodArgumentNotValidException.class}) + public ResponseEntity> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex){ + StringBuilder errorString = new StringBuilder(); + List fieldErrors = ((MethodArgumentNotValidException) ex).getBindingResult().getFieldErrors(); + for(FieldError fieldError: fieldErrors){ + errorString.append(fieldError.getDefaultMessage()); + errorString.append(" "); + } + log.error("MethodArgumentNotValidException Exception - Error : {}", ex.getMessage(), ex); + return ResponseEntity + .status(HttpStatus.BAD_REQUEST) + .body(new GenericResponse<>(null, errorString.toString())); } @ExceptionHandler({Exception.class}) @@ -41,6 +59,6 @@ public ResponseEntity> handleException(Exception ex){ log.error("Exception - Error : {}", ex.getMessage(), ex); return ResponseEntity .status(HttpStatus.INTERNAL_SERVER_ERROR) - .body(new GenericResponse<>(null, "Something went wrong, please try communicating on `#wg-skill-tree discord` channel" )); + .body(new GenericResponse<>(null, "Something went wrong, please try again." )); } } diff --git a/skill-tree/src/test/java/com/RDS/skilltree/SkillsIntegrationTests.java b/skill-tree/src/test/java/com/RDS/skilltree/SkillsIntegrationTests.java index 7c50081b..39ae2d6a 100644 --- a/skill-tree/src/test/java/com/RDS/skilltree/SkillsIntegrationTests.java +++ b/skill-tree/src/test/java/com/RDS/skilltree/SkillsIntegrationTests.java @@ -172,7 +172,8 @@ public void testAPIReturns400_OnCreatedByNotPassedForSKillCreation() { response.then() .statusCode(400) - .body("error", equalTo("Bad Request")); + .body("data",equalTo(null)) + .body("message", equalTo("Created by user Id cannot be null ")); } @Test @@ -190,7 +191,8 @@ public void testAPIReturns400_OnTypeNotPassedForSkillCreation() { response.then() .statusCode(400) - .body("error", equalTo("Bad Request")); //Todo change this on introducing the global exception handling + .body("data",equalTo(null)) + .body("message", equalTo("SkillType cannot be null ")); } @Test @@ -208,7 +210,8 @@ public void testAPIReturns400_OnNameNotPassedForSkillCreation() { response.then() .statusCode(400) - .body("error", equalTo("Bad Request")); + .body("data",equalTo(null)) + .body("message", equalTo("Name cannot be null ")); } @Test