A lightweight Python package for handling HTTP error status codes by raising a custom exception with detailed, human-friendly messages.
Project description
AWS Abort
A lightweight Python package for handling HTTP error status codes by raising a custom exception with detailed, human-friendly messages.
Overview
The abort package simplifies error handling in web applications or any Python project that interacts with HTTP APIs. Instead of manually checking status codes and constructing error messages, abort provides a clean interface to raise an AbortException with a pre-defined message for common HTTP error status codes. The package also leverages Python’s built-in logging module to help you track and debug issues based on error severity.
Features
- Custom Exception: The
AbortExceptionclass encapsulates an HTTP status code and a descriptive error message. - Pre-defined Error Messages: Built-in messages for common HTTP status codes (400, 401, 403, 404, 405, 409, 415, 424, 429, 500) help clarify what went wrong.
- Over-right Default Error Messages: If you are not a fan of the built-in messages, you can change them with ease!
- Integrated Logging: Automatically logs error details at appropriate logging levels (debug, warning, critical) to facilitate troubleshooting.
Installation
You can install the package using pip:
pip install aws-abort
Usage
Import the abort function from the package and call it with an HTTP status code and an optional message. This will log the error and raise an AbortException with a combined error message.
Example
from abort import abort
# Trigger an HTTP 400 Bad Request error
abort(400, "The submitted data is missing required fields.")
When this code runs, it logs the error at the debug level and raises an AbortException with a message like:
"Bad request, check your code is right, required fields entered? The submitted data is missing required fields."
API Reference
AbortException
A custom exception for HTTP error status codes.
Attributes:
status_code(int): The HTTP status code.message(str): A detailed error message combining a base message with additional context.
Example:
from abort import AbortException, abort
try:
abort(404, "Lost!")
except AbortException as e:
print(e.status_code) # 404
print(e.message) # "Not found, we searched, and we searched, but nothing was there... Sorry. Lost!"
abort(status_code, message="")
Handles HTTP error status codes by logging the error and raising an AbortException.
Parameters:
status_code(int): The HTTP status code.message(str, optional): Additional context to append to the base error message.
Raises:
AbortException: Raised with a detailed message based on the status code.
Update Built-In Messages
You may not want the default built-in messages, which is fine! You can set them to anything you like!
Example
from abort import AbortException, abort
# Set a different 405 base message
AbortException.base_405 = "Whatcha doing?!"
try:
abort(405, "This is a GET request!")
except AbortException as e:
print(e.status_code) # 405
print(e.message) # "Whatcha doing?! This is a GET request!"
Logging Configuration
The package uses Python's logging module under the logger name abort. You can configure logging in your application to capture these messages:
import logging
logging.basicConfig(level=logging.DEBUG)
License
This project is licensed under the MIT License. See the LICENSE file for more details.
Contributing
Contributions, issues, and feature requests are welcome!
Contact
For further inquiries or feedback, feel free to reach out via my website.
With abort, handling HTTP errors becomes more structured and maintainable. Enjoy seamless error management in your Python projects!
# That's all folks...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aws_abort-0.0.1.tar.gz.
File metadata
- Download URL: aws_abort-0.0.1.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
287cad6700d4c9931ca5999d34942e41e5c7d086b75f82b6be32da9157baef4a
|
|
| MD5 |
8a8f44c6755e6f4060357c9f50c80160
|
|
| BLAKE2b-256 |
1d17e08ac59649ce6302ca3e29a584dc8dbac38145d434617f22aaa135a07479
|
File details
Details for the file aws_abort-0.0.1-py3-none-any.whl.
File metadata
- Download URL: aws_abort-0.0.1-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
64a994139eb2e3d67e728b80b2a697dad5bfbad557b43a6ea83179f0988addba
|
|
| MD5 |
05b803ffe0358888c4ecf3bded7bb828
|
|
| BLAKE2b-256 |
df1bba323d4305db0ce4739aaa950b76c7a9f0526a750208bedd94847d812fb6
|