Class ExceptionHandlingAdvice
java.lang.Object
pro.nikolaev.restutils.components.ExceptionHandlingAdvice
Class representing
RestControllerAdvice bean for handling MVC exception.- Version:
- 1.0
- Author:
- Ilya Nikolaev
-
Constructor Summary
ConstructorsConstructorDescriptionExceptionHandlingAdvice(jakarta.servlet.MultipartConfigElement multipartConfigElement) -
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.http.ResponseEntity<ApiError> handel401(org.springframework.security.core.AuthenticationException e) ExceptionHandlerto handle anyAuthenticationExceptionexcept ofBadCredentialsException,UsernameNotFoundException,AccountExpiredException,DisabledExceptionandLockedException.org.springframework.http.ResponseEntity<ApiError> handle400(org.springframework.http.converter.HttpMessageNotReadableException e) ExceptionHandlerto handleHttpMessageNotReadableException.org.springframework.http.ResponseEntity<ApiError> handle400(org.springframework.web.bind.MethodArgumentNotValidException e) ExceptionHandlerto handleMethodArgumentNotValidException.org.springframework.http.ResponseEntity<ApiError> handle400(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException e) ExceptionHandlerto handleMethodArgumentTypeMismatchException.org.springframework.http.ResponseEntity<ApiError> handle403(org.springframework.security.access.AccessDeniedException e) ExceptionHandlerto handleAccessDeniedException.org.springframework.http.ResponseEntity<ApiError> handle404(org.springframework.web.servlet.resource.NoResourceFoundException e) ExceptionHandlerto handleNoResourceFoundException.org.springframework.http.ResponseEntity<ApiError> handle405(org.springframework.web.HttpRequestMethodNotSupportedException ignored) ExceptionHandlerto handleHttpRequestMethodNotSupportedException.org.springframework.http.ResponseEntity<ApiError> handle406(org.springframework.web.HttpMediaTypeNotAcceptableException e) ExceptionHandlerto handleHttpMediaTypeNotAcceptableException.org.springframework.http.ResponseEntity<ApiError> handle413(org.springframework.web.multipart.MaxUploadSizeExceededException e) ExceptionHandlerto handleMaxUploadSizeExceededException.org.springframework.http.ResponseEntity<ApiError> handle415(org.springframework.web.HttpMediaTypeNotSupportedException e) ExceptionHandlerto handleHttpMediaTypeNotSupportedException.org.springframework.http.ResponseEntity<ApiError> handle423(org.springframework.security.core.AuthenticationException e) ExceptionHandlerto handleAccountExpiredException,DisabledExceptionandLockedExceptionexceptions.org.springframework.http.ResponseEntity<ApiError> ExceptionHandlerto handleApiException.org.springframework.http.ResponseEntity<ApiError> handleBadCredentials(org.springframework.security.core.AuthenticationException ignored) ExceptionHandlerto handleBadCredentialsException, andUsernameNotFoundExceptionexceptions.org.springframework.http.ResponseEntity<ApiError> handleStatusException(org.springframework.web.server.ResponseStatusException e) ExceptionHandlerto handleResponseStatusException.org.springframework.http.ResponseEntity<ApiError> ExceptionHandlerto handle any exception not specified in other handlers ofExceptionHandlingAdvicethrown while executing aRestControllermethod.
-
Constructor Details
-
ExceptionHandlingAdvice
public ExceptionHandlingAdvice(jakarta.servlet.MultipartConfigElement multipartConfigElement)
-
-
Method Details
-
handleApiException
@ExceptionHandler(ApiException.class) public org.springframework.http.ResponseEntity<ApiError> handleApiException(ApiException e) ExceptionHandlerto handleApiException.- Parameters:
e-ApiExceptionto be processed byExceptionHandler- Returns:
ResponseEntitywith HTTP status from exception, exception reason asmessage, exception message indetailspart of the body andConnection: Closeheader- Since:
- 1.0
- See Also:
-
handleBadCredentials
@ExceptionHandler({org.springframework.security.authentication.BadCredentialsException.class,org.springframework.security.core.userdetails.UsernameNotFoundException.class}) public org.springframework.http.ResponseEntity<ApiError> handleBadCredentials(org.springframework.security.core.AuthenticationException ignored) ExceptionHandlerto handleBadCredentialsException, andUsernameNotFoundExceptionexceptions.- Parameters:
ignored-AuthenticationExceptionto be processed byExceptionHandler- Returns:
ResponseEntitywith HTTP status 401, "Не авторизован" message, "Не верный логин или пароль" indetailspart of the body andConnection: Closeheader- Since:
- 1.0
- See Also:
-
handel401
@ExceptionHandler(org.springframework.security.core.AuthenticationException.class) public org.springframework.http.ResponseEntity<ApiError> handel401(org.springframework.security.core.AuthenticationException e) ExceptionHandlerto handle anyAuthenticationExceptionexcept ofBadCredentialsException,UsernameNotFoundException,AccountExpiredException,DisabledExceptionandLockedException.- Parameters:
e-AuthenticationExceptionto be processed byExceptionHandler- Returns:
ResponseEntitywith HTTP status 401, "Не авторизован" message, exception message indetailspart of the body andConnection: Closeheader- Since:
- 1.0
- See Also:
-
handle423
@ExceptionHandler({org.springframework.security.authentication.AccountExpiredException.class,org.springframework.security.authentication.DisabledException.class,org.springframework.security.authentication.LockedException.class}) public org.springframework.http.ResponseEntity<ApiError> handle423(org.springframework.security.core.AuthenticationException e) ExceptionHandlerto handleAccountExpiredException,DisabledExceptionandLockedExceptionexceptions.- Parameters:
e-AuthenticationExceptionto be processed byExceptionHandler- Returns:
ResponseEntitywith HTTP status 423, "Заблокировано" message, exception message indetailspart of the body andConnection: Closeheader- Since:
- 1.0
- See Also:
-
handle403
@ExceptionHandler(org.springframework.security.access.AccessDeniedException.class) public org.springframework.http.ResponseEntity<ApiError> handle403(org.springframework.security.access.AccessDeniedException e) ExceptionHandlerto handleAccessDeniedException.- Parameters:
e-AccessDeniedExceptionto be processed byExceptionHandler- Returns:
ResponseEntitywith HTTP status 403, "Доступ запрещен" message, exception message indetailspart of the body andConnection: Closeheader- Since:
- 1.0
- See Also:
-
handle405
@ExceptionHandler(org.springframework.web.HttpRequestMethodNotSupportedException.class) public org.springframework.http.ResponseEntity<ApiError> handle405(org.springframework.web.HttpRequestMethodNotSupportedException ignored) ExceptionHandlerto handleHttpRequestMethodNotSupportedException.- Parameters:
ignored-HttpRequestMethodNotSupportedExceptionto be processed byExceptionHandler- Returns:
ResponseEntitywith HTTP status 405, "Метод не поддерживается" message andConnection: Closeheader- Since:
- 1.0
- See Also:
-
handle400
@ExceptionHandler(org.springframework.web.bind.MethodArgumentNotValidException.class) public org.springframework.http.ResponseEntity<ApiError> handle400(org.springframework.web.bind.MethodArgumentNotValidException e) ExceptionHandlerto handleMethodArgumentNotValidException.NOTE: only works if
Jakarta Bean Validationis properly configured.- Parameters:
e-MethodArgumentNotValidExceptionto be processed byExceptionHandler- Returns:
ResponseEntitywith HTTP status 400, "Некорректный запрос" message, failed parameter info indetailspart of the body andConnection: Closeheader- Since:
- 1.0
- See Also:
-
handle400
@ExceptionHandler(org.springframework.http.converter.HttpMessageNotReadableException.class) public org.springframework.http.ResponseEntity<ApiError> handle400(org.springframework.http.converter.HttpMessageNotReadableException e) ExceptionHandlerto handleHttpMessageNotReadableException.- Parameters:
e-HttpMessageNotReadableExceptionto be processed byExceptionHandler- Returns:
ResponseEntitywith HTTP status 400, "Некорректный запрос" message, exception message indetailspart of the body andConnection: Closeheader- Since:
- 1.0
- See Also:
-
handle400
@ExceptionHandler(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException.class) public org.springframework.http.ResponseEntity<ApiError> handle400(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException e) ExceptionHandlerto handleMethodArgumentTypeMismatchException.- Parameters:
e-MethodArgumentTypeMismatchExceptionto be processed byExceptionHandler- Returns:
ResponseEntitywith HTTP status 400, "Некорректный запрос" message, information about invalid parameter indetailspart of the body andConnection: Closeheader- Since:
- 1.0
- See Also:
-
handle406
@ExceptionHandler(org.springframework.web.HttpMediaTypeNotAcceptableException.class) public org.springframework.http.ResponseEntity<ApiError> handle406(org.springframework.web.HttpMediaTypeNotAcceptableException e) ExceptionHandlerto handleHttpMediaTypeNotAcceptableException.- Parameters:
e-HttpMediaTypeNotAcceptableExceptionto be processed byExceptionHandler- Returns:
ResponseEntitywith HTTP status 406, "Тип данных не поддерживается" message, exception message indetailspart of the body andConnection: Closeheader- Since:
- 1.0
- See Also:
-
handle415
@ExceptionHandler(org.springframework.web.HttpMediaTypeNotSupportedException.class) public org.springframework.http.ResponseEntity<ApiError> handle415(org.springframework.web.HttpMediaTypeNotSupportedException e) ExceptionHandlerto handleHttpMediaTypeNotSupportedException.- Parameters:
e-HttpMediaTypeNotSupportedExceptionto be processed byExceptionHandler- Returns:
ResponseEntitywith HTTP status 415, "Не поддерживаемый тип данных" message, exception message indetailspart of the body andConnection: Closeheader- Since:
- 1.0
- See Also:
-
handle413
@ExceptionHandler(org.springframework.web.multipart.MaxUploadSizeExceededException.class) public org.springframework.http.ResponseEntity<ApiError> handle413(org.springframework.web.multipart.MaxUploadSizeExceededException e) ExceptionHandlerto handleMaxUploadSizeExceededException.- Parameters:
e-MaxUploadSizeExceededExceptionto be processed byExceptionHandler- Returns:
ResponseEntitywith HTTP status 413, "Превышен максимальный размер запроса" message, either max request size, max file size or both indetailspart of the body andConnection: Closeheader- Since:
- 1.0
- See Also:
-
handleStatusException
@ExceptionHandler(org.springframework.web.server.ResponseStatusException.class) public org.springframework.http.ResponseEntity<ApiError> handleStatusException(org.springframework.web.server.ResponseStatusException e) ExceptionHandlerto handleResponseStatusException.- Parameters:
e-ResponseStatusExceptionto be processed byExceptionHandler- Returns:
ResponseEntitywith HTTP status from exception, exception reason asmessage, exception detailed message code indetailspart of the body andConnection: Closeheader- Since:
- 1.0
- See Also:
-
handle404
@ExceptionHandler(org.springframework.web.servlet.resource.NoResourceFoundException.class) public org.springframework.http.ResponseEntity<ApiError> handle404(org.springframework.web.servlet.resource.NoResourceFoundException e) ExceptionHandlerto handleNoResourceFoundException.- Parameters:
e-NoResourceFoundExceptionto be processed byExceptionHandler- Returns:
ResponseEntitywith HTTP status 404, "Не найдено" message, resource path indetailspart of the body andConnection: Closeheader- Since:
- 1.0
- See Also:
-
handleUnexpectedException
@ExceptionHandler(java.lang.Exception.class) public org.springframework.http.ResponseEntity<ApiError> handleUnexpectedException(Exception e) ExceptionHandlerto handle any exception not specified in other handlers ofExceptionHandlingAdvicethrown while executing aRestControllermethod.As this method is intended to handle any unexpected or otherwise handled exceptions it also logs them at
ERRORlevel for easier debugging.- Parameters:
e-Exceptionto be processed byExceptionHandler- Returns:
ResponseEntitywith HTTP status 500, "Внутренняя ошибка приложения" message, exception message indetailspart of the body andConnection: Closeheader- Since:
- 1.0
- See Also:
-