|
| 1 | +package com.park.utmstack.advice; |
| 2 | + |
| 3 | + |
| 4 | +import com.park.utmstack.security.TooMuchLoginAttemptsException; |
| 5 | +import com.park.utmstack.service.application_events.ApplicationEventService; |
| 6 | +import com.park.utmstack.util.ResponseUtil; |
| 7 | +import com.park.utmstack.util.exceptions.TfaVerificationException; |
| 8 | +import lombok.RequiredArgsConstructor; |
| 9 | +import lombok.extern.slf4j.Slf4j; |
| 10 | +import org.springframework.http.HttpStatus; |
| 11 | +import org.springframework.http.ResponseEntity; |
| 12 | +import org.springframework.security.authentication.BadCredentialsException; |
| 13 | +import org.springframework.web.bind.annotation.ExceptionHandler; |
| 14 | +import org.springframework.web.bind.annotation.RestControllerAdvice; |
| 15 | + |
| 16 | +import javax.servlet.http.HttpServletRequest; |
| 17 | +import java.util.NoSuchElementException; |
| 18 | + |
| 19 | +@Slf4j |
| 20 | +@RestControllerAdvice |
| 21 | +@RequiredArgsConstructor |
| 22 | +public class GlobalExceptionHandler { |
| 23 | + |
| 24 | + private final ApplicationEventService applicationEventService; |
| 25 | + |
| 26 | + @ExceptionHandler(TfaVerificationException.class) |
| 27 | + public ResponseEntity<?> TfaVerificationException(TfaVerificationException e, HttpServletRequest request) { |
| 28 | + return ResponseUtil.buildErrorResponse(HttpStatus.PRECONDITION_FAILED, e.getMessage()); |
| 29 | + } |
| 30 | + |
| 31 | + @ExceptionHandler(BadCredentialsException.class) |
| 32 | + public ResponseEntity<?> handleForbidden(BadCredentialsException e, HttpServletRequest request) { |
| 33 | + return ResponseUtil.buildUnauthorizedResponse(e.getMessage()); |
| 34 | + } |
| 35 | + |
| 36 | + @ExceptionHandler(TooMuchLoginAttemptsException.class) |
| 37 | + public ResponseEntity<?> handleTooManyLoginAttempts(TooMuchLoginAttemptsException e, HttpServletRequest request) { |
| 38 | + return ResponseUtil.buildLockedResponse(e.getMessage()); |
| 39 | + } |
| 40 | + |
| 41 | + @ExceptionHandler(NoSuchElementException.class) |
| 42 | + public ResponseEntity<?> handleNotFound(NoSuchElementException e, HttpServletRequest request) { |
| 43 | + return ResponseUtil.buildNotFoundResponse(e.getMessage()); |
| 44 | + } |
| 45 | + |
| 46 | + @ExceptionHandler(Exception.class) |
| 47 | + public ResponseEntity<?> handleGenericException(Exception e, HttpServletRequest request) { |
| 48 | + return ResponseUtil.buildInternalServerErrorResponse(e.getMessage()); |
| 49 | + } |
| 50 | +} |
0 commit comments