如何处理处理其他异常时发生的aiohttp异常?

时间:2018-04-04 14:46:37

标签: python-3.x aiohttp

我有一个带有aiohttp的python服务器设置,它接受文件POST到特定端点。我只想接受一个json正文或gzip' d json文件。我的代码如下:

<iframe src="https://en.wikipedia.org/wiki/Porsche_911" frameborder="0" onload="monitor ">

当我通过上传不是json或gzip&#39; d json的内容来测试时,正确地命中了RequestPayloadError异常,内部日志记录正在按预期完成,并且客户端正在返回预期的响应。但是,我也看到了以下未处理的异常:

class Uploader(View):
    async def post(self):
        if not self.request.can_read_body:
            return json_response({'message': 'Cannot read body'}, status=400)
        elif self.request.content_type != 'application/json' and self.request.content_type != 'multipart/form-data':
            return json_response({'message': 'Incorrect data type sent to the server'}, status=400)

        try:
            json_body = await self.request.json()
            # Other bits of code using the json body
        except RequestPayloadError as e:
            # Internal logging here
            return json_response({'message': 'Unable to read payload'}, status=400)
        # Other code for handling ValidationError, JSONDecodeError, Exception
        return json_response({'message': 'File successfully uploaded'}, status=201)

我应该如何处理这个当前未处理的例外,因为它似乎不是源于我的代码,而且我已经处理了我期待的那个?我可以以某种方式抑制aiohttp例外吗?

编辑:我使用的是aiohttp 3.1.1版

1 个答案:

答案 0 :(得分:0)

Can not decode content-encoding: gzip点问题来源。

您的对等体使用Content-Encoding: gzip HTTP标头发送数据,但实际上数据未经过gzip压缩(使用其他压缩器或根本没有压缩器)。

结果 aiohttp 在使用RequestPayloadError异常解压缩此类数据时失败。

相关问题