properly handle failed connections to masto

This commit is contained in:
Izalia Mae 2019-12-17 09:05:38 -05:00
parent 4838761dc7
commit 329bc8356e
2 changed files with 15 additions and 7 deletions

View file

@ -1,6 +1,7 @@
import aiohttp
import re
import json
import logging
from colour import Color
@ -11,7 +12,8 @@ error_codes = {
401: 'Unauthorized',
403: 'Forbidden',
404: 'NotFound',
500: 'InternalServerError'
500: 'InternalServerError',
504: 'GatewayTimeout'
}

View file

@ -4,9 +4,11 @@ import json
import logging
import binascii
import base64
import traceback
from urllib.parse import urlparse
from aiohttp.http_exceptions import *
from aiohttp.client_exceptions import *
from .signature import validate, pass_hash
from .functions import json_error, user_check
@ -49,14 +51,18 @@ async def passthrough(path, headers, post=None, query=None):
url = urlparse(path).path
querydata = query if query else ''
async with aiohttp.request(reqtype, f'https://{MASTOCONFIG["domain"]}/{path}{query}', headers=headers, data=post) as resp:
if resp.status not in [200, 202]:
logging.warning(f'Recieved error {resp.status} from Mastodon')
#json_error(500, f'Failed to forward request. Recieved error {resp.status} from Mastodon')
try:
async with aiohttp.request(reqtype, f'https://{MASTOCONFIG["domain"]}/{path}{query}', headers=headers, data=post) as resp:
if resp.status not in [200, 202]:
logging.warning(f'Recieved error {resp.status} from Mastodon')
json_error(504, f'Failed to forward request. Recieved error {resp.status} from Mastodon')
data = await resp.read()
data = await resp.read()
raise aiohttp.web.HTTPOk(body=data, content_type=resp.content_type)
raise aiohttp.web.HTTPOk(body=data, content_type=resp.content_type)
except ClientConnectorError:
return json_error(504, f'Failed to connect to Mastodon')
async def http_redirect(app, handler):