diff --git a/backend/sportsmatch/src/main/java/com/sportsmatch/configs/GlobalExceptionHandler.java b/backend/sportsmatch/src/main/java/com/sportsmatch/configs/GlobalExceptionHandler.java new file mode 100644 index 00000000..e08e8da1 --- /dev/null +++ b/backend/sportsmatch/src/main/java/com/sportsmatch/configs/GlobalExceptionHandler.java @@ -0,0 +1,33 @@ +package com.sportsmatch.configs; + +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.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +import javax.security.sasl.AuthenticationException; + +@ControllerAdvice +public class GlobalExceptionHandler { + + // handle request body + @ExceptionHandler(HttpMessageNotReadableException.class) + public ResponseEntity handleHttpMessageNotReadableException( + HttpMessageNotReadableException e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); + } + + // handle parameter with @Valid + @ExceptionHandler(MethodArgumentNotValidException.class) + public ResponseEntity handleMethodArgumentNotValidException( + MethodArgumentNotValidException e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); + } + + @ExceptionHandler(AuthenticationException.class) + public ResponseEntity handleAuthenticationException(AuthenticationException e) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.getMessage()); + } +}