diff options
| author | Shyam Sunder | 2021-01-06 10:37:59 -0500 |
|---|---|---|
| committer | Shyam Sunder | 2021-01-06 10:37:59 -0500 |
| commit | c732e62844719f4baf881ff0b9562701f736959e (patch) | |
| tree | 8c5a1662b16415f870d91eee22a9c1690bdcad2f /server | |
| parent | c7461c7f65cfc42e6f33970c47f1030ed2c3a184 (diff) | |
server/net: fix error handling
Diffstat (limited to 'server')
| -rw-r--r-- | server/szurubooru/func/net.py | 23 |
1 files changed, 10 insertions, 13 deletions
diff --git a/server/szurubooru/func/net.py b/server/szurubooru/func/net.py index c153162..e00a6c3 100644 --- a/server/szurubooru/func/net.py +++ b/server/szurubooru/func/net.py @@ -32,18 +32,15 @@ def download(url: str, use_video_downloader: bool = False) -> bytes: content_buffer = b"" length_tally = 0 - with urllib.request.urlopen(request) as handle: - while True: - try: - chunk = handle.read(_dl_chunk_size) - except Exception: - raise DownloadError(url) from None - if not chunk: - break - length_tally += len(chunk) - if length_tally > config.config["max_dl_filesize"]: - raise DownloadTooLargeError(url) - content_buffer += chunk + try: + with urllib.request.urlopen(request) as handle: + while (chunk := handle.read(_dl_chunk_size)) : + length_tally += len(chunk) + if length_tally > config.config["max_dl_filesize"]: + raise DownloadTooLargeError(url) + content_buffer += chunk + except urllib.error.HTTPError as ex: + raise DownloadError(url) from ex return content_buffer @@ -74,7 +71,7 @@ def post_to_webhooks(payload: Dict[str, Any]) -> List[Thread]: 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.data = json.dumps( payload, |