https://github.com/developerrsquared/http-exceptions
Raisable HTTP Exceptions
https://github.com/developerrsquared/http-exceptions
api exceptions http python python3 rest web
Last synced: 3 months ago
JSON representation
Raisable HTTP Exceptions
- Host: GitHub
- URL: https://github.com/developerrsquared/http-exceptions
- Owner: DeveloperRSquared
- License: mit
- Created: 2021-10-02T23:41:40.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2025-02-10T17:12:56.000Z (12 months ago)
- Last Synced: 2025-02-10T18:25:04.745Z (12 months ago)
- Topics: api, exceptions, http, python, python3, rest, web
- Language: Python
- Homepage: https://pypi.org/project/http-exceptions/
- Size: 257 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# HTTP Exceptions
[](https://github.com/DeveloperRSquared/http-exceptions/actions/workflows/publish.yml)
[](#http-exceptions)
[](LICENSE)
[](https://pypi.org/project/http-exceptions)
[](https://github.com/DeveloperRSquared/http-exceptions/actions/workflows/codeql-analysis.yml)
[](https://codecov.io/gh/DeveloperRSquared/http-exceptions)
[](https://results.pre-commit.ci/latest/github/DeveloperRSquared/http-exceptions/main)
[](https://pycqa.github.io/isort/)
[](https://github.com/psf/black)
[](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:
- PyPI Releases:
- Issue Tracker: