Skip to main content

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

Project description

response_handler

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

Features

  • Custom Response class for handling responses with success status, errors, and data.
  • Custom Error class for defining error codes and messages.
  • Custom ResponseError exception for handling response-related errors.
  • Utilities for adding errors, checking success status, and retrieving error messages and codes.

Installation

You can install response_handler via pip:

pip install response_handler

Usage

Importing the Package

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

from response_handler import Response, Error, ResponseError

Creating a Successful Response

Create a Response object with success set to True and provide any data:

response = Response(success=True, data="Some data")
if response.is_successful:
    print("Response is successful")
    print("Data:", response.data)

Creating a Failed Response

Create a Response object with success set to False:

response = Response(success=False)
if not response.is_successful:
    print("Response failed")

Adding Errors to a Response

Add errors to a response using the add_error method:

response = Response(success=False)
response.add_error(404, "Not Found")
response.add_error(500, "Internal Server Error")

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

Handling Missing Success Field

If the success field is not set, accessing is_successful raises a ResponseError:

response = Response()

try:
    print(response.is_successful)
except ResponseError as e:
    print("Caught an error:", str(e))

API Reference

Classes

Response

A generic class for handling responses.

Attributes:
  • success (Optional[bool]): Indicates whether the response is successful.
  • errors (Optional[List[Error]]): A list of errors in the response.
  • data (Optional[T]): The data in the response.
Methods:
  • is_successful (bool): Checks if the response is successful. Raises ResponseError if success is not set.
  • add_error(code: int, message: 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_codes (List[int]): Retrieves a list of error codes.

Error

A class for defining error codes and messages.

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

ResponseError

A custom exception for response errors.

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

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.0.tar.gz (4.1 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.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for response_handler_lib-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1f5daf5d2378f26313df49904063dcdbec7551f87f66d13f1f26b6420d92dee2
MD5 bcf60097499cfdfd1f80a72373782a02
BLAKE2b-256 dcd491d8d2eab6e44073042101550b33740c5009549c67b1bbc3c9a05819e7f1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for response_handler_lib-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 428976c263821bf3b129de02e975209a26766bf343453c0a637c86c75008ff6b
MD5 e28e963fbee06395d306b306942ca526
BLAKE2b-256 3543e2f566350c8a3369453624dd4dc30f4568bad3582fceb1f7a47477926823

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