Skip to main content

Raisable HTTP Exceptions

Project description

HTTP Exceptions

Publish

Python 3.7+ PyPI - License PyPI - Version

CodeQL codecov pre-commit.ci status

Imports: isort Code style: black Checked with mypy

Raisable HTTP Exceptions

Install

Simply install the package from PyPI.

$ 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:

    # 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:

    # 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:

    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)
    
    >>> 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.

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.

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

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

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

$ 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 using the instructions below

$ pre-commit install
$ pre-commit run --all-files

Running tests

$ poetry run pytest

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

http-exceptions-0.2.10.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

http_exceptions-0.2.10-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file http-exceptions-0.2.10.tar.gz.

File metadata

  • Download URL: http-exceptions-0.2.10.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.10.2 Linux/5.11.0-1028-azure

File hashes

Hashes for http-exceptions-0.2.10.tar.gz
Algorithm Hash digest
SHA256 77484475de13ead5dac72816387dd3c5d3898fd2588af02c1cd5454fb7412c06
MD5 cb6cf1153e73fb0fc55f9471d44390fe
BLAKE2b-256 1aeeeed139b5dcd4019d1148eebffb199a22320b9e46e268de3bffcca9a9012c

See more details on using hashes here.

File details

Details for the file http_exceptions-0.2.10-py3-none-any.whl.

File metadata

  • Download URL: http_exceptions-0.2.10-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.10.2 Linux/5.11.0-1028-azure

File hashes

Hashes for http_exceptions-0.2.10-py3-none-any.whl
Algorithm Hash digest
SHA256 26c97d021f798f8e6c9db477385d2e8f49ae2b26e6acb92b857637fb21cb1e2e
MD5 4a87e37ecebdd44e8fec0efe3b8d2a38
BLAKE2b-256 9dca53101f30b30fe3c6c247aced01aeda263ef4ce8c8dad44f2d0d591a4f0b3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page