Skip to content

Conversation

faizakram
Copy link
Owner

Summary:

Introduced a custom exception handling mechanism to improve error management and standardize API error responses across the application. This includes a CustomException class to encapsulate error codes, messages, and HTTP statuses, along with an updated GlobalExceptionHandler to process these exceptions.

Changes Made:

  • Added CustomException Class:

    • Allows defining exceptions with custom error codes, messages, and HTTP status codes.
    • Simplifies throwing meaningful exceptions from controllers and services.
  • Updated GlobalExceptionHandler:

    • Handles CustomException and returns structured error responses with timestamp, errorCode, message, and status.
    • Added fallback for handling generic exceptions to ensure consistent error messaging.
  • Example Usage in Controller:

    • Added a sample endpoint in SampleController to demonstrate how to throw a CustomException.

Why This Is Needed:

  • Provides consistent and informative error responses for API consumers.
  • Facilitates easier debugging and troubleshooting with clear error codes and messages.
  • Enhances maintainability by centralizing exception handling logic.
  • Ensures proper HTTP status codes are returned based on the error context.

Testing:

  • Tested endpoints to confirm:
    • Custom exceptions return correct errorCode, message, and status.
    • Generic exceptions are caught and return standardized error responses.
    • Validation errors and unexpected runtime errors are handled gracefully.

Related Issues:

  • Resolves #<issue_number> (if applicable)

Labels:

  • feature
  • enhancement
  • exception-handling

@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