Class GlobalExceptionHandler
This class intercepts exceptions thrown by any controller across the application
and transforms them into a standard ProblemDetail format (RFC 7807).
This ensures a consistent error response structure for API clients.
- Version:
- 1.2.0
- Author:
- Pasindu OG
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.http.ProblemDetailHandles all unexpected exceptions (catch-all handler).org.springframework.http.ProblemDetailHandles all custom exceptions that extendApiException.org.springframework.http.ProblemDetailHandles NullPointerExceptions explicitly.org.springframework.http.ProblemDetailhandleValidationExceptions(org.springframework.web.bind.MethodArgumentNotValidException ex) Handles bean validation errors (e.g., @NotNull, @Size failures).
-
Constructor Details
-
GlobalExceptionHandler
public GlobalExceptionHandler()
-
-
Method Details
-
handleAllExceptions
@ExceptionHandler(java.lang.Exception.class) public org.springframework.http.ProblemDetail handleAllExceptions(Exception ex) Handles all unexpected exceptions (catch-all handler).This method acts as a safety net for any error not explicitly handled by other methods. It returns a generic 500 Internal Server Error to avoid leaking sensitive stack traces to the client, while logging the full error details for debugging.
- Parameters:
ex- The exception that was thrown.- Returns:
- A
ProblemDetailwith HTTP 500 status and a generic error message.
-
handleValidationExceptions
@ExceptionHandler(org.springframework.web.bind.MethodArgumentNotValidException.class) public org.springframework.http.ProblemDetail handleValidationExceptions(org.springframework.web.bind.MethodArgumentNotValidException ex) Handles bean validation errors (e.g., @NotNull, @Size failures).Triggers when a
RequestBodyfails validation. It collects all field-level errors and returns them in a map under the "errors" property. If a field has multiple errors, they are joined by a semicolon.- Parameters:
ex- The validation exception containing the list of field errors.- Returns:
- A
ProblemDetailwith HTTP 400 status and a map of validation errors.
-
handleNullPointerExceptions
@ExceptionHandler(java.lang.NullPointerException.class) public org.springframework.http.ProblemDetail handleNullPointerExceptions(NullPointerException ex) Handles NullPointerExceptions explicitly.While strictly not necessary (as the catch-all handler covers this), having a specific handler allows for distinct logging or custom messaging for NPEs if required in the future.
- Parameters:
ex- The NullPointerException that was thrown.- Returns:
- A
ProblemDetailwith HTTP 500 status.
-
handleApiException
@ExceptionHandler(ApiException.class) public org.springframework.http.ProblemDetail handleApiException(ApiException ex) Handles all custom exceptions that extendApiException.This method dynamically extracts the HTTP status and message from the thrown exception, providing a standardized RFC 7807 response for any domain-specific error created by the library user.
- Parameters:
ex- The custom API exception instance.- Returns:
- A
ProblemDetailwith the status and message defined in the exception.
-