Skip to main content

A package for handling responses with potential errors and generic data.

Project description

response_handler_lib

A package for handling responses with potential errors and generic data, including predefined and custom error handling. This package is used as a complement to exceptions to have more control over business logic errors.

Features

  • Custom Response class for handling responses with errors and data.
  • Custom ErrorResponse class for defining error codes and messages.
  • ErrorResponseConfig class for managing predefined and custom error configurations.
  • PredefinedErrorCodes enum for predefined error codes.
  • Utilities for adding errors, checking for errors, and retrieving error messages and codes.
  • Convert Response to JSON format.

Installation

You can install response_handler_lib via pip:

pip install response_handler_lib

Usage

Importing the Package

To use the Response class and related utilities, import them from the response_handler_lib package:

from response_handler_lib import Response, ErrorResponseConfig, PredefinedErrorCodes

Creating a Successful Response

Create a Response object and provide any data:

response = Response(data="Some data")
if not response.has_errors:
    print("Response is successful")
    print("Data:", response.data)

Creating a Failed Response with Predefined Errors

Add predefined errors to a response using the add_error method:

response = Response()
response.add_error(PredefinedErrorCodes.VAL_ERR.value)
response.add_error(PredefinedErrorCodes.NOT_FND.value)

if response.has_errors:
    print("Errors:")
    for code, message in zip(response.error_types, response.error_messages):
        print(f"Error {code}: {message}")

Adding Custom Errors to the Configuration

Add custom errors to the ErrorResponseConfig:

ErrorResponseConfig.add_custom_error("CUS_ERR1", "Custom error message 1.")
ErrorResponseConfig.add_custom_error("CUS_ERR2", "Custom error message 2.")

Creating a Failed Response with Custom Errors

Add custom errors to a response using the add_error method:

response = Response()
response.add_error("CUS_ERR1")
response.add_error("CUS_ERR2")

if response.has_errors:
    print("Errors:")
    for code, message in zip(response.error_types, response.error_messages):
        print(f"Error {code}: {message}")

Adding Multiple Custom Errors at Once

Add multiple custom errors to the ErrorResponseConfig:

custom_errors = {
    "CUS_ERR3": "Custom error message 3.",
    "CUS_ERR4": "Custom error message 4."
}

ErrorResponseConfig.add_custom_errors(custom_errors)

Handling Undefined Errors

Attempting to add an undefined error raises a ValueError:

response = Response()

try:
    response.add_error("NOT_DEFINED")
except ValueError as e:
    print("Caught an error:", str(e))

Converting a Response to JSON

Convert a Response object to JSON format using the to_json method:

response = Response(data="Some data")
response.add_error("VAL_ERR")
print(response.to_json())

API Reference

Classes

Response

A generic class for handling responses.

Attributes:
  • errors (Optional[List[ErrorResponse]]): A list of errors in the response.
  • data (Optional[T]): The data in the response.
Methods:
  • add_error(error_code: str): Adds an error to the response.
  • has_errors (bool): Checks if the response has errors.
  • error_messages (List[str]): Retrieves a list of error messages.
  • error_types (List[str]): Retrieves a list of error codes.
  • to_json(): Converts the response to JSON format.

ErrorResponse

A class for defining error codes and messages.

Attributes:
  • code (str): The error code.
  • message (str): The error message.

ErrorResponseConfig

A class for managing predefined and custom error configurations.

Methods:
  • add_custom_error(code: str, message: str): Adds a custom error to the configuration.
  • add_custom_errors(errors: Dict[str, str]): Adds multiple custom errors to the configuration.
  • get_error(code: str): Retrieves an error by code.

PredefinedErrorCodes

An enum for predefined error codes.

Contributing

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

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contact

Author: Angel Kürten
Email: angel@angelkurten.com
GitHub: angelkurten

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

response_handler_lib-0.1.2.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

response_handler_lib-0.1.2-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file response_handler_lib-0.1.2.tar.gz.

File metadata

  • Download URL: response_handler_lib-0.1.2.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for response_handler_lib-0.1.2.tar.gz
Algorithm Hash digest
SHA256 59f19354490ad5b3f3a34ad046f6fb2f23bf4212d233c6374d3ddaf36e1f3d3d
MD5 a8af2e7dfb3781598d2e768d117b97b6
BLAKE2b-256 58c25ca31b3bd4f303cf59d630066b54f0de9f8f2cb00a7d734529f056b16668

See more details on using hashes here.

File details

Details for the file response_handler_lib-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for response_handler_lib-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4874a353e2784b8793023fbcdf0c56aade6d40e39fba2fbb2996ba283c69f488
MD5 a334f3d66003ea4c6290bb52d679660e
BLAKE2b-256 c7807fb369c1523ab4c347c21f0217c5c1d40e811e5d259413497c64c56cf638

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page