APIException


NameAPIException JSON
Version 0.1.13 PyPI version JSON
download
home_pageNone
SummaryA customizable exception handling library for FastAPI
upload_time2025-07-21 03:31:22
maintainerNone
docs_urlNone
authorAhmet Kutay URAL
requires_python>=3.6
licenseNone
keywords
VCS
bugtrack_url
requirements pydantic uvicorn fastapi httpx
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # APIException for FastAPI
[![PyPI version](https://img.shields.io/pypi/v/APIException?cacheSeconds=60)](https://pypi.org/project/APIException/)
[![Documentation](https://img.shields.io/badge/docs-latest-brightgreen.svg)](https://akutayural.github.io/APIException/)
[![Downloads](https://pepy.tech/badge/APIException)](https://pepy.tech/project/APIException)
[![Python Versions](https://img.shields.io/pypi/pyversions/APIException.svg)](https://pypi.org/project/APIException/)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Documentation](https://img.shields.io/badge/docs-latest-brightgreen.svg)](https://akutayural.github.io/APIException/)

**APIException** is a fully extensible exception-handling library for **FastAPI**, designed to help you **standardize error responses**, **manage custom error codes**, and ensure **predictable, well-documented APIs** β€” from day one.

- πŸ”’ Consistent JSON responses for **both** success and errors.
- πŸ“š Beautiful Swagger/OpenAPI documentation with clear error cases.
- βš™οΈ Customizable error codes with `BaseExceptionCode`.
- πŸ”— Global fallback for unhandled server-side errors.
- πŸ—‚οΈ Use with **multiple FastAPI apps**.
- πŸ“œ Automatic logging of every exception detail.
- βœ”οΈ Production-ready with unit test examples.

Β· [**View on PyPI**](https://pypi.org/project/APIException) 

Β· [**Full Documentation**](https://akutayural.github.io/APIException/)

Reading the [full documentation](https://akutayural.github.io/APIException/) is **highly recommended** β€” it’s clear, thorough, and helps you get started in minutes.

---

## πŸ“¦ Installation

```bash
pip install APIException==0.1.12
```

![pip-install-APIException.gif](pip-install-APIException.gif)

---

## ⚑ Quickstart

**1️⃣ Register the Handler**

```python
from APIException import register_exception_handlers
from fastapi import FastAPI

app = FastAPI()
register_exception_handlers(app)  # uses ResponseModel by default

# Use raw dict instead:
# register_exception_handlers(app, use_response_model=False)
```

---

## πŸ” See It In Action

```python
from fastapi import FastAPI, Path
from APIException import APIException, ExceptionStatus, register_exception_handlers, ResponseModel, APIResponse, BaseExceptionCode
from pydantic import BaseModel

app = FastAPI()

# Register exception handlers globally to have the consistent
# error handling and response structure
register_exception_handlers(app=app)

# Create the validation model for your response
class UserResponse(BaseModel):
    id: int
    username: str

# Define your custom exception codes extending BaseExceptionCode
class CustomExceptionCode(BaseExceptionCode):
    USER_NOT_FOUND = ("USR-404", "User not found.", "The user ID does not exist.")
    INVALID_API_KEY = ("API-401", "Invalid API key.", "Provide a valid API key.")
    PERMISSION_DENIED = ("PERM-403", "Permission denied.", "Access to this resource is forbidden.")


@app.get("/user/{user_id}",
    response_model=ResponseModel[UserResponse],
    responses=APIResponse.custom(
        (401, CustomExceptionCode.INVALID_API_KEY),
        (403, CustomExceptionCode.PERMISSION_DENIED)
    )
)
async def user(user_id: int = Path()):
    if user_id == 1:
        raise APIException(
            error_code=CustomExceptionCode.USER_NOT_FOUND,
            http_status_code=401,
        )
    data = UserResponse(id=1, username="John Doe")
    return ResponseModel[UserResponse](
        data=data,
        description="User found and returned."
    )
```

![_user_{user_id}.gif](_user_{user_id}.gif)

---



**2️⃣ Raise an Exception**

```python
from APIException import APIException, ExceptionCode, register_exception_handlers
from fastapi import FastAPI
app = FastAPI()

register_exception_handlers(app)

@app.get("/login")
async def login(username: str, password: str):
    if username != "admin" or password != "admin":
        raise APIException(
            error_code=ExceptionCode.AUTH_LOGIN_FAILED,
            http_status_code=401
        )
    return {"message": "Login successful!"}
```

---

**3️⃣ Use ResponseModel for Success Responses**

```python
from APIException import ResponseModel, register_exception_handlers
from fastapi import FastAPI
app = FastAPI()

register_exception_handlers(app)

@app.get("/success")
async def success():
    return ResponseModel(
        data={"foo": "bar"},
        message="Everything went fine!"
    )
```

**_Response Model In Abstract:_**


![response_model.gif](response_model.gif)


---

## 🧩 Custom Error Codes

Always extend BaseExceptionCode β€” don’t subclass ExceptionCode directly!

```python
from APIException import BaseExceptionCode

class CustomExceptionCode(BaseExceptionCode):
    USER_NOT_FOUND = ("USR-404", "User not found.", "User does not exist.")
    INVALID_API_KEY = ("API-401", "Invalid API key.", "Key missing or invalid.")
```

And use it:

```python
from APIException import APIException

raise APIException(
    error_code=CustomExceptionCode.USER_NOT_FOUND,
    http_status_code=404
)
```

---

## βš™οΈ Override Default HTTP Status Codes

```python
from APIException import set_default_http_codes

set_default_http_codes({
    "FAIL": 422,
    "WARNING": 202
})
```

---

## 🌐 Multiple Apps Support
```python
from fastapi import FastAPI
from APIException import register_exception_handlers

mobile_app = FastAPI()
admin_app = FastAPI()
merchant_app = FastAPI()

register_exception_handlers(mobile_app)
register_exception_handlers(admin_app)
register_exception_handlers(merchant_app)
```

---

## πŸ“ Automatic Logging

Every APIException automatically logs:

- File name & line number

- Error code, status, message, description

Or use the built-in logger:

```python
from APIException import logger

logger.info("Custom info log")
logger.error("Custom error log")
```

---

## βœ… Testing Example

```python
import unittest
from APIException import APIException, ExceptionCode, ResponseModel

class TestAPIException(unittest.TestCase):
    def test_api_exception(self):
        exc = APIException(error_code=ExceptionCode.AUTH_LOGIN_FAILED)
        self.assertEqual(exc.status.value, "FAIL")

    def test_response_model(self):
        res = ResponseModel(data={"foo": "bar"})
        self.assertEqual(res.status.value, "SUCCESS")

if __name__ == "__main__":
    unittest.main()
```

**Run the Tests**
- To run the tests, you can use the following command in your terminal:

```bash
python -m unittest discover -s tests
```

---

## πŸ”— Full Documentation

Find detailed guides and examples in the [official docs](https://akutayural.github.io/APIException/).

---

## πŸ“œ Changelog

**v0.1.13 (2025-07-21)**

βœ… **Initial stable version**

- /examples/fastapi_usage.py has been updated.

- 422 Pydantic error has been fixed in APIResponse.default()

- Documentation has been updated.

- Exception Args has been added to the logs.

- Readme has been updated. New gifs have been added.


**v0.1.12 (2025-07-14)**

- /examples/fastapi_usage.py has been updated.

- 422 Pydantic error has been handled in register_handler

- Documentation has been added.

- `use_fallback_middleware` has been added.

**v0.1.11 (2025-07-13)**

- Added CLI entrypoint (api_exception-info)

- Stable test suite with FastAPI TestClient

- Multiple app support

- Raw dict or Pydantic output

- Automatic logging improvements


**v0.1.0 (2025-06-25)**


πŸš€ Prototype started!

- Project scaffolding

- `ResponseModel` has been added

- `APIException` has been added

- Defined base ideas for standardizing error handling

---

## License
This project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for more details.
If you like this library and find it useful, don’t forget to give it a ⭐ on GitHub!

## Contact
If you have any questions or suggestions, please feel free to reach out at [ahmetkutayural.dev](https://ahmetkutayural.dev/#contact)





            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "APIException",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": null,
    "author": "Ahmet Kutay URAL",
    "author_email": "ahmetkutayural@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/2a/03/a381577aec13fe1bcc38263302352102f91719acb68e75083ffcaeb8745c/APIException-0.1.13.tar.gz",
    "platform": null,
    "description": "# APIException for FastAPI\n[![PyPI version](https://img.shields.io/pypi/v/APIException?cacheSeconds=60)](https://pypi.org/project/APIException/)\n[![Documentation](https://img.shields.io/badge/docs-latest-brightgreen.svg)](https://akutayural.github.io/APIException/)\n[![Downloads](https://pepy.tech/badge/APIException)](https://pepy.tech/project/APIException)\n[![Python Versions](https://img.shields.io/pypi/pyversions/APIException.svg)](https://pypi.org/project/APIException/)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n[![Documentation](https://img.shields.io/badge/docs-latest-brightgreen.svg)](https://akutayural.github.io/APIException/)\n\n**APIException** is a fully extensible exception-handling library for **FastAPI**, designed to help you **standardize error responses**, **manage custom error codes**, and ensure **predictable, well-documented APIs** \u2014 from day one.\n\n- \ud83d\udd12 Consistent JSON responses for **both** success and errors.\n- \ud83d\udcda Beautiful Swagger/OpenAPI documentation with clear error cases.\n- \u2699\ufe0f Customizable error codes with `BaseExceptionCode`.\n- \ud83d\udd17 Global fallback for unhandled server-side errors.\n- \ud83d\uddc2\ufe0f Use with **multiple FastAPI apps**.\n- \ud83d\udcdc Automatic logging of every exception detail.\n- \u2714\ufe0f Production-ready with unit test examples.\n\n\u00b7 [**View on PyPI**](https://pypi.org/project/APIException) \n\n\u00b7 [**Full Documentation**](https://akutayural.github.io/APIException/)\n\nReading the [full documentation](https://akutayural.github.io/APIException/) is **highly recommended** \u2014 it\u2019s clear, thorough, and helps you get started in minutes.\n\n---\n\n## \ud83d\udce6 Installation\n\n```bash\npip install APIException==0.1.12\n```\n\n![pip-install-APIException.gif](pip-install-APIException.gif)\n\n---\n\n## \u26a1 Quickstart\n\n**1\ufe0f\u20e3 Register the Handler**\n\n```python\nfrom APIException import register_exception_handlers\nfrom fastapi import FastAPI\n\napp = FastAPI()\nregister_exception_handlers(app)  # uses ResponseModel by default\n\n# Use raw dict instead:\n# register_exception_handlers(app, use_response_model=False)\n```\n\n---\n\n## \ud83d\udd0d See It In Action\n\n```python\nfrom fastapi import FastAPI, Path\nfrom APIException import APIException, ExceptionStatus, register_exception_handlers, ResponseModel, APIResponse, BaseExceptionCode\nfrom pydantic import BaseModel\n\napp = FastAPI()\n\n# Register exception handlers globally to have the consistent\n# error handling and response structure\nregister_exception_handlers(app=app)\n\n# Create the validation model for your response\nclass UserResponse(BaseModel):\n    id: int\n    username: str\n\n# Define your custom exception codes extending BaseExceptionCode\nclass CustomExceptionCode(BaseExceptionCode):\n    USER_NOT_FOUND = (\"USR-404\", \"User not found.\", \"The user ID does not exist.\")\n    INVALID_API_KEY = (\"API-401\", \"Invalid API key.\", \"Provide a valid API key.\")\n    PERMISSION_DENIED = (\"PERM-403\", \"Permission denied.\", \"Access to this resource is forbidden.\")\n\n\n@app.get(\"/user/{user_id}\",\n    response_model=ResponseModel[UserResponse],\n    responses=APIResponse.custom(\n        (401, CustomExceptionCode.INVALID_API_KEY),\n        (403, CustomExceptionCode.PERMISSION_DENIED)\n    )\n)\nasync def user(user_id: int = Path()):\n    if user_id == 1:\n        raise APIException(\n            error_code=CustomExceptionCode.USER_NOT_FOUND,\n            http_status_code=401,\n        )\n    data = UserResponse(id=1, username=\"John Doe\")\n    return ResponseModel[UserResponse](\n        data=data,\n        description=\"User found and returned.\"\n    )\n```\n\n![_user_{user_id}.gif](_user_{user_id}.gif)\n\n---\n\n\n\n**2\ufe0f\u20e3 Raise an Exception**\n\n```python\nfrom APIException import APIException, ExceptionCode, register_exception_handlers\nfrom fastapi import FastAPI\napp = FastAPI()\n\nregister_exception_handlers(app)\n\n@app.get(\"/login\")\nasync def login(username: str, password: str):\n    if username != \"admin\" or password != \"admin\":\n        raise APIException(\n            error_code=ExceptionCode.AUTH_LOGIN_FAILED,\n            http_status_code=401\n        )\n    return {\"message\": \"Login successful!\"}\n```\n\n---\n\n**3\ufe0f\u20e3 Use ResponseModel for Success Responses**\n\n```python\nfrom APIException import ResponseModel, register_exception_handlers\nfrom fastapi import FastAPI\napp = FastAPI()\n\nregister_exception_handlers(app)\n\n@app.get(\"/success\")\nasync def success():\n    return ResponseModel(\n        data={\"foo\": \"bar\"},\n        message=\"Everything went fine!\"\n    )\n```\n\n**_Response Model In Abstract:_**\n\n\n![response_model.gif](response_model.gif)\n\n\n---\n\n## \ud83e\udde9 Custom Error Codes\n\nAlways extend BaseExceptionCode \u2014 don\u2019t subclass ExceptionCode directly!\n\n```python\nfrom APIException import BaseExceptionCode\n\nclass CustomExceptionCode(BaseExceptionCode):\n    USER_NOT_FOUND = (\"USR-404\", \"User not found.\", \"User does not exist.\")\n    INVALID_API_KEY = (\"API-401\", \"Invalid API key.\", \"Key missing or invalid.\")\n```\n\nAnd use it:\n\n```python\nfrom APIException import APIException\n\nraise APIException(\n    error_code=CustomExceptionCode.USER_NOT_FOUND,\n    http_status_code=404\n)\n```\n\n---\n\n## \u2699\ufe0f Override Default HTTP Status Codes\n\n```python\nfrom APIException import set_default_http_codes\n\nset_default_http_codes({\n    \"FAIL\": 422,\n    \"WARNING\": 202\n})\n```\n\n---\n\n## \ud83c\udf10 Multiple Apps Support\n```python\nfrom fastapi import FastAPI\nfrom APIException import register_exception_handlers\n\nmobile_app = FastAPI()\nadmin_app = FastAPI()\nmerchant_app = FastAPI()\n\nregister_exception_handlers(mobile_app)\nregister_exception_handlers(admin_app)\nregister_exception_handlers(merchant_app)\n```\n\n---\n\n## \ud83d\udcdd Automatic Logging\n\nEvery APIException automatically logs:\n\n- File name & line number\n\n- Error code, status, message, description\n\nOr use the built-in logger:\n\n```python\nfrom APIException import logger\n\nlogger.info(\"Custom info log\")\nlogger.error(\"Custom error log\")\n```\n\n---\n\n## \u2705 Testing Example\n\n```python\nimport unittest\nfrom APIException import APIException, ExceptionCode, ResponseModel\n\nclass TestAPIException(unittest.TestCase):\n    def test_api_exception(self):\n        exc = APIException(error_code=ExceptionCode.AUTH_LOGIN_FAILED)\n        self.assertEqual(exc.status.value, \"FAIL\")\n\n    def test_response_model(self):\n        res = ResponseModel(data={\"foo\": \"bar\"})\n        self.assertEqual(res.status.value, \"SUCCESS\")\n\nif __name__ == \"__main__\":\n    unittest.main()\n```\n\n**Run the Tests**\n- To run the tests, you can use the following command in your terminal:\n\n```bash\npython -m unittest discover -s tests\n```\n\n---\n\n## \ud83d\udd17 Full Documentation\n\nFind detailed guides and examples in the [official docs](https://akutayural.github.io/APIException/).\n\n---\n\n## \ud83d\udcdc Changelog\n\n**v0.1.13 (2025-07-21)**\n\n\u2705 **Initial stable version**\n\n- /examples/fastapi_usage.py has been updated.\n\n- 422 Pydantic error has been fixed in APIResponse.default()\n\n- Documentation has been updated.\n\n- Exception Args has been added to the logs.\n\n- Readme has been updated. New gifs have been added.\n\n\n**v0.1.12 (2025-07-14)**\n\n- /examples/fastapi_usage.py has been updated.\n\n- 422 Pydantic error has been handled in register_handler\n\n- Documentation has been added.\n\n- `use_fallback_middleware` has been added.\n\n**v0.1.11 (2025-07-13)**\n\n- Added CLI entrypoint (api_exception-info)\n\n- Stable test suite with FastAPI TestClient\n\n- Multiple app support\n\n- Raw dict or Pydantic output\n\n- Automatic logging improvements\n\n\n**v0.1.0 (2025-06-25)**\n\n\n\ud83d\ude80 Prototype started!\n\n- Project scaffolding\n\n- `ResponseModel` has been added\n\n- `APIException` has been added\n\n- Defined base ideas for standardizing error handling\n\n---\n\n## License\nThis project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for more details.\nIf you like this library and find it useful, don\u2019t forget to give it a \u2b50 on GitHub!\n\n## Contact\nIf you have any questions or suggestions, please feel free to reach out at [ahmetkutayural.dev](https://ahmetkutayural.dev/#contact)\n\n\n\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A customizable exception handling library for FastAPI",
    "version": "0.1.13",
    "project_urls": {
        "Documentation": "https://akutayural.github.io/APIException/",
        "PyPI": "https://pypi.org/project/APIException/",
        "Source": "https://github.com/akutayural/APIException"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "daec8f3cef26ff8341d389dceebcd387a537b1133f2f575e0c78a00b7270c358",
                "md5": "852d7a2883ab26eb8eb140183b8092d0",
                "sha256": "d586fc902a903b4aaf69d40219f40087485759850abeb361cc658858982bdbde"
            },
            "downloads": -1,
            "filename": "apiexception-0.1.13-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "852d7a2883ab26eb8eb140183b8092d0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 17984,
            "upload_time": "2025-07-21T03:33:45",
            "upload_time_iso_8601": "2025-07-21T03:33:45.883702Z",
            "url": "https://files.pythonhosted.org/packages/da/ec/8f3cef26ff8341d389dceebcd387a537b1133f2f575e0c78a00b7270c358/apiexception-0.1.13-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "209c58bfa6aee07bb80b2111d10979e02598566e0402da34a47c18b05ed7d993",
                "md5": "c80220a4825f035060fada31b0f97011",
                "sha256": "c39e95241e5f5628e16a975c644067a011e59f96abc3c5a2e99ed9bf19f94ea0"
            },
            "downloads": -1,
            "filename": "APIException-0.1.13-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c80220a4825f035060fada31b0f97011",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 17915,
            "upload_time": "2025-07-21T03:31:20",
            "upload_time_iso_8601": "2025-07-21T03:31:20.687734Z",
            "url": "https://files.pythonhosted.org/packages/20/9c/58bfa6aee07bb80b2111d10979e02598566e0402da34a47c18b05ed7d993/APIException-0.1.13-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "2a03a381577aec13fe1bcc38263302352102f91719acb68e75083ffcaeb8745c",
                "md5": "5dfec0aa6a29250d1935170d441b2897",
                "sha256": "dfbdbe0fae692fb3a5093908551064bc83e19770ab3e3e92fde15f6b029f3641"
            },
            "downloads": -1,
            "filename": "APIException-0.1.13.tar.gz",
            "has_sig": false,
            "md5_digest": "5dfec0aa6a29250d1935170d441b2897",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 14786,
            "upload_time": "2025-07-21T03:31:22",
            "upload_time_iso_8601": "2025-07-21T03:31:22.298002Z",
            "url": "https://files.pythonhosted.org/packages/2a/03/a381577aec13fe1bcc38263302352102f91719acb68e75083ffcaeb8745c/APIException-0.1.13.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-21 03:31:22",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "akutayural",
    "github_project": "APIException",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "pydantic",
            "specs": [
                [
                    ">=",
                    "2.0.0"
                ]
            ]
        },
        {
            "name": "uvicorn",
            "specs": [
                [
                    "==",
                    "0.32.0"
                ]
            ]
        },
        {
            "name": "fastapi",
            "specs": [
                [
                    "==",
                    "0.115.4"
                ]
            ]
        },
        {
            "name": "httpx",
            "specs": [
                [
                    "==",
                    "0.27.0"
                ]
            ]
        }
    ],
    "lcname": "apiexception"
}
        
Elapsed time: 0.76011s