Class GlobalExceptionHandler
java.lang.Object
springboot.bg.harisauto.common.exception.GlobalExceptionHandler
Global exception handler for the web layer.
Business exceptions are turned into a flash message on the page the user came from, so a wrong password or a duplicate email reads as a form error rather than a whitelabel 500 page.
- Author:
- Kristian Popov
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.web.servlet.ModelAndViewhandleAccessDenied(org.springframework.security.access.AccessDeniedException e, jakarta.servlet.http.HttpServletRequest request) Handles an attempt to read a resource that belongs to somebody else.handleAccountExceptions(RuntimeException e, org.springframework.web.servlet.mvc.support.RedirectAttributes attributes) Handles a failed password change or a duplicate email on the settings screen.org.springframework.web.servlet.ModelAndViewhandleMissingResource(Exception e, jakarta.servlet.http.HttpServletRequest request) Keeps Spring's own routing failures as 404s.org.springframework.web.servlet.ModelAndViewhandleNotFound(RuntimeException e, jakarta.servlet.http.HttpServletRequest request) Handles lookups for users, services and categories that do not exist.org.springframework.web.servlet.ModelAndViewhandleResponseStatus(org.springframework.web.server.ResponseStatusException e, jakarta.servlet.http.HttpServletRequest request) Preserves the status carried by aResponseStatusException.org.springframework.web.servlet.ModelAndViewhandleUnexpected(Exception e, jakarta.servlet.http.HttpServletRequest request) Last-resort handler for anything not matched above.handleVehicleExceptions(VehicleBusinessException e, org.springframework.web.servlet.mvc.support.RedirectAttributes attributes) Handles vehicle-related exceptions.
-
Constructor Details
-
GlobalExceptionHandler
public GlobalExceptionHandler()
-
-
Method Details
-
handleVehicleExceptions
@ExceptionHandler(VehicleBusinessException.class) public String handleVehicleExceptions(VehicleBusinessException e, org.springframework.web.servlet.mvc.support.RedirectAttributes attributes) Handles vehicle-related exceptions.- Parameters:
e- - VehicleBusinessExceptionattributes- - RedirectAttributes- Returns:
- String
-
handleAccountExceptions
@ExceptionHandler({UserPasswordDoesNotMatchException.class,UserEmailAlreadyExistsException.class}) public String handleAccountExceptions(RuntimeException e, org.springframework.web.servlet.mvc.support.RedirectAttributes attributes) Handles a failed password change or a duplicate email on the settings screen.- Parameters:
e- The business exception.attributes- Flash attributes for the redirect.- Returns:
- Redirect back to the settings page.
-
handleNotFound
@ExceptionHandler({UserDoesNotExistException.class,ResourceNotFoundException.class,jakarta.persistence.EntityNotFoundException.class}) @ResponseStatus(NOT_FOUND) public org.springframework.web.servlet.ModelAndView handleNotFound(RuntimeException e, jakarta.servlet.http.HttpServletRequest request) Handles lookups for users, services and categories that do not exist.- Parameters:
e- The not-found exception.request- The request that triggered it.- Returns:
- The 404 view.
-
handleAccessDenied
@ExceptionHandler(org.springframework.security.access.AccessDeniedException.class) @ResponseStatus(NOT_FOUND) public org.springframework.web.servlet.ModelAndView handleAccessDenied(org.springframework.security.access.AccessDeniedException e, jakarta.servlet.http.HttpServletRequest request) Handles an attempt to read a resource that belongs to somebody else.- Parameters:
e- The access-denied exception.request- The request that triggered it.- Returns:
- The 404 view, so the resource's existence is not disclosed.
-
handleMissingResource
@ExceptionHandler({org.springframework.web.servlet.resource.NoResourceFoundException.class,org.springframework.web.servlet.NoHandlerFoundException.class}) @ResponseStatus(NOT_FOUND) public org.springframework.web.servlet.ModelAndView handleMissingResource(Exception e, jakarta.servlet.http.HttpServletRequest request) Keeps Spring's own routing failures as 404s.Without this they are swallowed by the catch-all below and reported as 500, so a missing page or static file would look like a server fault.
- Parameters:
e- The routing exception.request- The request that triggered it.- Returns:
- The 404 view.
-
handleResponseStatus
@ExceptionHandler(org.springframework.web.server.ResponseStatusException.class) public org.springframework.web.servlet.ModelAndView handleResponseStatus(org.springframework.web.server.ResponseStatusException e, jakarta.servlet.http.HttpServletRequest request) Preserves the status carried by aResponseStatusException.- Parameters:
e- The exception carrying its own status.request- The request that triggered it.- Returns:
- The error view, rendered with that status.
-
handleUnexpected
@ExceptionHandler(java.lang.Exception.class) @ResponseStatus(INTERNAL_SERVER_ERROR) public org.springframework.web.servlet.ModelAndView handleUnexpected(Exception e, jakarta.servlet.http.HttpServletRequest request) Last-resort handler for anything not matched above.The cause is logged with its stack trace, but the page shows only a generic message: exception text can carry identifiers, SQL or file paths that should not reach the browser.
- Parameters:
e- The unhandled exception.request- The request that triggered it.- Returns:
- The generic error view.
-