You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
try:
# your code here
response = requests.request("POST", url, headers=headers, data=payload)
response.raise_for_status()
except KeyError as e:
print(f"[ERROR] KeyError: Missing key {e}")
except requests.exceptions.RequestException as e:
print(f"[ERROR] Request Failed: {e}")
except Exception as e:
print(f"[ERROR] Unexpected Error: {str(e)}")
Check if variable exists and raise ValueError
api_key = os.getenv('CANTONESE_AI_API_KEY')
content_json_file = os.getenv('CONTENT_JSON_FILE')
voice_id = os.getenv('VOICE_ID')
if not api_key:
raise ValueError("CANTONESE_AI_API_KEY is missing in .env")
if not content_json_file:
raise ValueError("CONTENT_JSON_FILE is missing in .env")
if not voice_id:
raise ValueError("VOICE_ID is missing in .env")
audio_base64 = response_data.get('file')
if audio_base64 is None:
raise ValueError("API Response missing 'file' key")
Loading JSON + safe .get() usage
with open('content.json', 'r', encoding='utf-8') as f:
content = json.load(f)
for item in content:
item_id = item.get('id')
category = item.get('category')
text = item.get('text')
if item_id is None or category is None or text is None:
print(f"[ERROR] Missing required field in item: {item}")
continue