Skip to main content

No project description provided

Project description

HTTP Status Code Exception Library

The HTTP Status Code Exception Library is a Python library designed to provide a structured and object-oriented approach to handling HTTP status codes as exceptions. This library simplifies the process of working with HTTP responses by mapping each status code to a corresponding exception class.


Table of Contents

  1. Features
  2. Installation
  3. Usage
  4. Examples
  5. Contributing
  6. License

Features

  • Comprehensive Coverage: Includes all standard HTTP status codes (1xx, 2xx, 3xx, 4xx, 5xx).
  • Object-Oriented Design: Each status code is represented as a distinct exception class.
  • Customizable Messages: Allows for custom error messages when raising exceptions.
  • Easy Integration: Designed to be easily integrated into web applications or APIs.
  • Extensible: Easily extendable to include additional functionality or custom status codes.

Installation

You can install the library using pip:

pip install http-status-code-exception

Alternatively, if you are using Poetry, add it to your project:

poetry add http-status-code-exception

Usage

The library provides a base exception class HTTPException and specific subclasses for each HTTP status code. You can import and use these classes directly in your application.

Importing Exceptions

from http_status_code_exception.client_error import NotFound

Raising Exceptions

raise NotFound("The requested resource was not found")

Retrieving Exceptions Dynamically

If you need to retrieve an exception class based on a status code, use the get function:

from http_status_code_exception import get

status_code: int = 404
exception_class = get(status_code)
raise exception_class("Page not found")

Examples

Example 1: Handling Client Errors

from http_status_code_exception.client_error import BadRequest, Unauthorized, Forbidden

def handle_client_errors():
    try:
        # Simulate a bad request
        raise BadRequest("Invalid request data")
    except BadRequest as e:
        print(f"Bad Request: {e}")
    except Unauthorized as e:
        print(f"Unauthorized: {e}")
    except Forbidden as e:
        print(f"Forbidden: {e}")

Example 2: Handling Server Errors

from http_status_code_exception.error_server import InternalServerError, ServiceUnavailable

def handle_server_errors():
    try:
        # Simulate an internal server error
        raise InternalServerError("Database connection failed")
    except InternalServerError as e:
        print(f"Internal Server Error: {e}")
    except ServiceUnavailable as e:
        print(f"Service Unavailable: {e}")

Contributing

Contributions are welcome! If you find a bug or have a feature request, please open an issue or submit a pull request.

  1. Fork the repository.
  2. Create a new branch for your changes (git checkout -b feature/new-feature).
  3. Commit your changes (git commit -am 'Add new feature').
  4. Push to the branch (git push origin feature/new-feature).
  5. Submit a pull request.

License

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


Acknowledgments

  • Thanks to the Python community for inspiring this library.
  • Special thanks to contributors who have helped improve this project.

Feel free to reach out if you have any questions or need further assistance!

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

http_status_code_exception-0.1.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

File details

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

File metadata

  • Download URL: http_status_code_exception-0.1.0.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.12.3 Linux/6.8.0-51-generic

File hashes

Hashes for http_status_code_exception-0.1.0.tar.gz
Algorithm Hash digest
SHA256 59c6de0d29cf85c79ccf0e944c4a63f21188e180d988474711c45a0d9692f725
MD5 f39d7464bbf9ad41402122b5650ac050
BLAKE2b-256 96996f07a8e77fb00af28b356e036807b9f8c0db686cecd2b06a8f1a871b1061

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for http_status_code_exception-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2e191cf8d051d47ad4cf33882c227888b62112ba1e270e7930994a81f238d719
MD5 d5958f0c27935b4e10dcab1e2ae569f1
BLAKE2b-256 e9331cf4a90bbe943181c04e1935f541efc6bf23999869318b512c3ff9c088fe

See more details on using hashes here.

Supported by

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