Skip to content

requests.exceptions.JSONDecodeError at Telegraph.create_page #56

@SyberiaK

Description

@SyberiaK

I have a Telegram bot that creates Telegraph pages with some user stats.
Sometimes it raises requests.exceptions.JSONDecodeError at Telegraph.create_page:

File "/root/botty/main.py", line 474, in user_stats
    telegraph_response = telegraph.create_page(stats_page_title,
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/telegraph/api.py", line 183, in create_page
    return self._telegraph.method('createPage', values={
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/telegraph/api.py", line 35, in method
    ).json()
      ^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/requests/models.py", line 975, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

What's strange is that if you try to call Telegraph.create_page again - it runs flawlessly.
So I ended up with "fuckit" strategy:

try:
    telegraph_response = telegraph.create_page(stats_page_title,
                                               html_content=stats_page_text,
                                               author_name=bot_name,
                                               author_url=bot_link)
except requests.exceptions.JSONDecodeError:  # fuck it
    telegraph_response = telegraph.create_page(stats_page_title,
                                               html_content=stats_page_text,
                                               author_name=bot_name,
                                               author_url=bot_link)

I suspect this could be fixed if we could pass in timeout argument to inner post request.

def create_page(self, title, content=None, html_content=None,
                author_name=None, author_url=None, return_content=False, timeout=0):
    ...

    return self._telegraph.method('createPage', values={
       'title': title,
       'author_name': author_name,
       'author_url': author_url,
       'content': content_json,
       'return_content': return_content
    }, timeout=timeout)
def method(self, method, values=None, path='', timeout=0):
    ...

    response = self.session.post(
        'https://api.{}/{}/{}'.format(self.domain, method, path),
        data=values, timeout=timeout
    ).json()

   ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions