Include unhandled error body in force-JSON middleware

This commit is contained in:
Tulir Asokan 2021-12-12 21:15:01 +02:00
parent d87158be50
commit 33bc777bfd

View File

@ -15,6 +15,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from typing import Callable, Awaitable from typing import Callable, Awaitable
import logging import logging
import base64
from aiohttp import web from aiohttp import web
@ -52,7 +53,12 @@ async def error(request: web.Request, handler: Handler) -> web.Response:
elif ex.status_code == 405: elif ex.status_code == 405:
return resp.method_not_allowed return resp.method_not_allowed
return web.json_response({ return web.json_response({
"error": f"Unhandled HTTP {ex.status}", "httpexception": {
"headers": {key: value for key, value in ex.headers.items()},
"class": type(ex).__name__,
"body": ex.text or base64.b64encode(ex.body)
},
"error": f"Unhandled HTTP {ex.status}: {ex.text[:128] or 'non-text response'}",
"errcode": f"unhandled_http_{ex.status}", "errcode": f"unhandled_http_{ex.status}",
}, status=ex.status) }, status=ex.status)
except Exception: except Exception: