From f4b65be8763226bc03546673ac75a2ceaa49f758 Mon Sep 17 00:00:00 2001 From: Ben Busby Date: Tue, 28 Dec 2021 11:38:18 -0700 Subject: [PATCH] Catch invalid XML in suggestion response As reported in #593, the XML response body returned for search suggestions can apparently contain invalid XML elements. This catches the error and returns an empty suggestion list instead of erroring. Fixes #593 --- app/request.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/request.py b/app/request.py index d7157af..17ac034 100644 --- a/app/request.py +++ b/app/request.py @@ -229,9 +229,13 @@ class Request: if not response: return [] - root = ET.fromstring(response) - return [_.attrib['data'] for _ in - root.findall('.//suggestion/[@data]')] + try: + root = ET.fromstring(response) + return [_.attrib['data'] for _ in + root.findall('.//suggestion/[@data]')] + except ET.ParseError: + # Malformed XML response + return [] def send(self, base_url='', query='', attempt=0, force_mobile=False) -> Response: