Package com.moeen.shared_lib.exception
Class GlobalExceptionHandler
java.lang.Object
com.moeen.shared_lib.exception.GlobalExceptionHandler
Global Exception Handler for the REST API.
This class uses @RestControllerAdvice to catch exceptions across the whole
application.
It standardizes the error response format for different types of exceptions.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.http.ResponseEntity<Object> handleConflictException(ConflictException ex, org.springframework.web.context.request.WebRequest request) Handles exceptions when a conflict occurs.org.springframework.http.ResponseEntity<Object> handleConstraintViolationException(jakarta.validation.ConstraintViolationException ex, org.springframework.web.context.request.WebRequest request) Handles validation errors for path variables and request parameters.org.springframework.http.ResponseEntity<Object> handleEntityNotFoundException(ResourceNotFoundException ex, org.springframework.web.context.request.WebRequest request) Handles exceptions when a specific entity is not found in the database.org.springframework.http.ResponseEntity<Object> handleGlobalException(Exception ex, org.springframework.web.context.request.WebRequest request) A catch-all handler for any other unhandled exceptions.org.springframework.http.ResponseEntity<Object> handleHttpMessageNotReadable(org.springframework.http.converter.HttpMessageNotReadableException ex, org.springframework.web.context.request.WebRequest request) Handles exceptions when the request body is malformed or not readable.org.springframework.http.ResponseEntity<Object> handleIllegalArgumentException(IllegalArgumentException ex, org.springframework.web.context.request.WebRequest request) Handles general bad requests that don't fall into more specific categories.org.springframework.http.ResponseEntity<Object> handleIllegalStateException(IllegalStateException ex, org.springframework.web.context.request.WebRequest request) Handles invalid state transitions for order status changes.org.springframework.http.ResponseEntity<Object> handleMethodArgumentTypeMismatch(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException ex, org.springframework.web.context.request.WebRequest request) Handles exceptions when a method argument is not the expected type.org.springframework.http.ResponseEntity<Object> handleMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException ex, org.springframework.web.context.request.WebRequest request) Handles cases where the HTTP method used is not supported for the requested endpoint.org.springframework.http.ResponseEntity<Object> handleNoResourceFoundException(org.springframework.web.servlet.resource.NoResourceFoundException ex, org.springframework.web.context.request.WebRequest request) Handles cases where a requested resource is not found.org.springframework.http.ResponseEntity<Object> handleNoSuchElementException(NoSuchElementException ex, org.springframework.web.context.request.WebRequest request) Handles cases where a requested resource is not found.org.springframework.http.ResponseEntity<Object> handleValidationException(ValidationException ex, org.springframework.web.context.request.WebRequest request) Handles custom programmatic validation errors that include field paths.org.springframework.http.ResponseEntity<Object> handleValidationExceptions(org.springframework.web.bind.MethodArgumentNotValidException ex, org.springframework.web.context.request.WebRequest request) Handles validation errors for request bodies annotated with @Valid.
-
Constructor Details
-
GlobalExceptionHandler
public GlobalExceptionHandler()
-
-
Method Details
-
handleValidationExceptions
@ExceptionHandler(org.springframework.web.bind.MethodArgumentNotValidException.class) public org.springframework.http.ResponseEntity<Object> handleValidationExceptions(org.springframework.web.bind.MethodArgumentNotValidException ex, org.springframework.web.context.request.WebRequest request) Handles validation errors for request bodies annotated with @Valid. This is triggered when MethodArgumentNotValidException is thrown.- Parameters:
ex- The MethodArgumentNotValidException instance.request- The current web request.- Returns:
- A ResponseEntity containing the standardized error response.
-
handleConstraintViolationException
@ExceptionHandler(jakarta.validation.ConstraintViolationException.class) public org.springframework.http.ResponseEntity<Object> handleConstraintViolationException(jakarta.validation.ConstraintViolationException ex, org.springframework.web.context.request.WebRequest request) Handles validation errors for path variables and request parameters. This is triggered when ConstraintViolationException is thrown.- Parameters:
ex- The ConstraintViolationException instance.request- The current web request.- Returns:
- A ResponseEntity containing the standardized error response.
-
handleNoSuchElementException
@ExceptionHandler(java.util.NoSuchElementException.class) public org.springframework.http.ResponseEntity<Object> handleNoSuchElementException(NoSuchElementException ex, org.springframework.web.context.request.WebRequest request) Handles cases where a requested resource is not found. This is a common exception for repository or service layers.- Parameters:
ex- The NoSuchElementException instance.request- The current web request.- Returns:
- A ResponseEntity with HTTP 404 Not Found status.
-
handleNoResourceFoundException
@ExceptionHandler(org.springframework.web.servlet.resource.NoResourceFoundException.class) public org.springframework.http.ResponseEntity<Object> handleNoResourceFoundException(org.springframework.web.servlet.resource.NoResourceFoundException ex, org.springframework.web.context.request.WebRequest request) Handles cases where a requested resource is not found. This is a common exception for static resources.- Parameters:
ex- The NoResourceFoundException instance.request- The current web request.- Returns:
- A ResponseEntity with HTTP 404 Not Found status.
-
handleMethodArgumentTypeMismatch
@ExceptionHandler(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException.class) public org.springframework.http.ResponseEntity<Object> handleMethodArgumentTypeMismatch(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException ex, org.springframework.web.context.request.WebRequest request) Handles exceptions when a method argument is not the expected type. For example, providing a string for a UUID parameter that is not in the correct format.- Parameters:
ex- The MethodArgumentTypeMismatchException instance.request- The current web request.- Returns:
- A ResponseEntity with HTTP 400 Bad Request status.
-
handleIllegalArgumentException
@ExceptionHandler(java.lang.IllegalArgumentException.class) public org.springframework.http.ResponseEntity<Object> handleIllegalArgumentException(IllegalArgumentException ex, org.springframework.web.context.request.WebRequest request) Handles general bad requests that don't fall into more specific categories.- Parameters:
ex- The IllegalArgumentException instance.request- The current web request.- Returns:
- A ResponseEntity with HTTP 400 Bad Request status.
-
handleIllegalStateException
@ExceptionHandler(java.lang.IllegalStateException.class) public org.springframework.http.ResponseEntity<Object> handleIllegalStateException(IllegalStateException ex, org.springframework.web.context.request.WebRequest request) Handles invalid state transitions for order status changes. This is triggered when IllegalStateException is thrown.- Parameters:
ex- The IllegalStateException instance.request- The current web request.- Returns:
- A ResponseEntity with HTTP 409 Conflict status.
-
handleHttpMessageNotReadable
@ExceptionHandler(org.springframework.http.converter.HttpMessageNotReadableException.class) public org.springframework.http.ResponseEntity<Object> handleHttpMessageNotReadable(org.springframework.http.converter.HttpMessageNotReadableException ex, org.springframework.web.context.request.WebRequest request) Handles exceptions when the request body is malformed or not readable. This is triggered when HttpMessageNotReadableException is thrown, often due to JSON parsing errors.- Parameters:
ex- The HttpMessageNotReadableException instance.request- The current web request.- Returns:
- A ResponseEntity with HTTP 400 Bad Request status.
-
handleMethodNotSupportedException
@ExceptionHandler(org.springframework.web.HttpRequestMethodNotSupportedException.class) public org.springframework.http.ResponseEntity<Object> handleMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException ex, org.springframework.web.context.request.WebRequest request) Handles cases where the HTTP method used is not supported for the requested endpoint. For example, sending a POST request to an endpoint that only supports GET. Returns a 405 Method Not Allowed status.- Parameters:
ex- The HttpRequestMethodNotSupportedException instance.request- The current web request.- Returns:
- A ResponseEntity with HTTP 405 Method Not Allowed status.
-
handleGlobalException
@ExceptionHandler(java.lang.Exception.class) public org.springframework.http.ResponseEntity<Object> handleGlobalException(Exception ex, org.springframework.web.context.request.WebRequest request) A catch-all handler for any other unhandled exceptions. This ensures that the client always receives a structured JSON error response. It's important to log the full exception for debugging purposes.- Parameters:
ex- The Exception instance.request- The current web request.- Returns:
- A ResponseEntity with HTTP 500 Internal Server Error status.
-