You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**APIException** is a robust, production-ready Python library for FastAPI that simplifies exception handling and ensures consistent, well-structured API responses. Designed for developers who want to eliminate boilerplate error handling and improve Swagger/OpenAPI documentation, APIException makes your FastAPI projects cleaner and easier to maintain.
@@ -34,20 +35,13 @@ Reading the [full documentation](https://akutayural.github.io/APIException/) is
34
35
---
35
36
36
37
> [!IMPORTANT]
37
-
> **New in v0.2.1:** <br>
38
-
> - ✨ **Async support** for `extra_log_fields` → you can now use `await request.body()` directly.
39
-
> - 🧩 **Python 3.9 compatibility restored** with `typing_extensions.TypeGuard`.
40
-
> - ⚡ Improved `response_utils.py` type-safety for all Python versions. <br>
41
-
> - 📦 Updated dependencies and `pyproject.toml` for wider environment support. <br>
> - 📢 Featured in [**Python Weekly #710**](https://www.pythonweekly.com/p/python-weekly-issue-710-august-14-2025-3200567a10d37d87) 🎉 <br>
50
-
> - 👉 For full details and usage examples, see [**register_exception_handlers reference**](https://akutayural.github.io/APIException/usage/register_exception_handlers/) <br>
40
+
> APIException is actively maintained and used in production FastAPI services.
41
+
> It is designed to be integrated as a library, not copied piece by piece into projects.
42
+
>
43
+
> If you find yourself copy-pasting error handling logic across services,
44
+
> this library exists to replace that pattern with something cleaner and more sustainable.
51
45
52
46
---
53
47
@@ -63,21 +57,71 @@ pip install apiexception
63
57
64
58
## ⚡ Quickstart: How to Integrate APIException
65
59
66
-
**1️⃣ Register the Handler**
60
+
**1️⃣ Plug and Play**
67
61
68
62
```python
69
-
from api_exception import register_exception_handlers, logger
70
63
from fastapi import FastAPI
64
+
from pydantic import BaseModel, Field
65
+
from api_exception import (
66
+
register_exception_handlers,
67
+
APIException,
68
+
BaseExceptionCode,
69
+
ResponseModel,
70
+
APIResponse,
71
+
)
71
72
72
73
app = FastAPI()
73
74
register_exception_handlers(app) # uses ResponseModel by default
74
75
75
-
logger.setLevel("INFO") # Set logging level if needed
76
+
77
+
classCustomExceptionCode(BaseExceptionCode):
78
+
USER_NOT_FOUND= ("USR-404", "User not found.", "The user ID does not exist.")
79
+
80
+
81
+
classUserModel(BaseModel):
82
+
id: int= Field(..., example=1)
83
+
username: str= Field(..., example="John Doe")
84
+
85
+
86
+
@app.get(
87
+
"/user/{user_id}",
88
+
response_model=ResponseModel[UserModel],
89
+
responses=APIResponse.default(),
90
+
)
91
+
asyncdefuser(user_id: int):
92
+
if user_id ==1:
93
+
raise APIException(
94
+
error_code=CustomExceptionCode.USER_NOT_FOUND,
95
+
http_status_code=404,
96
+
)
97
+
98
+
if user_id ==3:
99
+
a =1
100
+
b =0
101
+
c = a / b # This will raise ZeroDivisionError and be caught by the global exception handler
APIException is actively used in production systems and shared across the Python community.
539
+
If you're using it in your project, a ⭐ on GitHub helps signal support and keeps development active.
482
540
483
541
---
484
542
485
543
## 🌍 Community & Recognition
486
544
487
545
- 📢 Featured in [**Python Weekly #710**](https://www.pythonweekly.com/p/python-weekly-issue-710-august-14-2025-3200567a10d37d87) 🎉
488
-
- 🔥 Ranked **#3** globally in [r/FastAPI](https://www.reddit.com/r/FastAPI/comments/1ma39rq/make_your_fastapi_responses_clean_consistent/) under the *pip package* flair.
546
+
- 🏆 Selected as one of the **Top Python Libraries of 2025** by [**Tryolabs**](https://tryolabs.com/blog/top-python-libraries-2025)
547
+
- 🐍 Shared by [**PythonHub**](https://x.com/PythonHub/status/1957733129000472650) on X (Twitter)
548
+
- 🐦 Shared by [@tom_doerr](https://x.com/tom_doerr/status/1996308358916116964) on X (Twitter)
489
549
- ⭐ Gaining traction on GitHub with developers adopting it for real-world FastAPI projects.
490
550
- 💬 Actively discussed and shared across the Python community.
0 commit comments