Package com.example.blogs.app.exception
Class GlobalExceptionHandler
java.lang.Object
com.example.blogs.app.exception.GlobalExceptionHandler
Centralized exception handler that translates exceptions into standardized error responses.
Handles validation errors, domain exceptions, and generic failures.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionhandleMethodValidationException(HandlerMethodValidationException exception, jakarta.servlet.http.HttpServletRequest request) Handles validation errors for method parameters and path variables.handleMissingRequestBodyException(HttpMessageNotReadableException exception, jakarta.servlet.http.HttpServletRequest request) Handles errors when HTTP message body cannot be read or parsed.handleRegularException(Exception exception, jakarta.servlet.http.HttpServletRequest request) Handles all domain-specific and unhandled exceptions.handleValidationException(MethodArgumentNotValidException exception, jakarta.servlet.http.HttpServletRequest request) Handles validation errors for request body fields annotated with Jakarta validation constraints.
-
Constructor Details
-
GlobalExceptionHandler
public GlobalExceptionHandler()
-
-
Method Details
-
handleValidationException
@ExceptionHandler(org.springframework.web.bind.MethodArgumentNotValidException.class) public ResponseEntity<ErrorResponse> handleValidationException(MethodArgumentNotValidException exception, jakarta.servlet.http.HttpServletRequest request) Handles validation errors for request body fields annotated with Jakarta validation constraints. Extracts all validation error messages and returns them in a standardized error response.- Parameters:
exception- the validation exception containing binding result errorsrequest- the HTTP request that triggered the exception- Returns:
- error response with HTTP 400 status and list of validation errors
-
handleMethodValidationException
@ExceptionHandler(org.springframework.web.method.annotation.HandlerMethodValidationException.class) public ResponseEntity<ErrorResponse> handleMethodValidationException(HandlerMethodValidationException exception, jakarta.servlet.http.HttpServletRequest request) Handles validation errors for method parameters and path variables. Processes field errors and parameter constraint violations into standardized error messages.- Parameters:
exception- the validation exception containing parameter validation errorsrequest- the HTTP request that triggered the exception- Returns:
- error response with HTTP 400 status and list of validation errors
-
handleMissingRequestBodyException
@ExceptionHandler(org.springframework.http.converter.HttpMessageNotReadableException.class) public ResponseEntity<ErrorResponse> handleMissingRequestBodyException(HttpMessageNotReadableException exception, jakarta.servlet.http.HttpServletRequest request) Handles errors when HTTP message body cannot be read or parsed. Processes different scenarios: invalid enum values, unrecognized fields, missing body, or malformed JSON.- Parameters:
exception- the message parsing exceptionrequest- the HTTP request that triggered the exception- Returns:
- error response with HTTP 400 status and descriptive error message
-
handleRegularException
@ExceptionHandler(java.lang.Exception.class) public ResponseEntity<ErrorResponse> handleRegularException(Exception exception, jakarta.servlet.http.HttpServletRequest request) Handles all domain-specific and unhandled exceptions. Maps exceptions to appropriate HTTP status codes using the status mapper. Logs server errors (5xx) with full context for debugging.- Parameters:
exception- the exception to handlerequest- the HTTP request that triggered the exception- Returns:
- error response with appropriate HTTP status and exception details
-