async def _raise_for_status(resp: aiohttp.ClientResponse) -> None: """Check resp for status and if error log additional info.""" # Copied from aiohttp's raise_for_status() -- since it releases the # response payload, we need to grab the `resp.text` first to help users # debug. # # Useability/performance notes: # * grabbing the response can be slow for large files, only do it as # needed # * we can't know in advance what encoding the files might have unless # we're certain in advance that the result is an error payload from # Google (otherwise, it could be a binary blob from GCS, for example) # * sometimes, errors are expected, so we should try to avoid polluting # logs in that case # # https://github.com/aio-libs/aiohttp/blob/ # 385b03ef21415d062886e1caab74eb5b93fdb887/aiohttp/ # client_reqrep.py#L892-L902 if resp.status >= 400: assert resp.reason is not None # Google's error messages are useful, pass 'em through body = await resp.text(errors='replace') resp.release() raise aiohttp.ClientResponseError(resp.request_info, resp.history, status=resp.status, message=f'{resp.reason}: {body}', headers=resp.headers)
async def _raise_for_status(self, resp: aiohttp.ClientResponse) -> None: if resp.status >= 400: explanation = await resp.text() resp.release() logger.debug("Server responded:\n%s\n%s", explanation, "=" * 40) raise SPARQLRequestFailed(resp.request_info, resp.history, code=resp.status, message=resp.reason, explanation=explanation)
def raise_not_200(response: aiohttp.ClientResponse) -> None: if response.status != 200: assert response.reason is not None response.release() raise aiohttp.ClientResponseError( response.request_info, response.history, status=response.status, message=response.reason, headers=response.headers, )
def aiohttp_raise_for_status(response: aiohttp.ClientResponse): # workaround aiohttp bug, can remove after fixed in aiohttp # issue: https://github.com/aio-libs/aiohttp/issues/3906 if response.status >= 400: response.release() raise aiohttp.ClientResponseError( response.request_info, response.history, status=response.status, message=response.reason, headers=response.headers, )
async def handle_error_response(self, response: aiohttp.ClientResponse) -> None: if response.status >= 400: try: body = await response.json() except aiohttp.ContentTypeError: body = await response.text() response.release() raise HttpResponseException(response.request_info, response.history, status=response.status, reason=response.reason, headers=response.headers, body=body)