package ru.ulstu.core.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.validation.FieldError; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ModelAttribute; import ru.ulstu.core.error.EntityIdIsNullException; import ru.ulstu.core.model.ErrorConstants; import ru.ulstu.core.model.response.Response; import ru.ulstu.core.model.response.ResponseExtended; import ru.ulstu.user.error.UserActivationError; import ru.ulstu.user.error.UserEmailExistsException; import ru.ulstu.user.error.UserIdExistsException; import ru.ulstu.user.error.UserIsUndeadException; import ru.ulstu.user.error.UserLoginExistsException; import ru.ulstu.user.error.UserNotActivatedException; import ru.ulstu.user.error.UserNotFoundException; import ru.ulstu.user.error.UserPasswordsNotValidOrNotMatchException; import ru.ulstu.user.error.UserResetKeyError; import ru.ulstu.user.service.UserService; import java.util.Set; import java.util.stream.Collectors; @ControllerAdvice public class AdviceController { private final Logger log = LoggerFactory.getLogger(AdviceController.class); private final UserService userService; public AdviceController(UserService userService) { this.userService = userService; } @ModelAttribute("currentUser") public String getCurrentUser() { return userService.getCurrentUser().getUserAbbreviate(); } @ModelAttribute("flashMessage") public String getFlashMessage() { return null; } private Response handleException(ErrorConstants error) { log.warn(error.toString()); return new Response<>(error); } private ResponseExtended handleException(ErrorConstants error, E errorData) { log.warn(error.toString()); return new ResponseExtended<>(error, errorData); } @ExceptionHandler(EntityIdIsNullException.class) public Response handleEntityIdIsNullException(Throwable e) { return handleException(ErrorConstants.ID_IS_NULL); } @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseExtended> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { final Set errors = e.getBindingResult().getAllErrors().stream() .filter(error -> error instanceof FieldError) .map(error -> ((FieldError) error).getField()) .collect(Collectors.toSet()); return handleException(ErrorConstants.VALIDATION_ERROR, errors); } @ExceptionHandler(UserIdExistsException.class) public Response handleUserIdExistsException(Throwable e) { return handleException(ErrorConstants.USER_ID_EXISTS); } @ExceptionHandler(UserActivationError.class) public ResponseExtended handleUserActivationError(Throwable e) { return handleException(ErrorConstants.USER_ACTIVATION_ERROR, e.getMessage()); } @ExceptionHandler(UserLoginExistsException.class) public ResponseExtended handleUserLoginExistsException(Throwable e) { return handleException(ErrorConstants.USER_LOGIN_EXISTS, e.getMessage()); } @ExceptionHandler(UserEmailExistsException.class) public ResponseExtended handleUserEmailExistsException(Throwable e) { return handleException(ErrorConstants.USER_EMAIL_EXISTS, e.getMessage()); } @ExceptionHandler(UserPasswordsNotValidOrNotMatchException.class) public Response handleUserPasswordsNotValidOrNotMatchException(Throwable e) { return handleException(ErrorConstants.USER_PASSWORDS_NOT_VALID_OR_NOT_MATCH); } @ExceptionHandler(UserNotFoundException.class) public ResponseExtended handleUserNotFoundException(Throwable e) { return handleException(ErrorConstants.USER_NOT_FOUND, e.getMessage()); } @ExceptionHandler(UserNotActivatedException.class) public Response handleUserNotActivatedException(Throwable e) { return handleException(ErrorConstants.USER_NOT_ACTIVATED); } @ExceptionHandler(UserResetKeyError.class) public ResponseExtended handleUserResetKeyError(Throwable e) { return handleException(ErrorConstants.USER_RESET_ERROR, e.getMessage()); } @ExceptionHandler(UserIsUndeadException.class) public ResponseExtended handleUserIsUndeadException(Throwable e) { return handleException(ErrorConstants.USER_UNDEAD_ERROR, e.getMessage()); } }