Skip to content

v0.1.3

Choose a tag to compare

@angelkurten angelkurten released this 12 Jul 16:55
· 14 commits to main since this release
a1a347f

Description:

This pull request introduces significant enhancements and updates to the response_handler_lib package, improving its capability to handle both predefined and custom errors, convert responses to JSON, and ensuring robust testing. Below is a summary of the changes:

New Features and Enhancements:

  1. Error Handling Enhancements:

    • Added PredefinedErrorCodes enum to manage predefined error codes, reducing the risk of typos.
    • Introduced ErrorResponseConfig class to manage predefined and custom error configurations.
    • Enhanced Response class to handle both predefined and custom errors effectively.
    • Added where property to ErrorResponse to capture the location (file, class, method, and line number) where the error was generated.
  2. JSON Conversion:

    • Added to_json method in the Response class to convert response objects to JSON format.
    • Optionally include the where property in the JSON output.
  3. Additional Tests:

    • Comprehensive tests added to verify the functionality of predefined and custom errors.
    • Tests for checking successful responses with and without data.
    • Tests for mixed responses containing both errors and data.
    • Test for verifying the to_json method's output, including optional where.

Detailed Changes:

  1. response_handler_lib Package:

    • ErrorResponse Class: Combined previous ErrorType and Error classes into one, added where property.
    • PredefinedErrorCodes Enum: Contains predefined error codes like VAL_ERR, NOT_FND, INT_ERR, etc.
    • ErrorResponseConfig Class:
      • add_custom_error method: Allows adding individual custom errors.
      • add_custom_errors method: Allows adding multiple custom errors at once.
      • get_error method: Retrieves error details by code.
    • Response Class:
      • add_error method: Adds errors to the response, capturing the location automatically.
      • has_errors property: Checks if the response has errors.
      • error_messages property: Retrieves a list of error messages.
      • error_types property: Retrieves a list of error codes.
      • to_json method: Converts the response to JSON format, optionally including where.
  2. Tests:

    • Predefined Error Tests: Verify correct handling of predefined errors.
    • Custom Error Tests: Ensure custom errors can be added and retrieved correctly.
    • Multiple Custom Errors Tests: Verify handling of multiple custom errors.
    • Undefined Error Test: Ensure a ValueError is raised for undefined errors.
    • Response Without Errors Test: Check correct handling of responses without errors.
    • Successful Response With Data Test: Validate responses with data.
    • Mixed Errors and Data Response Test: Verify handling of responses containing both errors and data.
    • JSON Conversion Test: Ensure correct JSON conversion of response objects, with optional where.

Example Usage:

from response_handler_lib import Response, ErrorResponseConfig, PredefinedErrorCodes

# Adding custom errors to the configuration
ErrorResponseConfig.add_custom_error("CUS_ERR1", "Custom error message 1.")
ErrorResponseConfig.add_custom_error("CUS_ERR2", "Custom error message 2.")

# Creating a response with predefined and custom errors
response = Response()
response.add_error(PredefinedErrorCodes.VAL_ERR.value)
response.add_error("CUS_ERR1")

# Printing the response details
print(response.has_errors)  # True
print(response.error_messages)  # ['Validation failed.', 'Custom error message 1.']
print(response.error_types)  # ['VAL_ERR', 'CUS_ERR1']
print(response.to_json())  # JSON representation of the response without 'where'
print(response.to_json(include_where=True))  # JSON representation of the response with 'where'

Contributing:

  • Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License:

  • This project is licensed under the MIT License.

Contact:

Please review the changes and provide any feedback. Thank you for your contribution!

What's Changed

  • Enhance Response Handling with Custom and Predefined Errors, JSON Conversion, and Additional Tests by @angelkurten in #1

New Contributors

Full Changelog: https://github.com/angelkurten/response_handler/commits/v0.1.3