generated from Arquisoft/wiq_0
-
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.
- Loading branch information
1 parent
5a0c70c
commit c9091c3
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
api/src/main/java/lab/en2b/quizapi/commons/exceptions/CustomControllerAdvice.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,61 @@ | ||
package lab.en2b.quizapi.commons.exceptions; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
import lombok.extern.log4j.Log4j2; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.authentication.BadCredentialsException; | ||
import org.springframework.security.authentication.InternalAuthenticationServiceException; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
||
@ControllerAdvice | ||
@Log4j2 | ||
@Order(Ordered.HIGHEST_PRECEDENCE) | ||
public class CustomControllerAdvice extends ResponseEntityExceptionHandler { | ||
@ExceptionHandler(NoSuchElementException.class) | ||
public ResponseEntity<String> handleNoSuchElementException(NoSuchElementException exception){ | ||
log.error(exception.getMessage(),exception); | ||
return new ResponseEntity<>(exception.getMessage(),HttpStatus.NOT_FOUND); | ||
} | ||
|
||
@ExceptionHandler(IllegalArgumentException.class) | ||
public ResponseEntity<String> handleIllegalArgumentException(IllegalArgumentException exception){ | ||
log.error(exception.getMessage(),exception); | ||
return new ResponseEntity<>(exception.getMessage(),HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
@ExceptionHandler(BadCredentialsException.class) | ||
public ResponseEntity<String> handleBadCredentialsException(BadCredentialsException exception){ | ||
log.error(exception.getMessage(),exception); | ||
return new ResponseEntity<>(exception.getMessage(),HttpStatus.UNAUTHORIZED); | ||
} | ||
|
||
@ExceptionHandler(AccessDeniedException.class) | ||
public ResponseEntity<String> handleAccessDeniedException(AccessDeniedException exception){ | ||
log.error(exception.getMessage(),exception); | ||
return new ResponseEntity<>(exception.getMessage(), HttpStatus.FORBIDDEN); | ||
} | ||
|
||
@ExceptionHandler(TokenRefreshException.class) | ||
public ResponseEntity<String> handleTokenRefreshException(TokenRefreshException exception) { | ||
log.error(exception.getMessage(),exception); | ||
return new ResponseEntity<>(exception.getMessage(),HttpStatus.FORBIDDEN); | ||
} | ||
@ExceptionHandler(InternalAuthenticationServiceException.class) | ||
public ResponseEntity<String> handleInternalAuthenticationServiceException(InternalAuthenticationServiceException exception) { | ||
log.error(exception.getMessage(),exception); | ||
return new ResponseEntity<>(exception.getMessage(),HttpStatus.FORBIDDEN); | ||
} | ||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<String> handleException(Exception exception){ | ||
log.error(exception.getMessage(),exception); | ||
return new ResponseEntity<>(exception.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
} |