From a197760d1957b12b2b53ded84922ab971c9f9297 Mon Sep 17 00:00:00 2001 From: Ayo Date: Sat, 6 Jun 2026 11:35:51 +0200 Subject: [PATCH] feat: new error template to display fetch errors --- templates/_error.html | 191 ++++++++++++++++++++++++++++++++++++++++++ threads.py | 53 +++++++----- 2 files changed, 222 insertions(+), 22 deletions(-) create mode 100644 templates/_error.html diff --git a/templates/_error.html b/templates/_error.html new file mode 100644 index 0000000..3792661 --- /dev/null +++ b/templates/_error.html @@ -0,0 +1,191 @@ + + + + + + + {{ app.title }} + + {% if threads|length == 1 %} + + + {% else %} + + + {% endif %} + + + + + + + + + + + + + {% include "styles.html" %} + + + + + + +
+ {% include "nav.html" %} + +

{{ app.title }}

+
+

{{ app.description }}

+ +
+
+

Whoa!

+

{{message}}

+
+ + + + + + + diff --git a/threads.py b/threads.py index 50fff6f..2fb15ca 100755 --- a/threads.py +++ b/threads.py @@ -48,8 +48,9 @@ def get_account_tagged_statuses(tag_name): statuses = [utils.clean_status(s) for s in statuses] return statuses else: - current_app.logger.error(f"get_account_tagged_statuses returned: {response.status_code}", url) - return [] + message=f"get_account_tagged_statuses returned: {response.status_code} for {url}" + current_app.logger.error(message) + raise ValueError(message) def get_featured_tags(): id = get_user_id() @@ -60,8 +61,9 @@ def get_featured_tags(): tags = response.json() return tags else: - current_app.logger.error(f"get_featured_tags returned: {response.status_code}", url) - return [] + message=f"get_featured_tags returned: {response.status_code} for {url}" + current_app.logger.error(message) + raise ValueError(message) ### middleware @threads.before_request @@ -91,8 +93,9 @@ def fetch_statuses(ids): return statuses else: - current_app.logger.error(f"fetch_statuses returned: {response.status_code}", url) - return [] + message=f"fetch_statuses returned: {response.status_code} for {url}" + current_app.logger.error(message) + raise ValueError(message) def fetch_thread(id): url = server() + '/api/v1/statuses/' + id @@ -103,8 +106,9 @@ def fetch_thread(id): status['descendants'] = get_descendants(server(), status) return status else: - current_app.logger.error(f"fetch_thread returned: {response.status_code}", url) - return None + message=f"fetch_thread returned: {response.status_code} for {url}" + current_app.logger.error(message) + raise ValueError(message) def get_descendants(server, status): author_id = status['account']['id'] @@ -120,29 +124,34 @@ def get_descendants(server, status): descendants.append(utils.clean_status(reply)) return descendants else: - current_app.logger.error(f"get_descendants returned: {response.status_code}", url) - return [] + message=f"get_descendants returned: {response.status_code} for {url}" + current_app.logger.error(message) + raise ValueError(message) ### routes @threads.route('/') @cache.cached(timeout=300) def home(): - statuses = fetch_statuses(thread_ids) - statuses = [utils.clean_status(s) for s in statuses] - attribution = get_attribution() app = get_app_config() - tags = [] + attribution = get_attribution() + try: + statuses = fetch_statuses(thread_ids) + statuses = [utils.clean_status(s) for s in statuses] + tags = [] - # List featured hashtags - tags = get_featured_tags() + # List featured hashtags + tags = get_featured_tags() - # Remove any `None` entries from the status list - if statuses is None: - statuses = [] # fallback to an empty list - else: - statuses = [s for s in statuses if s] # keep only truthy statuses + # Remove any `None` entries from the status list + if statuses is None: + statuses = [] # fallback to an empty list + else: + statuses = [s for s in statuses if s] # keep only truthy statuses + + return render_template('_home.html', threads=statuses, tags=tags, app=app, attribution=attribution, render_date=datetime.now()) + except ValueError as message: + return render_template('_error.html', app=app, attribution=attribution, render_date=datetime.now(), message=message) - return render_template('_home.html', threads=statuses, tags=tags, app=app, attribution=attribution, render_date=datetime.now()) @threads.route('/tag/')