diff --git a/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/exception/ExceptionHandling.java b/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/exception/ExceptionHandling.java index 095a379..bde1e09 100644 --- a/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/exception/ExceptionHandling.java +++ b/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/exception/ExceptionHandling.java @@ -1,14 +1,30 @@ package net.shyshkin.study.fullstack.supportportal.backend.exception; +import com.auth0.jwt.exceptions.TokenExpiredException; import lombok.extern.slf4j.Slf4j; import net.shyshkin.study.fullstack.supportportal.backend.controller.UserResource; import net.shyshkin.study.fullstack.supportportal.backend.domain.HttpResponse; +import net.shyshkin.study.fullstack.supportportal.backend.exception.domain.EmailExistsException; +import net.shyshkin.study.fullstack.supportportal.backend.exception.domain.EmailNotFoundException; +import net.shyshkin.study.fullstack.supportportal.backend.exception.domain.UserNotFoundException; +import net.shyshkin.study.fullstack.supportportal.backend.exception.domain.UsernameExistsException; +import org.springframework.http.HttpMethod; 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.DisabledException; +import org.springframework.security.authentication.LockedException; +import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; +import javax.persistence.NoResultException; +import java.io.IOException; +import java.util.Objects; + +import static org.springframework.http.HttpStatus.*; + @Slf4j @RestControllerAdvice(basePackageClasses = {UserResource.class}) public class ExceptionHandling { @@ -22,7 +38,59 @@ public class ExceptionHandling { @ExceptionHandler({DisabledException.class}) public ResponseEntity accountDisabledException() { - return createHttpResponse(HttpStatus.BAD_REQUEST, ACCOUNT_DISABLED); + return createHttpResponse(BAD_REQUEST, ACCOUNT_DISABLED); + } + + @ExceptionHandler(BadCredentialsException.class) + public ResponseEntity badCredentialsException() { + return createHttpResponse(BAD_REQUEST, INCORRECT_CREDENTIALS); + } + + @ExceptionHandler(AccessDeniedException.class) + public ResponseEntity accessDeniedException() { + return createHttpResponse(FORBIDDEN, NOT_ENOUGH_PERMISSION); + } + + @ExceptionHandler(LockedException.class) + public ResponseEntity lockedException() { + return createHttpResponse(UNAUTHORIZED, ACCOUNT_LOCKED); + } + + @ExceptionHandler(TokenExpiredException.class) + public ResponseEntity tokenExpiredException(TokenExpiredException exception) { + return createHttpResponse(UNAUTHORIZED, exception.getMessage()); + } + + @ExceptionHandler({ + EmailExistsException.class, UsernameExistsException.class, + EmailNotFoundException.class, UserNotFoundException.class + }) + public ResponseEntity badRequestExceptionHandler(Exception exception) { + return createHttpResponse(BAD_REQUEST, exception.getMessage()); + } + + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) + public ResponseEntity methodNotSupportedException(HttpRequestMethodNotSupportedException exception) { + HttpMethod supportedMethod = Objects.requireNonNull(exception.getSupportedHttpMethods()).iterator().next(); + return createHttpResponse(METHOD_NOT_ALLOWED, String.format(METHOD_IS_NOT_ALLOWED, supportedMethod)); + } + + @ExceptionHandler(Exception.class) + public ResponseEntity internalServerErrorException(Exception exception) { + log.error(exception.getMessage()); + return createHttpResponse(INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR_MSG); + } + + @ExceptionHandler(NoResultException.class) + public ResponseEntity notFoundException(NoResultException exception) { + log.error(exception.getMessage()); + return createHttpResponse(NOT_FOUND, exception.getMessage()); + } + + @ExceptionHandler(IOException.class) + public ResponseEntity iOException(IOException exception) { + log.error(exception.getMessage()); + return createHttpResponse(INTERNAL_SERVER_ERROR, ERROR_PROCESSING_FILE); } private ResponseEntity createHttpResponse(HttpStatus httpStatus, String message) {