HTTP Exceptions
Project description
http-exceptions
Raisable HTTP Exceptions
What is it good for?
-
Save writing boilerplate code:
# 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 HTTPException def some_function(): raise ForbiddenException() # e.g. app/api.py def api(request): return some_function()
-
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)(message=response.text) >>> response = Response(status_code=403) >>> raise_from_status(response=response) # ForbiddenException raised
Install http-exceptions
Simply install the package from PyPI.
pip install -U http-exceptions
And that is it, you are ready to raise HTTP Exceptions.
What else?
HTTPException
Base class that provides all the exceptions to be raised.
HTTPExceptions.from_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
try:
raise RequestHeaderFieldsTooLargeException
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 ServerException
try:
raise HTTPVersionNotSupportedException
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
License
This project is licensed under the terms of the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for http_exceptions-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ac768d707caabdf882f9eabf40f34d900f88cc399a64ef6db22015fe9553f50 |
|
MD5 | 1bc6797196e2f7b752084aa16c169c52 |
|
BLAKE2b-256 | aaab645f9dd2fc113e48d3235e4f97fe4f7a96b86622a1682d396cb7ea8a09ae |