41. Custom exception handling - Part 3 (#3)
This commit is contained in:
@ -1,14 +1,30 @@
|
|||||||
package net.shyshkin.study.fullstack.supportportal.backend.exception;
|
package net.shyshkin.study.fullstack.supportportal.backend.exception;
|
||||||
|
|
||||||
|
import com.auth0.jwt.exceptions.TokenExpiredException;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.shyshkin.study.fullstack.supportportal.backend.controller.UserResource;
|
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.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.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
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.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.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
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
|
@Slf4j
|
||||||
@RestControllerAdvice(basePackageClasses = {UserResource.class})
|
@RestControllerAdvice(basePackageClasses = {UserResource.class})
|
||||||
public class ExceptionHandling {
|
public class ExceptionHandling {
|
||||||
@ -22,7 +38,59 @@ public class ExceptionHandling {
|
|||||||
|
|
||||||
@ExceptionHandler({DisabledException.class})
|
@ExceptionHandler({DisabledException.class})
|
||||||
public ResponseEntity<HttpResponse> accountDisabledException() {
|
public ResponseEntity<HttpResponse> accountDisabledException() {
|
||||||
return createHttpResponse(HttpStatus.BAD_REQUEST, ACCOUNT_DISABLED);
|
return createHttpResponse(BAD_REQUEST, ACCOUNT_DISABLED);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(BadCredentialsException.class)
|
||||||
|
public ResponseEntity<HttpResponse> badCredentialsException() {
|
||||||
|
return createHttpResponse(BAD_REQUEST, INCORRECT_CREDENTIALS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(AccessDeniedException.class)
|
||||||
|
public ResponseEntity<HttpResponse> accessDeniedException() {
|
||||||
|
return createHttpResponse(FORBIDDEN, NOT_ENOUGH_PERMISSION);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(LockedException.class)
|
||||||
|
public ResponseEntity<HttpResponse> lockedException() {
|
||||||
|
return createHttpResponse(UNAUTHORIZED, ACCOUNT_LOCKED);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(TokenExpiredException.class)
|
||||||
|
public ResponseEntity<HttpResponse> tokenExpiredException(TokenExpiredException exception) {
|
||||||
|
return createHttpResponse(UNAUTHORIZED, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler({
|
||||||
|
EmailExistsException.class, UsernameExistsException.class,
|
||||||
|
EmailNotFoundException.class, UserNotFoundException.class
|
||||||
|
})
|
||||||
|
public ResponseEntity<HttpResponse> badRequestExceptionHandler(Exception exception) {
|
||||||
|
return createHttpResponse(BAD_REQUEST, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
||||||
|
public ResponseEntity<HttpResponse> 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<HttpResponse> internalServerErrorException(Exception exception) {
|
||||||
|
log.error(exception.getMessage());
|
||||||
|
return createHttpResponse(INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR_MSG);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(NoResultException.class)
|
||||||
|
public ResponseEntity<HttpResponse> notFoundException(NoResultException exception) {
|
||||||
|
log.error(exception.getMessage());
|
||||||
|
return createHttpResponse(NOT_FOUND, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(IOException.class)
|
||||||
|
public ResponseEntity<HttpResponse> iOException(IOException exception) {
|
||||||
|
log.error(exception.getMessage());
|
||||||
|
return createHttpResponse(INTERNAL_SERVER_ERROR, ERROR_PROCESSING_FILE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ResponseEntity<HttpResponse> createHttpResponse(HttpStatus httpStatus, String message) {
|
private ResponseEntity<HttpResponse> createHttpResponse(HttpStatus httpStatus, String message) {
|
||||||
|
|||||||
Reference in New Issue
Block a user