Display exception errors in console

This commit is contained in:
Ivan Habunek 2024-03-28 12:35:11 +01:00
parent 9bf0f1b425
commit 96526c2ca5
No known key found for this signature in database
GPG Key ID: F5F0623FF5EBCB3D
3 changed files with 24 additions and 21 deletions

View File

@ -1,15 +1,13 @@
import os
import httpx
from twitchdl.exceptions import ConsoleError
CHUNK_SIZE = 1024
CONNECT_TIMEOUT = 5
RETRY_COUNT = 5
class DownloadFailed(Exception):
pass
def _download(url: str, path: str):
tmp_path = path + ".tmp"
size = 0
@ -35,4 +33,4 @@ def download_file(url: str, path: str, retries: int = RETRY_COUNT):
except httpx.RequestError:
pass
raise DownloadFailed(":(")
raise ConsoleError(f"Failed downloading after {retries} attempts: {url}")

View File

@ -1,4 +1,5 @@
import click
class ConsoleError(Exception):
class ConsoleError(click.ClickException):
"""Raised when an error occurs and script exectuion should halt."""
pass

View File

@ -4,16 +4,19 @@ Twitch API access.
import httpx
import json
import click
from typing import Dict
from twitchdl import CLIENT_ID
from twitchdl.exceptions import ConsoleError
class GQLError(Exception):
def __init__(self, errors):
super().__init__("GraphQL query failed")
self.errors = errors
class GQLError(click.ClickException):
def __init__(self, errors: list[str]):
message = "GraphQL query failed."
for error in errors:
message += f"\n* {error}"
super().__init__(message)
def authenticated_post(url, data=None, json=None, headers={}):
@ -29,24 +32,25 @@ def authenticated_post(url, data=None, json=None, headers={}):
return response
def gql_post(query):
def gql_post(query: str):
url = "https://gql.twitch.tv/gql"
response = authenticated_post(url, data=query).json()
if "errors" in response:
raise GQLError(response["errors"])
return response
response = authenticated_post(url, data=query)
gql_raise_on_error(response)
return response.json()
def gql_query(query: str, headers: Dict[str, str] = {}):
url = "https://gql.twitch.tv/gql"
response = authenticated_post(url, json={"query": query}, headers=headers).json()
response = authenticated_post(url, json={"query": query}, headers=headers)
gql_raise_on_error(response)
return response.json()
if "errors" in response:
raise GQLError(response["errors"])
return response
def gql_raise_on_error(response: httpx.Response):
data = response.json()
if "errors" in data:
errors = [e["message"] for e in data["errors"]]
raise GQLError(errors)
VIDEO_FIELDS = """