From ef962d523717da3d9e5a7d21ce1fea248c70aba4 Mon Sep 17 00:00:00 2001 From: Jonah Feldman Date: Wed, 29 Jan 2020 15:12:02 -0500 Subject: [PATCH] Create UnexpectedHTTPError Exception Create an additional Exception class for handling all non-successful HTTP responses that are not covered by the previously created exception classes. Allows clients who face 500 series HTTP errors to better catch those exceptions instead of being forced to catch all exceptions in their try/except blocks --- OTXv2.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/OTXv2.py b/OTXv2.py index 68f1046..ca03492 100755 --- a/OTXv2.py +++ b/OTXv2.py @@ -81,6 +81,13 @@ def __init__(self, value=None): def __str__(self): return repr(self.value) + +class UnexpectedHTTPError(Exception): + def __init__(self, value=None): + self.value = value or "Unexpected HTTP error" + + def __str__(self): + return repr(self.value) class OTXv2(object): @@ -135,7 +142,7 @@ def _response_json(): elif response.status_code == 404: raise NotFound() elif str(response.status_code)[0] != "2": - raise Exception("Unexpected http code: %r, response=%r", response.status_code, _response_json()) + raise UnexpectedHTTPError("Unexpected http code: %r, response=%r", response.status_code, _response_json()) return response