diff --git a/src/main/java/dev/struchkov/example/controlleradvice/controller/ExceptionApiHandler.java b/src/main/java/dev/struchkov/example/controlleradvice/controller/ExceptionApiHandler.java new file mode 100644 index 0000000..5838ace --- /dev/null +++ b/src/main/java/dev/struchkov/example/controlleradvice/controller/ExceptionApiHandler.java @@ -0,0 +1,28 @@ +package dev.struchkov.example.controlleradvice.controller; + +import dev.struchkov.example.controlleradvice.domain.ErrorMessage; +import dev.struchkov.example.controlleradvice.exception.NotFoundException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; + +@RestControllerAdvice +public class ExceptionApiHandler { + + @ExceptionHandler(NotFoundException.class) + public ResponseEntity authException(NotFoundException exception) { + return ResponseEntity + .status(HttpStatus.NOT_FOUND) + .body(new ErrorMessage(exception.getMessage())); + } + + @ExceptionHandler(MethodArgumentTypeMismatchException.class) + public ResponseEntity authException(MethodArgumentTypeMismatchException exception) { + return ResponseEntity + .status(HttpStatus.NOT_FOUND) + .body(new ErrorMessage(exception.getMessage())); + } + +} diff --git a/src/main/java/dev/struchkov/example/controlleradvice/service/CustomExceptionResolver.java b/src/main/java/dev/struchkov/example/controlleradvice/service/CustomExceptionResolver.java deleted file mode 100644 index 3335524..0000000 --- a/src/main/java/dev/struchkov/example/controlleradvice/service/CustomExceptionResolver.java +++ /dev/null @@ -1,29 +0,0 @@ -package dev.struchkov.example.controlleradvice.service; - -import dev.struchkov.example.controlleradvice.exception.NotFoundException; -import org.springframework.http.HttpStatus; -import org.springframework.stereotype.Component; -import org.springframework.web.servlet.ModelAndView; -import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver; -import org.springframework.web.servlet.view.json.MappingJackson2JsonView; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -@Component -public class CustomExceptionResolver extends AbstractHandlerExceptionResolver { - - @Override - protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) { - final ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView()); - if (e instanceof NotFoundException) { - modelAndView.setStatus(HttpStatus.NOT_FOUND); - modelAndView.addObject("message", "Пользователь не найден"); - return modelAndView; - } - modelAndView.setStatus(HttpStatus.INTERNAL_SERVER_ERROR); - modelAndView.addObject("message", "При выполнении запроса произошла ошибка"); - return modelAndView; - } - -}