From 6ee8db1df261ff008f0531c8d20dcde4fed0b10f 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 ebcb4c0..9370ab3 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..3032547 100644 --- a/check_patroni/types.py +++ b/check_patroni/types.py @@ -1,3 +1,4 @@ +import json from typing import Any, Callable, List, Optional, Tuple, Union from urllib.parse import urlparse @@ -71,7 +72,7 @@ class PatroniResource(nagiosplugin.Resource): try: return r.json() - except requests.exceptions.JSONDecodeError: + except (json.JSONDecodeError, ValueError): return None raise nagiosplugin.CheckError("Connection failed for all provided endpoints")