40. Custom exception handling - Part 2 (#3)

This commit is contained in:
Art
2021-09-06 15:13:59 +03:00
parent cefef9bd11
commit 411e67bb7d

View File

@ -2,6 +2,11 @@ package net.shyshkin.study.fullstack.supportportal.backend.exception;
import lombok.extern.slf4j.Slf4j;
import net.shyshkin.study.fullstack.supportportal.backend.controller.UserResource;
import net.shyshkin.study.fullstack.supportportal.backend.domain.HttpResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.DisabledException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@ -11,7 +16,22 @@ public class ExceptionHandling {
private static final String METHOD_IS_NOT_ALLOWED = "This request method is not allowed on this endpoint. Please send a '%s' request";
private static final String INTERNAL_SERVER_ERROR_MSG = "An error occurred while processing the request";
private static final String INCORRECT_CREDENTIALS = "Username / password incorrect. Please try again";
private static final String ACCOUNT_DISABLED ="Your account has been disabled. If this is an error, please contact administration";
private static final String ACCOUNT_DISABLED = "Your account has been disabled. If this is an error, please contact administration";
private static final String ERROR_PROCESSING_FILE = "Error occurred while processing file";
private static final String NOT_ENOUGH_PERMISSION = "You do not have enough permission";
@ExceptionHandler({DisabledException.class})
public ResponseEntity<HttpResponse> accountDisabledException() {
return createHttpResponse(HttpStatus.BAD_REQUEST, ACCOUNT_DISABLED);
}
private ResponseEntity<HttpResponse> createHttpResponse(HttpStatus httpStatus, String message) {
HttpResponse httpResponse = HttpResponse.builder()
.httpStatus(httpStatus)
.httpStatusCode(httpStatus.value())
.reason(httpStatus.getReasonPhrase().toUpperCase())
.message(message.toUpperCase())
.build();
return new ResponseEntity<>(httpResponse, httpStatus);
}
}