More exception handling for json decoding

merge-requests/30/head
Bob Mottram 2024-01-19 21:43:48 +00:00
parent d337f663d9
commit fbc597256f
3 changed files with 41 additions and 6 deletions

View File

@ -21462,7 +21462,16 @@ class PubServer(BaseHTTPRequestHandler):
except OSError:
print('EX: unable to read file ' + filename)
if content:
content_json = json.loads(content)
try:
content_json = json.loads(content)
except json.decoder.JSONDecodeError as ex:
self._400()
print('EX: json decode error ' + str(ex) +
' from GET content_json ' +
str(content))
self.server.getreq_busy = False
return
msg_str = json.dumps(content_json, ensure_ascii=False)
msg_str = self._convert_domains(calling_domain,
referer_domain,

View File

@ -4608,11 +4608,25 @@ def _inbox_after_initial(server, inbox_start_time,
if onion_domain:
if onion_domain in message_str:
message_str = message_str.replace(onion_domain, domain)
message_json = json.loads(message_str)
try:
message_json = json.loads(message_str)
except json.decoder.JSONDecodeError as ex:
print('EX: json decode error ' + str(ex) +
' from _inbox_after_initial onion ' +
str(message_str))
inbox_start_time = time.time()
return False
if i2p_domain:
if i2p_domain in message_str:
message_str = message_str.replace(i2p_domain, domain)
message_json = json.loads(message_str)
try:
message_json = json.loads(message_str)
except json.decoder.JSONDecodeError as ex:
print('EX: json decode error ' + str(ex) +
' from _inbox_after_initial i2p ' +
str(message_str))
inbox_start_time = time.time()
return False
actor = key_id
if '#' in actor:

View File

@ -168,7 +168,13 @@ def libretranslate_languages(url: str, api_key: str = None) -> []:
with request.urlopen(req) as response:
response_str = response.read().decode()
result = json.loads(response_str)
try:
result = json.loads(response_str)
except json.decoder.JSONDecodeError as ex:
print('EX: json decode error ' + str(ex) +
' from libretranslate_languages ' +
str(response_str))
return []
if not result:
return []
if not isinstance(result, list):
@ -283,8 +289,14 @@ def libretranslate(url: str, text: str,
if not response_str:
return original_text
translated_text = \
'<p>' + json.loads(response_str)['translatedText'] + '</p>'
try:
translated_text = \
'<p>' + json.loads(response_str)['translatedText'] + '</p>'
except json.decoder.JSONDecodeError as ex:
print('EX: json decode error ' + str(ex) +
' from libretranslate ' +
str(response_str))
return original_text
# append links form the original text
if links: