v0.1.3
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:
-
Error Handling Enhancements:
- Added
PredefinedErrorCodesenum to manage predefined error codes, reducing the risk of typos. - Introduced
ErrorResponseConfigclass to manage predefined and custom error configurations. - Enhanced
Responseclass to handle both predefined and custom errors effectively. - Added
whereproperty toErrorResponseto capture the location (file, class, method, and line number) where the error was generated.
- Added
-
JSON Conversion:
- Added
to_jsonmethod in theResponseclass to convert response objects to JSON format. - Optionally include the
whereproperty in the JSON output.
- Added
-
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_jsonmethod's output, including optionalwhere.
Detailed Changes:
-
response_handler_libPackage:ErrorResponseClass: Combined previousErrorTypeandErrorclasses into one, addedwhereproperty.PredefinedErrorCodesEnum: Contains predefined error codes likeVAL_ERR,NOT_FND,INT_ERR, etc.ErrorResponseConfigClass:add_custom_errormethod: Allows adding individual custom errors.add_custom_errorsmethod: Allows adding multiple custom errors at once.get_errormethod: Retrieves error details by code.
ResponseClass:add_errormethod: Adds errors to the response, capturing the location automatically.has_errorsproperty: Checks if the response has errors.error_messagesproperty: Retrieves a list of error messages.error_typesproperty: Retrieves a list of error codes.to_jsonmethod: Converts the response to JSON format, optionally includingwhere.
-
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:
- Author: Angel Kürten
- Email: angel@angelkurten.com
- GitHub: angelkurten
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
- @angelkurten made their first contribution in #1
Full Changelog: https://github.com/angelkurten/response_handler/commits/v0.1.3