I’m working with the Twitch API using some Python libraries like twitch-python and python-twitch-client for data collection purposes.
The main issue I’m facing is that I can’t properly catch HTTP errors. I’m not using urllib.request directly, so I’m confused about the right way to handle these exceptions.
Here’s my current code:
categories = []
for item in video_list[50:]:
video_data = api_client.videos.get_by_id(item).game
try:
categories.append(video_data)
except Exception as error:
print(type(error))
Even with the try-except block, I still get this error: HTTPError: 404 Client Error: Not Found for url
Why isn’t my exception handling working? What’s the proper way to catch these HTTP errors when using Twitch API libraries?
That generic Exception catch is probably hiding the actual error you need to handle. Most Python HTTP libraries throw specific exceptions for different status codes. Catch HTTPError directly instead - you’ll need to import it from the requests library since most Twitch API wrappers use that underneath.
from requests.exceptions import HTTPError
categories = []
for item in video_list[50:]:
try:
video_data = api_client.videos.get_by_id(item).game
categories.append(video_data)
except HTTPError as e:
if e.response.status_code == 404:
print(f"Video {item} not found, skipping")
else:
print(f"HTTP error {e.response.status_code} for video {item}")
This way you can handle different HTTP status codes properly. I’ve worked with APIs before and 404s happen all the time with video content that gets deleted or goes private.
You’re manually handling flaky API calls when you could automate the whole thing. I’ve built similar data collection systems - the manual approach always creates headaches.
Yeah, you can catch HTTPError exceptions like others said, but what about rate limits? API maintenance downtime? You’ll end up writing tons of retry logic and error handling.
I switched to Latenode for API workflows like this. It handles Twitch API calls with built-in error handling, automatic retries, and fallback logic. No more try-except blocks everywhere.
The workflow: trigger for each video ID → HTTP request to Twitch → conditional logic for status codes → automatic storage. Video returns 404? Skips to the next one. Hit rate limits? Waits and retries.
You also get logging and monitoring built-in, so you can see exactly which videos fail and why. Way cleaner than console error prints.
You’ve got the try-except block in the wrong spot. The HTTP error happens during the API call api_client.videos.get_by_id(item).game, not when you’re appending to the list. You need to wrap the actual API request where the exception’s getting thrown.
Move your exception handling like this:
categories = []
for item in video_list[50:]:
try:
video_data = api_client.videos.get_by_id(item).game
categories.append(video_data)
except Exception as error:
print(f"Error processing video {item}: {type(error)}")
continue
That 404 error means some videos in your list probably don’t exist anymore or have restricted access. This way you’ll skip the problematic ones and keep processing the rest without crashing the whole loop.