Package: http-exceptions
HTTP ExceptionsRaisable HTTP ExceptionsInstallSimply install the package fromPyPI.$pipinstall-Uhttp-exceptionsAnd that is it, you are ready to raise HTTP Exceptions.What is it good for?Saves writing boilerplate code:Converts this:# e.g. app/internal.pydefsome_function()->None:raiseSomeError()# e.g. app/api.pydefapi(request:Request)->Response:try:response=some_function()exceptSomeError:response=Response(status_code=403)returnresponseinto this:# e.g. app/internal.pyfromhttp_exceptionsimportForbiddenExceptiondefsome_function()->None:raiseForbiddenException()# e.g. app/api.pydefapi(request:Request)->None:returnsome_function()Dynamic exception raising:fromhttp_exceptionsimportHTTPExceptiondefraise_from_status(response:Response)->None:if400<=response.status<600:raiseHTTPException.from_status_code(status_code=response.status_code)(message=response.text)>>>response=Response(status_code=403)>>>raise_from_status(response=response)# ForbiddenException raisedWhat else?HTTPExceptionBase class that provides all the exceptions to be raised.HTTPExceptions.from_status_code(status_code=status_code)Returns the relevant Exception corresponding tostatus_codee.g.HTTPExceptions.from_status_code(status_code=431)->RequestHeaderFieldsTooLargeExceptionClientExceptionSubclass ofHTTPExceptionserving as a base class for exceptions with statuses in the [400, 499] range.fromhttp_exceptionsimportClientException,RequestHeaderFieldsTooLargeExceptiontry:raiseRequestHeaderFieldsTooLargeException# 431 - Client exceptionexceptClientException:# exception is caught herepassServerExceptionSubclass ofHTTPExceptionserving as a base class for exceptions with statuses in the [500, 599] range.fromhttp_exceptionsimportHTTPVersionNotSupportedException,ServerExceptiontry:raiseHTTPVersionNotSupportedException# 505 - Server exceptionexceptServerException:# exception is caught herepassAvailable ExceptionsClient Exceptions:400 <= status <= 499400:BadRequestException401:UnauthorizedException402:PaymentRequiredException403:ForbiddenException404:NotFoundException405:MethodNotAllowedException406:NotAcceptableException407:ProxyAuthenticationRequiredException408:RequestTimeoutException409:ConflictException410:GoneException411:LengthRequiredException412:PreconditionFailedException413:PayloadTooLargeException414:URITooLongException415:UnsupportedMediaTypeException416:RangeNotSatisfiableException417:ExpectationFailedException418:ImATeapotException421:MisdirectedRequestException422:UnprocessableEntityException423:LockedException424:FailedDependencyException425:TooEarlyException426:UpgradeRequiredException428:PreconditionRequiredException429:TooManyRequestsException431:RequestHeaderFieldsTooLargeException444:NoResponseException451:UnavailableForLegalReasonsExceptionServer Exceptions:500 <= status <= 599500:InternalServerErrorException501:NotImplementedException502:BadGatewayException503:ServiceUnavailableException504:GatewayTimeoutException505:HTTPVersionNotSupportedException506:VariantAlsoNegotiatesException507:InsufficientStorageException508:LoopDetectedException510:NotExtendedException511:NetworkAuthenticationRequiredExceptionContributingContributions are welcome via pull requests.First time setup$gitclonegit@github.com:DeveloperRSquared/http-exceptions.git
$cdhttp-exceptions
$poetryinstall
$poetryshellTools including black, mypy etc. will run automatically if you installpre-commitusing the instructions below$pre-commitinstall
$pre-commitrun--all-filesRunning tests$poetryrunpytestLinksSource Code:https://github.com/DeveloperRSquared/http-exceptions/PyPI Releases:https://pypi.org/project/http-exceptions/Issue Tracker:https://github.com/DeveloperRSquared/http-exceptions/issues/