Redis connection returning UnicodeError

258 Views Asked by At

I am trying to connect to Heroku Redis, with Python 3.8, to save on Redis dictionaries and strings. I am trying to use Herokus Redis as persistence for a Telegram bot (libraries: python-telegram-bot and Telethon) At soon as the Redis instance is called on SET(), I receive the following error:

2020-10-06 16:15:26,335 - telegram.utils.promise - ERROR - An uncaught error was raised while running the promise
app[web.1]: Traceback (most recent call last):
app[web.1]: File "/app/.heroku/python/lib/python3.8/encodings/idna.py", line 165, in encode
app[web.1]: raise UnicodeError("label empty or too long")
app[web.1]: UnicodeError: label empty or too long
app[web.1]: 
app[web.1]: The above exception was the direct cause of the following exception:
app[web.1]:
app[web.1]: Traceback (most recent call last):
app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/telegram/utils/promise.py", line 56, in run
app[web.1]: self._result = self.pooled_function(*self.args, **self.kwargs)
app[web.1]: File "/app/karim/bot/commands/account.py", line 7, in check_account
app[web.1]: manager= SessionManager(Persistence.ACCOUNT, chat_id=update.effective_chat.id, user_id=update.effective_chat.id, message_id=update.message.message_id)
app[web.1]: File "/app/karim/classes/persistence.py", line 14, in wrapper
app[web.1]: self.serialize()
app[web.1]: File "/app/karim/classes/persistence.py", line 64, in serialize
app[web.1]: redis_connector.hmset('persistence:{}{}{}'.format(self.method, self.user_id, self.chat_id), obj_dict)
app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/redis/client.py", line 3075, in hmset
app[web.1]: return self.execute_command('HMSET', name, *items)
app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/redis/client.py", line 898, in execute_command
app[web.1]: conn = self.connection or pool.get_connection(command_name, **options)
app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/redis/connection.py", line 1192, in get_connection
app[web.1]: connection.connect()
app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/redis/connection.py", line 559, in connect
app[web.1]: sock = self._connect()
app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/redis/connection.py", line 584, in _connect
app[web.1]: for res in socket.getaddrinfo(self.host, self.port, self.socket_type,
app[web.1]: File "/app/.heroku/python/lib/python3.8/socket.py", line 918, in getaddrinfo
app[web.1]: for res in _socket.getaddrinfo(host, port, family, type, proto, flags):

I am wondering whether the error is caused when connecting to Redis (I initiated the connection one time only, and Heroku might be changing the REDIS_URL) or when trying to pass in the dictionary...

This is the redis_connector initializer:

redis_connector = redis.Redis(host=secrets.get_var('REDIS_URL'), port=14909, db=0, decode_responses=False)

and this is the code line that is causing the error:

redis_connector.hmset('persistence:{}{}{}'.format(self.method, self.user_id, self.chat_id), obj_dict)

This is the dictionary I am trying to pass:

{'method': 'account', 'chat_id': 1234, 'user_id': 1234, 'message_id': 55, 'phone': -1, 'password': -1, 'code': -1, 'phone_code_hash': -1, 'code_tries': 0}

For full code reference: https://github.com/davidwickerhf/karim (in particular https://github.com/davidwickerhf/karim/blob/main/karim/classes/persistence.py , https://github.com/davidwickerhf/karim/blob/main/karim/classes/session_manager.py and https://github.com/davidwickerhf/karim/blob/main/karim/__init__.py - files were redis connection is called)

Thank you for your help in advance

0

There are 0 best solutions below