Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Error handling if user does not exist #1158

Open
mi01 opened this issue Jun 4, 2024 · 0 comments
Open

[BUG] Error handling if user does not exist #1158

mi01 opened this issue Jun 4, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@mi01
Copy link

mi01 commented Jun 4, 2024

Describe the bug

I try to retrieve public user data for a list of TikTok accounts. This usually works fine, but there are some issues with this library, which should be fixed.

The buggy code

async with TikTokApi() as api:
    await api.create_sessions(
        ms_tokens=[ms_token],
        num_sessions=1,
        sleep_after=3,
        executable_path="/opt/google/chrome/google-chrome",
    )

    user = api.user(username=username)
    user_info = None
    try:
        user_info = await user.info()
        print(f"SUCCESS: {username}")
    except (KeyError, exceptions.InvalidResponseException, exceptions.EmptyResponseException, Exception) as e:
        print(f"ERROR: {e} :: https://www.tiktok.com/@{username} :: {user_info}")

Expected behavior

If the account does not exist, I expect to get a meaningful exception. But when running the code above for a non-existing account I get the following response from TikTok:

{"extra":{"fatal_item_ids":[],"logid":"...","now":1717514022000},"log_pb": "impr_id":"..."},"statusCode":10221,"status_code":0,"status_msg":"","userInfo":{}}

In my code example above this response triggers a KeyError instead of a meaningful exception. So I had a look into this library. make_request() in tiktok.py checks, if the result is None or an empty string or if we can decode the JSON or if status_code != 0.

result = await self.run_fetch_script(
signed_url, headers=headers, session_index=i
)
if result is None:
raise Exception("TikTokApi.run_fetch_script returned None")
if result == "":
raise EmptyResponseException(result, "TikTok returned an empty response")
try:
data = json.loads(result)
if data.get("status_code") != 0:
self.logger.error(f"Got an unexpected status code: {data}")
return data
except json.decoder.JSONDecodeError:
if retry_count == retries:
self.logger.error(f"Failed to decode json response: {result}")
raise InvalidJSONException()

None of this error conditions is true for this response. Then info() in user.py tries to extract data with __extract_from_data() not present in the response, so a KeyError is raised.

By the way, if the account does exist "statusCode": 0 is in the response.

But there is another issue, which makes it hard to distinguish between a non-existing account and this other case. When retrieving user data for several accounts, sometimes this error occurs:

2024-06-04 17:36:14,559 - TikTokApi.tiktok - ERROR - Got an unexpected status code: {'userInfo': {'user': {}, 'stats': {}, 'shareMeta': {}}}

And again in this case I get a KeyError instead of an meaningful exception, since none of the error conditions apply. For both cases user.info() return None by the way.

Thank you for this great library. But the error handling needs some updates, probably because TikTok slightly changed its API.

Desktop (please complete the following information):

  • OS: Linux 5.15.146.1-microsoft-standard-WSL2
  • Python 3.12.2
  • TikTokApi Version 6.3.0
@mi01 mi01 added the bug Something isn't working label Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant