wrap some things in try/except blocks

This commit is contained in:
revsuine 2024-11-15 14:36:54 +00:00
parent 272842162d
commit 4670bcdafd
Signed by: revsuine
GPG key ID: 3F257B68F5BC9339

View file

@ -72,7 +72,12 @@ def decode_email(message: email.message.Message) -> email.message.Message:
# this is a kinda hacky way to do this by manipulating the message as a
# string but i couldn't get it to work any other way
decoded_bytes = message.as_bytes()
# sometimes this raises an exception and idk why
try:
decoded_bytes = message.as_bytes()
except UnicodeEncodeError:
decoded_bytes = message.as_string().encode()
# if email doesn't need decoding
has_quopri = b'Content-Transfer-Encoding: quoted-printable' in decoded_bytes
has_base64 = b'Content-Transfer-Encoding: base64' in decoded_bytes
@ -189,7 +194,12 @@ def encrypt(
return message.as_string()
# make necessary changes to message
message = decode_email(message)
# this function is quite clunky and seems to throw exceptions from time to
# time; if we can't make the necessary changes we want to just continue
try:
message = decode_email(message)
except:
pass
gpg = gnupg.GPG()
gpg.encoding = encoding