-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SMA-91: added global ExceptionHandler
- Loading branch information
1 parent
a689ebe
commit 30df6c9
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
backend/sportsmatch/src/main/java/com/sportsmatch/configs/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |