From 8b563c58f75780f99468fa83ef3d33661e4d4c9d Mon Sep 17 00:00:00 2001 From: Denis Laxalde Date: Fri, 6 Oct 2023 14:02:24 +0200 Subject: [PATCH] Avoid using requests's JSONDecodeError This exception is only present in "recent" version of requests, typically not in the version distributed by Debian bullseye. Since requests' JSONDecodeError is in general a subclass of json.JSONDecodeError, we use the latter, but also handle the plain ValueError (which json.JSONDecodeError is a subclass of) because requests might use simplejson (which uses its own JSONDecodeError, also a subclass of ValueError). --- CHANGELOG.md | 3 +++ check_patroni/types.py | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b4d8f55..77712b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ ### Fixed +* Add compatibility with [requests](https://requests.readthedocs.io) + version 2.25 and higher. + ### Misc * Improve test coverage by running an HTTP server to fake the Patroni API (#55 diff --git a/check_patroni/types.py b/check_patroni/types.py index 096f31b..e3c143d 100644 --- a/check_patroni/types.py +++ b/check_patroni/types.py @@ -1,4 +1,5 @@ from typing import Any, Callable, List, Optional, Tuple, Union +import json from urllib.parse import urlparse import attr @@ -71,7 +72,7 @@ def rest_api(self, service: str) -> Any: try: return r.json() - except requests.exceptions.JSONDecodeError: + except (json.JSONDecodeError, ValueError): return None raise nagiosplugin.CheckError("Connection failed for all provided endpoints")