# HTTP Exceptions
[![Publish](https://github.com/DeveloperRSquared/http-exceptions/actions/workflows/publish.yml/badge.svg)](https://github.com/DeveloperRSquared/http-exceptions/actions/workflows/publish.yml)
[![Python 3.7+](https://img.shields.io/badge/python-3.7+-brightgreen.svg)](#http-exceptions)
[![PyPI - License](https://img.shields.io/pypi/l/http-exceptions.svg)](LICENSE)
[![PyPI - Version](https://img.shields.io/pypi/v/http-exceptions.svg)](https://pypi.org/project/http-exceptions)
[![CodeQL](https://github.com/DeveloperRSquared/http-exceptions/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/DeveloperRSquared/http-exceptions/actions/workflows/codeql-analysis.yml)
[![codecov](https://codecov.io/gh/DeveloperRSquared/http-exceptions/branch/main/graph/badge.svg?token=8SJ30A2GV7)](https://codecov.io/gh/DeveloperRSquared/http-exceptions)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/DeveloperRSquared/http-exceptions/main.svg)](https://results.pre-commit.ci/latest/github/DeveloperRSquared/http-exceptions/main)
[![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Checked with mypy](http://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/)
Raisable HTTP Exceptions
## Install
Simply install the package from [PyPI](https://pypi.org/project/http-exceptions/).
```sh
$ pip install -U http-exceptions
```
And that is it, you are ready to raise HTTP Exceptions.
## What is it good for?
1. Saves writing boilerplate code:
Converts this:
```py
# e.g. app/internal.py
def some_function() -> None:
raise SomeError()
# e.g. app/api.py
def api(request: Request) -> Response:
try:
response = some_function()
except SomeError:
response = Response(status_code=403)
return response
```
into this:
```py
# e.g. app/internal.py
from http_exceptions import ForbiddenException
def some_function() -> None:
raise ForbiddenException()
# e.g. app/api.py
def api(request: Request) -> None:
return some_function()
```
2. Dynamic exception raising:
```py
from http_exceptions import HTTPException
def raise_from_status(response: Response) -> None:
if 400 <= response.status < 600:
raise HTTPException.from_status_code(status_code=response.status_code)(message=response.text)
```
```py
>>> response = Response(status_code=403)
>>> raise_from_status(response=response) # ForbiddenException raised
```
## What else?
### `HTTPException`
Base class that provides all the exceptions to be raised.
### `HTTPExceptions.from_status_code(status_code=status_code)`
Returns the relevant Exception corresponding to `status_code`
e.g. `HTTPExceptions.from_status_code(status_code=431)` -> `RequestHeaderFieldsTooLargeException`
### `ClientException`
Subclass of `HTTPException` serving as a base class for exceptions with statuses in the [400, 499] range.
```py
from http_exceptions import ClientException, RequestHeaderFieldsTooLargeException
try:
raise RequestHeaderFieldsTooLargeException # 431 - Client exception
except ClientException:
# exception is caught here
pass
```
### `ServerException`
Subclass of `HTTPException` serving as a base class for exceptions with statuses in the [500, 599] range.
```py
from http_exceptions import HTTPVersionNotSupportedException, ServerException
try:
raise HTTPVersionNotSupportedException # 505 - Server exception
except ServerException:
# exception is caught here
pass
```
## Available Exceptions
### Client Exceptions: `400 <= status <= 499`
```py
400: BadRequestException
401: UnauthorizedException
402: PaymentRequiredException
403: ForbiddenException
404: NotFoundException
405: MethodNotAllowedException
406: NotAcceptableException
407: ProxyAuthenticationRequiredException
408: RequestTimeoutException
409: ConflictException
410: GoneException
411: LengthRequiredException
412: PreconditionFailedException
413: PayloadTooLargeException
414: URITooLongException
415: UnsupportedMediaTypeException
416: RangeNotSatisfiableException
417: ExpectationFailedException
418: ImATeapotException
421: MisdirectedRequestException
422: UnprocessableEntityException
423: LockedException
424: FailedDependencyException
425: TooEarlyException
426: UpgradeRequiredException
428: PreconditionRequiredException
429: TooManyRequestsException
431: RequestHeaderFieldsTooLargeException
444: NoResponseException
451: UnavailableForLegalReasonsException
```
### Server Exceptions: `500 <= status <= 599`
```py
500: InternalServerErrorException
501: NotImplementedException
502: BadGatewayException
503: ServiceUnavailableException
504: GatewayTimeoutException
505: HTTPVersionNotSupportedException
506: VariantAlsoNegotiatesException
507: InsufficientStorageException
508: LoopDetectedException
510: NotExtendedException
511: NetworkAuthenticationRequiredException
```
## Contributing
Contributions are welcome via pull requests.
### First time setup
```sh
$ git clone git@github.com:DeveloperRSquared/http-exceptions.git
$ cd http-exceptions
$ poetry install
$ poetry shell
```
Tools including black, mypy etc. will run automatically if you install [pre-commit](https://pre-commit.com) using the instructions below
```sh
$ pre-commit install
$ pre-commit run --all-files
```
### Running tests
```sh
$ poetry run pytest
```
## Links
- Source Code: <https://github.com/DeveloperRSquared/http-exceptions/>
- PyPI Releases: <https://pypi.org/project/http-exceptions/>
- Issue Tracker: <https://github.com/DeveloperRSquared/http-exceptions/issues/>
Raw data
{
"_id": null,
"home_page": "https://github.com/DeveloperRSquared/http-exceptions",
"name": "http-exceptions",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7,<4.0",
"maintainer_email": "",
"keywords": "python,python3,http,exceptions,fastapi,api,web,rest",
"author": "rikhilrai",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/1a/ee/eed139b5dcd4019d1148eebffb199a22320b9e46e268de3bffcca9a9012c/http-exceptions-0.2.10.tar.gz",
"platform": "",
"description": "# HTTP Exceptions\n\n[![Publish](https://github.com/DeveloperRSquared/http-exceptions/actions/workflows/publish.yml/badge.svg)](https://github.com/DeveloperRSquared/http-exceptions/actions/workflows/publish.yml)\n\n[![Python 3.7+](https://img.shields.io/badge/python-3.7+-brightgreen.svg)](#http-exceptions)\n[![PyPI - License](https://img.shields.io/pypi/l/http-exceptions.svg)](LICENSE)\n[![PyPI - Version](https://img.shields.io/pypi/v/http-exceptions.svg)](https://pypi.org/project/http-exceptions)\n\n[![CodeQL](https://github.com/DeveloperRSquared/http-exceptions/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/DeveloperRSquared/http-exceptions/actions/workflows/codeql-analysis.yml)\n[![codecov](https://codecov.io/gh/DeveloperRSquared/http-exceptions/branch/main/graph/badge.svg?token=8SJ30A2GV7)](https://codecov.io/gh/DeveloperRSquared/http-exceptions)\n[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/DeveloperRSquared/http-exceptions/main.svg)](https://results.pre-commit.ci/latest/github/DeveloperRSquared/http-exceptions/main)\n\n[![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n[![Checked with mypy](http://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/)\n\nRaisable HTTP Exceptions\n\n## Install\n\nSimply install the package from [PyPI](https://pypi.org/project/http-exceptions/).\n\n```sh\n$ pip install -U http-exceptions\n```\n\nAnd that is it, you are ready to raise HTTP Exceptions.\n\n## What is it good for?\n\n1. Saves writing boilerplate code:\n\n Converts this:\n\n ```py\n # e.g. app/internal.py\n def some_function() -> None:\n raise SomeError()\n\n # e.g. app/api.py\n def api(request: Request) -> Response:\n try:\n response = some_function()\n except SomeError:\n response = Response(status_code=403)\n return response\n ```\n\n into this:\n\n ```py\n # e.g. app/internal.py\n from http_exceptions import ForbiddenException\n\n def some_function() -> None:\n raise ForbiddenException()\n\n # e.g. app/api.py\n def api(request: Request) -> None:\n return some_function()\n ```\n\n2. Dynamic exception raising:\n\n ```py\n from http_exceptions import HTTPException\n\n def raise_from_status(response: Response) -> None:\n if 400 <= response.status < 600:\n raise HTTPException.from_status_code(status_code=response.status_code)(message=response.text)\n ```\n\n ```py\n >>> response = Response(status_code=403)\n >>> raise_from_status(response=response) # ForbiddenException raised\n ```\n\n## What else?\n\n### `HTTPException`\n\nBase class that provides all the exceptions to be raised.\n\n### `HTTPExceptions.from_status_code(status_code=status_code)`\n\nReturns the relevant Exception corresponding to `status_code`\n\ne.g. `HTTPExceptions.from_status_code(status_code=431)` -> `RequestHeaderFieldsTooLargeException`\n\n### `ClientException`\n\nSubclass of `HTTPException` serving as a base class for exceptions with statuses in the [400, 499] range.\n\n```py\nfrom http_exceptions import ClientException, RequestHeaderFieldsTooLargeException\n\ntry:\n raise RequestHeaderFieldsTooLargeException # 431 - Client exception\nexcept ClientException:\n # exception is caught here\n pass\n```\n\n### `ServerException`\n\nSubclass of `HTTPException` serving as a base class for exceptions with statuses in the [500, 599] range.\n\n```py\nfrom http_exceptions import HTTPVersionNotSupportedException, ServerException\n\ntry:\n raise HTTPVersionNotSupportedException # 505 - Server exception\nexcept ServerException:\n # exception is caught here\n pass\n```\n\n## Available Exceptions\n\n### Client Exceptions: `400 <= status <= 499`\n\n```py\n400: BadRequestException\n401: UnauthorizedException\n402: PaymentRequiredException\n403: ForbiddenException\n404: NotFoundException\n405: MethodNotAllowedException\n406: NotAcceptableException\n407: ProxyAuthenticationRequiredException\n408: RequestTimeoutException\n409: ConflictException\n410: GoneException\n411: LengthRequiredException\n412: PreconditionFailedException\n413: PayloadTooLargeException\n414: URITooLongException\n415: UnsupportedMediaTypeException\n416: RangeNotSatisfiableException\n417: ExpectationFailedException\n418: ImATeapotException\n421: MisdirectedRequestException\n422: UnprocessableEntityException\n423: LockedException\n424: FailedDependencyException\n425: TooEarlyException\n426: UpgradeRequiredException\n428: PreconditionRequiredException\n429: TooManyRequestsException\n431: RequestHeaderFieldsTooLargeException\n444: NoResponseException\n451: UnavailableForLegalReasonsException\n```\n\n### Server Exceptions: `500 <= status <= 599`\n\n```py\n500: InternalServerErrorException\n501: NotImplementedException\n502: BadGatewayException\n503: ServiceUnavailableException\n504: GatewayTimeoutException\n505: HTTPVersionNotSupportedException\n506: VariantAlsoNegotiatesException\n507: InsufficientStorageException\n508: LoopDetectedException\n510: NotExtendedException\n511: NetworkAuthenticationRequiredException\n```\n\n## Contributing\n\nContributions are welcome via pull requests.\n\n### First time setup\n\n```sh\n$ git clone git@github.com:DeveloperRSquared/http-exceptions.git\n$ cd http-exceptions\n$ poetry install\n$ poetry shell\n```\n\nTools including black, mypy etc. will run automatically if you install [pre-commit](https://pre-commit.com) using the instructions below\n\n```sh\n$ pre-commit install\n$ pre-commit run --all-files\n```\n\n### Running tests\n\n```sh\n$ poetry run pytest\n```\n\n## Links\n\n- Source Code: <https://github.com/DeveloperRSquared/http-exceptions/>\n- PyPI Releases: <https://pypi.org/project/http-exceptions/>\n- Issue Tracker: <https://github.com/DeveloperRSquared/http-exceptions/issues/>\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Raisable HTTP Exceptions",
"version": "0.2.10",
"project_urls": {
"Homepage": "https://github.com/DeveloperRSquared/http-exceptions",
"Repository": "https://github.com/DeveloperRSquared/http-exceptions"
},
"split_keywords": [
"python",
"python3",
"http",
"exceptions",
"fastapi",
"api",
"web",
"rest"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "9dca53101f30b30fe3c6c247aced01aeda263ef4ce8c8dad44f2d0d591a4f0b3",
"md5": "4a87e37ecebdd44e8fec0efe3b8d2a38",
"sha256": "26c97d021f798f8e6c9db477385d2e8f49ae2b26e6acb92b857637fb21cb1e2e"
},
"downloads": -1,
"filename": "http_exceptions-0.2.10-py3-none-any.whl",
"has_sig": false,
"md5_digest": "4a87e37ecebdd44e8fec0efe3b8d2a38",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7,<4.0",
"size": 8843,
"upload_time": "2022-02-15T13:49:45",
"upload_time_iso_8601": "2022-02-15T13:49:45.587995Z",
"url": "https://files.pythonhosted.org/packages/9d/ca/53101f30b30fe3c6c247aced01aeda263ef4ce8c8dad44f2d0d591a4f0b3/http_exceptions-0.2.10-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "1aeeeed139b5dcd4019d1148eebffb199a22320b9e46e268de3bffcca9a9012c",
"md5": "cb6cf1153e73fb0fc55f9471d44390fe",
"sha256": "77484475de13ead5dac72816387dd3c5d3898fd2588af02c1cd5454fb7412c06"
},
"downloads": -1,
"filename": "http-exceptions-0.2.10.tar.gz",
"has_sig": false,
"md5_digest": "cb6cf1153e73fb0fc55f9471d44390fe",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7,<4.0",
"size": 9456,
"upload_time": "2022-02-15T13:49:44",
"upload_time_iso_8601": "2022-02-15T13:49:44.367971Z",
"url": "https://files.pythonhosted.org/packages/1a/ee/eed139b5dcd4019d1148eebffb199a22320b9e46e268de3bffcca9a9012c/http-exceptions-0.2.10.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-02-15 13:49:44",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "DeveloperRSquared",
"github_project": "http-exceptions",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"lcname": "http-exceptions"
}