Skip to content

Commit

Permalink
Added exception handling for MethodArgumentNotValidException and fixe…
Browse files Browse the repository at this point in the history
…d integration tests
  • Loading branch information
vikhyat187 committed Dec 25, 2023
1 parent beec3f0 commit e322ddf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -33,14 +37,28 @@ public ResponseEntity<GenericResponse<Object>> 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<GenericResponse<Object>> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex){
StringBuilder errorString = new StringBuilder();
List<FieldError> 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})
public ResponseEntity<GenericResponse<Object>> 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." ));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit e322ddf

Please sign in to comment.