server/net: fix error handling

This commit is contained in:
Shyam Sunder 2021-01-06 10:37:59 -05:00
parent c7461c7f65
commit c732e62844

View file

@ -32,18 +32,15 @@ def download(url: str, use_video_downloader: bool = False) -> bytes:
content_buffer = b"" content_buffer = b""
length_tally = 0 length_tally = 0
with urllib.request.urlopen(request) as handle: try:
while True: with urllib.request.urlopen(request) as handle:
try: while (chunk := handle.read(_dl_chunk_size)) :
chunk = handle.read(_dl_chunk_size) length_tally += len(chunk)
except Exception: if length_tally > config.config["max_dl_filesize"]:
raise DownloadError(url) from None raise DownloadTooLargeError(url)
if not chunk: content_buffer += chunk
break except urllib.error.HTTPError as ex:
length_tally += len(chunk) raise DownloadError(url) from ex
if length_tally > config.config["max_dl_filesize"]:
raise DownloadTooLargeError(url)
content_buffer += chunk
return content_buffer return content_buffer
@ -74,7 +71,7 @@ def post_to_webhooks(payload: Dict[str, Any]) -> List[Thread]:
return threads return threads
def _post_to_webhook(webhook: str, payload: Dict[str, Any]) -> None: def _post_to_webhook(webhook: str, payload: Dict[str, Any]) -> int:
req = urllib.request.Request(webhook) req = urllib.request.Request(webhook)
req.data = json.dumps( req.data = json.dumps(
payload, payload,