Skip to content

fix: add error handling for TikTok parse failures#726

Open
Arvuno wants to merge 1 commit into
Evil0ctal:mainfrom
Arvuno:contrib/douyin/tiktok-parse-error
Open

fix: add error handling for TikTok parse failures#726
Arvuno wants to merge 1 commit into
Evil0ctal:mainfrom
Arvuno:contrib/douyin/tiktok-parse-error

Conversation

@Arvuno
Copy link
Copy Markdown

@Arvuno Arvuno commented May 21, 2026

Summary

  • Added input validation for TikTok video ID (itemId) to check for empty values and valid numeric format (10-21 digits)
  • Added response structure validation to handle unexpected API response types
  • Added graceful handling for TikTok API error responses (missing item_info)

Problem

  • Invalid TikTok video IDs could cause crashes with unhelpful error messages
  • Empty or malformed itemId parameters were not validated before being passed to the crawler
  • TikTok API returning success status with missing video data caused unclear failures

Solution

  • Added pre-validation of itemId format with regex: 10-21 numeric digits
  • Added response type checking (dict expected) with descriptive ValueError
  • Added handling for TikTok API returning status_code=0 but missing item_info
  • Error messages now clearly indicate whether the issue is:
    • 400: Invalid input format
    • 404: Video not found/unavailable
    • General parse failures

Tests

# Test invalid video ID format
curl "http://localhost:8000/tiktok/web/fetch_one_video?itemId=invalid"
# Expected: 400 with "Invalid video ID format" message

# Test empty video ID
curl "http://localhost:8000/tiktok/web/fetch_one_video?itemId="
# Expected: 400 with "itemId cannot be empty" message

# Test valid video ID (example from docs)
curl "http://localhost:8000/tiktok/web/fetch_one_video?itemId=7339393672959757570"
# Expected: 200 with video data (or 404 if video unavailable)

Risk / Compatibility

  • Risk: LOW
  • Affects: TikTok web API endpoint only (app API unchanged)
  • Backward compatible: Valid requests continue to work as before
  • New behavior: Invalid inputs now receive proper error responses instead of generic 400

- Add input validation for itemId (must be numeric, 10-21 digits)
- Add response structure validation (expect dict, check for item_info)
- Add graceful error handling with specific HTTP status codes:
  - 400 for invalid input format
  - 404 for video not found/unavailable
  - 400 for general parse failures
- Move import re to module top level to follow Python best practices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant