Skip to content

Conversation

faizakram
Copy link
Owner

Summary:

Implemented a centralized and standardized exception handling mechanism using @RestControllerAdvice in the Spring Boot project. This ensures uniform error responses across all API endpoints and improves error traceability.

Changes Made:

  • Added RestExceptionHandler:

    • Handles validation errors (MethodArgumentNotValidException) with structured responses.
    • Catches custom business logic exceptions using CustomRuntimeException.
    • Manages generic RuntimeException with logging and standardized error messages.
  • Created CustomRuntimeException:

    • Custom exception class with flexible error messaging, response codes, and HTTP status handling.
  • Added StandardResponse DTO:

    • Standardized response structure for both success and error scenarios, including responseCode, responseDescription, response, and isOkay.
  • Implemented ResponseUtil:

    • Utility class to create consistent success and error responses using localized messages from the message source.

Why This Is Needed:

  • Provides consistent error handling and standardized response formats for all APIs.
  • Simplifies debugging by logging errors uniformly and returning meaningful messages to the API consumers.
  • Facilitates internationalization (i18n) by integrating message localization via MessageSource.

Testing:

  • Tested API endpoints to verify:
    • Validation errors are captured and returned with meaningful field-specific messages.
    • Custom exceptions (CustomRuntimeException) are properly handled with specific response codes and descriptions.
    • Generic runtime exceptions return appropriate error responses and are logged correctly.

@faizakram faizakram self-assigned this Feb 1, 2025
@faizakram faizakram added documentation Improvements or additions to documentation enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Feb 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants