def _exception_for_non_200_status(status: int, uri: str) -> HTTPError: return HTTPError( "Received a very surprising HTTP status " "(%(status)i) for %(uri)s" % locals(), status, uri, )
def _exception_for_5xx_status(status: int, uri: str) -> HTTPError: return HTTPError( "Received a server error (%(status)i) for " "%(uri)s" % locals(), status, uri, )
def _exception_for_non_200_status(status: int, uri: str, body: Optional[str]) -> HTTPError: return HTTPError( f"Received a very surprising HTTP status ({status}) for {uri}", status, uri, body, )
def _exception_for_5xx_status(status: int, uri: str, body: Optional[str]) -> HTTPError: return HTTPError( f"Received a server error ({status}) for {uri}", status, uri, body, )
def _exception_for_4xx_status(self, status: int, content_type: str, body: str, uri: str) -> GeoIP2Error: if not body: return HTTPError( "Received a %(status)i error for %(uri)s " "with no body." % locals(), status, uri, body, ) if content_type.find("json") == -1: return HTTPError( "Received a %i for %s with the following " "body: %s" % (status, uri, str(content_type)), status, uri, body, ) try: decoded_body = json.loads(body) except ValueError as ex: return HTTPError( "Received a %(status)i error for %(uri)s but it did" " not include the expected JSON body: " % locals() + ", ".join(ex.args), status, uri, body, ) else: if "code" in decoded_body and "error" in decoded_body: return self._exception_for_web_service_error( decoded_body.get("error"), decoded_body.get("code"), status, uri) return HTTPError( "Response contains JSON but it does not specify " "code or error keys", status, uri, body, )
def _exception_for_4xx_status( self, status: int, content_type: str, body: str, uri: str ) -> GeoIP2Error: if not body: return HTTPError( f"Received a {status} error for {uri} with no body.", status, uri, body, ) if content_type.find("json") == -1: return HTTPError( f"Received a {status} for {uri} with the following body: {body}", status, uri, body, ) try: decoded_body = json.loads(body) except ValueError as ex: return HTTPError( f"Received a {status} error for {uri} but it did not include " + "the expected JSON body: " + ", ".join(ex.args), status, uri, body, ) else: if "code" in decoded_body and "error" in decoded_body: return self._exception_for_web_service_error( decoded_body.get("error"), decoded_body.get("code"), status, uri ) return HTTPError( "Response contains JSON but it does not specify code or error keys", status, uri, body, )