From c4a694422d1d125c29f955eafe61a5986d9db704 Mon Sep 17 00:00:00 2001 From: Angus L'Herrou Date: Tue, 18 Apr 2023 15:59:30 -0500 Subject: [PATCH 1/2] raise RedcapError if status code is 500 --- redcap/request.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/redcap/request.py b/redcap/request.py index 50ff46d..e4ba702 100644 --- a/redcap/request.py +++ b/redcap/request.py @@ -186,6 +186,9 @@ def execute( self.url, data=self.payload, verify=verify_ssl, files=file, **kwargs ) + if response.status_code == 500: + raise RedcapError(f"HTTP error 500 {response.reason}") + content = self.get_content( response, format_type=self.fmt, From ab45bd9dca83989f7fc4cf5732773027bbb5e76c Mon Sep 17 00:00:00 2001 From: Angus L'Herrou Date: Tue, 18 Apr 2023 15:59:58 -0500 Subject: [PATCH 2/2] raise RedcapError for JSONDecodeError in get_content --- redcap/request.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/redcap/request.py b/redcap/request.py index e4ba702..2460536 100644 --- a/redcap/request.py +++ b/redcap/request.py @@ -16,7 +16,7 @@ overload, ) -from requests import RequestException, Response, Session +from requests import RequestException, Response, Session, JSONDecodeError if TYPE_CHECKING: from io import TextIOWrapper @@ -150,7 +150,10 @@ def get_content( return [{}] if format_type == "json": - return response.json() + try: + return response.json() + except JSONDecodeError as jde: + raise RedcapError("Unable to decode response as JSON") from jde # don't do anything to csv/xml strings return response.text