Class GlobalExceptionHandler
java.lang.Object
org.ntnu.idi.idatt2106.sparesti.sparestibackend.exception.GlobalExceptionHandler
Global exception handler to handle different types of exceptions across the application. It provides centralized
exception handling for various types of exceptions that may occur during the execution of the application.
- Since:
- 17.4.24
- Version:
- 1.0
- Author:
- Harry L.X and Lars M.L.N
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.http.ResponseEntity
<String> Exception handler method for BadCredentialsExceptionorg.springframework.http.ResponseEntity
<String> Handle exceptions related to bad input or invalid requests.org.springframework.http.ResponseEntity
<String> handleConstraintViolationException
(jakarta.validation.ConstraintViolationException ex) Handle exceptions related to constraint violations in validation.org.springframework.http.ResponseEntity
<String> Handle exceptions related to existing objects.org.springframework.http.ResponseEntity
<String> Handle exceptions related to non-existing objects.Handle exception for invalid objects
-
Constructor Details
-
GlobalExceptionHandler
public GlobalExceptionHandler()Default constructor
-
-
Method Details
-
handleObjectAlreadyExistException
@ExceptionHandler({UserAlreadyExistsException.class,ChallengeConfigAlreadyExistsException.class,AccountAlreadyExistsException.class}) public org.springframework.http.ResponseEntity<String> handleObjectAlreadyExistException(Exception ex) Handle exceptions related to existing objects.- Parameters:
ex
- The exception indicating that an object already exists.- Returns:
- ResponseEntity with an appropriate HTTP status code and error message.
-
handleObjectDoesNotExistException
@ExceptionHandler({UserNotFoundException.class,org.springframework.security.core.userdetails.UsernameNotFoundException.class,GoalNotFoundException.class,ChallengeConfigNotFoundException.class,ChallengeNotFoundException.class,ConfigNotFoundException.class,AccountNotFoundException.class,AssertionRequestNotFoundException.class,EmailNotFoundException.class}) public org.springframework.http.ResponseEntity<String> handleObjectDoesNotExistException(Exception ex) Handle exceptions related to non-existing objects.- Parameters:
ex
- The exception indicating that an object does not exist.- Returns:
- ResponseEntity with an appropriate HTTP status code and error message.
-
handleBadInputException
@ExceptionHandler({java.lang.IllegalArgumentException.class,org.springframework.http.converter.HttpMessageNotReadableException.class,BadInputException.class,java.lang.NullPointerException.class,org.springframework.web.bind.MissingServletRequestParameterException.class,org.springframework.web.HttpRequestMethodNotSupportedException.class,javax.mail.MessagingException.class,org.springframework.web.bind.MethodArgumentNotValidException.class,ActiveGoalLimitExceededException.class,org.springframework.dao.DataIntegrityViolationException.class,ChallengeAlreadyCompletedException.class,ActiveGoalLimitExceededException.class,NotActiveGoalException.class,StorageException.class,StorageFileNotFoundException.class}) public org.springframework.http.ResponseEntity<String> handleBadInputException(Exception ex) Handle exceptions related to bad input or invalid requests.- Parameters:
ex
- The exception indicating bad input or invalid request.- Returns:
- ResponseEntity with an appropriate HTTP status code and error message.
-
handleBadCredentialsException
@ExceptionHandler(org.springframework.security.authentication.BadCredentialsException.class) public org.springframework.http.ResponseEntity<String> handleBadCredentialsException(Exception ex) Exception handler method for BadCredentialsException- Parameters:
ex
- Exception- Returns:
- ResponseEntity with error message
-
handleValidationException
@ExceptionHandler(ObjectNotValidException.class) public org.springframework.http.ResponseEntity<Set<String>> handleValidationException(ObjectNotValidException ex) Handle exception for invalid objects- Parameters:
ex
- ObjectNotValid exception- Returns:
- ResponseEntity with bad request response code containing all violations
-
handleConstraintViolationException
@ExceptionHandler(jakarta.validation.ConstraintViolationException.class) @ResponseBody public org.springframework.http.ResponseEntity<String> handleConstraintViolationException(jakarta.validation.ConstraintViolationException ex) Handle exceptions related to constraint violations in validation.- Parameters:
ex
- The exception indicating constraint violations during validation.- Returns:
- ResponseEntity with an appropriate HTTP status code and error message.
-