Нужно обработать ошибку pymongo.errors.DuplicateKeyError, не срабатывает обработчик вообще.
Вот код
async def add_user(user_id, user_name, name):
date = datetime.now().date()
collection.insert_one({
"_id" : user_id,
'name' : name,
"username" : user_name,
"date" : str(date)
})
@dp.message_handler(commands=['start'])
async def welcome_send_info(message: types.Message):
await message.reply('привет')
name = message.from_user.full_name
user_name = message.from_user.username
user_id = message.from_user.id
try:
await add_user(user_id, user_name, name)
except Exception as e:
print(e)
Future exception was never retrieved
future: <Future finished exception=DuplicateKeyError("E11000 duplicate key error collection: Dimalexus.BOT index: _id_ dup key: { _id: 1978560091 }, full error: {'index': 0, 'code': 11000, 'keyPattern': {'_id': 1}, 'keyValue': {'_id': 1978560091}, 'errmsg': 'E11000 duplicate key error collection: Dimalexus.BOT index: _id_ dup key: { _id: 1978560091 }'}")>
Traceback (most recent call last):
File "C:\Users\артем\AppData\Local\Programs\Python\Python310\lib\concurrent\futures\thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
File "C:\Users\артем\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\collection.py", line 606, in insert_one
self._insert_one(
File "C:\Users\артем\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\collection.py", line 547, in _insert_one
self.__database.client._retryable_write(acknowledged, _insert_command, session)
File "C:\Users\артем\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\mongo_client.py", line 1399, in _retryable_write
return self._retry_with_session(retryable, func, s, None)
File "C:\Users\артем\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\mongo_client.py", line 1286, in _retry_with_session
return self._retry_internal(retryable, func, session, bulk)
File "C:\Users\артем\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\mongo_client.py", line 1320, in _retry_internal
return func(session, sock_info, retryable)
File "C:\Users\артем\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\collection.py", line 545, in _insert_command
_check_write_command_response(result)
File "C:\Users\артем\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\helpers.py", line 216, in _check_write_command_response
_raise_last_write_error(write_errors)
File "C:\Users\артем\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\helpers.py", line 188, in _raise_last_write_error
raise DuplicateKeyError(error.get("errmsg"), 11000, error)
pymongo.errors.DuplicateKeyError: E11000 duplicate key error collection: Dimalexus.BOT index: _id_ dup key: { _id: 1978560091 }, full error: {'index': 0, 'code': 11000, 'keyPattern': {'_id': 1}, 'keyValue': {'_id': 1978560091}, 'errmsg': 'E11000 duplicate key error collection: Dimalexus.BOT index: _id_ dup key: { _id: 1978560091 }'}