如何处理异步函数中的所有错误(Pyrogram 库)

How to handle all errors in async function (Pyrogram library)

提问人:Sayhag 提问时间:6/13/2023 更新时间:6/13/2023 访问量:97

问:

我有几千行代码,由几个普通函数和异步函数组成。我想将收到的所有错误保存在一个文件中,而无需尝试/排除或记录整个代码。 我通过搜索熟悉了这个优秀的函数,但问题是这个函数没有收到异步函数的错误,我不知道原因,但我猜是因为代码没有因为收到错误而停止,所以发生了这种情况,或者问题可能出在函数本身。是异步的。

我的def得到所有错误:

def myexcepthook(e_type, e_value, e_tb, org_excepthook=sys.excepthook):

    #open error file for append
    with open('errors.txt', 'a') as error_file:
        sys.stderr = error_file
        sys.stderr.write(f'\n--------------- Exception --------------\n')

        # get time
        now = datetime.now()

        # write error and time
        sys.stderr.write(f'TIME: {now}\n\n')
        org_excepthook(e_type, e_value, e_tb)

sys.excepthook = myexcepthook

我的主要代码(简化,因为它太长):

async def riseforgame(m: CallbackQuery, move=None):

    if move:
        await m.message.edit('you win!')
    else:
        return None

我收到的错误,它没有写在文件中:

Telegram says: [400 MESSAGE_NOT_MODIFIED] - The message was not modified because you tried to edit it using the same content (caused by "messages.EditMessage")
Traceback (most recent call last):
  File "C:\Users\HaM\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\pyrogram\dispatcher.py", line 240, in handler_worker
    await handler.callback(self.client, *args)
  File "c:\Users\HaM\Desktop\New folder\DLG.py", line 1296, in callback
    await riseforgame(m, num)
  File "c:\Users\HaM\Desktop\New folder\DLG.py", line 1029, in riseforgame
    awermsg = await m.message.edit('you win!', reply_markup=InlineKeyboardMarkup([
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\HaM\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\pyrogram\types\messages_and_media\message.py", line 2803, in edit_text
    return await self._client.edit_message_text(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\HaM\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\pyrogram\methods\messages\edit_message_text.py", line 82, in edit_message_text
    r = await self.invoke(
        ^^^^^^^^^^^^^^^^^^
  File "C:\Users\HaM\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\pyrogram\methods\advanced\invoke.py", line 79, in invoke
    r = await self.session.invoke(
        ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\HaM\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\pyrogram\session\session.py", line 389, in invoke
    return await self.send(query, timeout=timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\HaM\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\pyrogram\session\session.py", line 357, in send
    RPCError.raise_it(result, type(data))
  File "C:\Users\HaM\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\pyrogram\errors\rpc_error.py", line 91, in raise_it
    raise getattr(
pyrogram.errors.exceptions.bad_request_400.MessageNotModified: Telegram says: [400 MESSAGE_NOT_MODIFIED] - The message was not modified because you tried to edit it using the same content (caused by "messages.EditMessage")

谢谢你帮助我

python-3.x 错误处理 电报 pyrogram

评论


答: 暂无答案