Skip to content

Commit 9a71350

Browse files
authored
Deprecate Exception in favour of custom exception class (#57)
1 parent 7109a9c commit 9a71350

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

filestack/exceptions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class FilestackHTTPError(Exception):
2+
"""
3+
Custom HTTPError instead of requests.exceptions.HTTPError to add response body.
4+
5+
References:
6+
- https://github.com/psf/requests/pull/4234
7+
"""

filestack/utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import random
44
from functools import partial
55
import requests as original_requests
6+
from requests.exceptions import HTTPError
67

78
from filestack import config
9+
from filestack.exceptions import FilestackHTTPError
810

911

1012
def unique_id(length=10):
@@ -30,8 +32,10 @@ def handle_request(self, name, *args, **kwargs):
3032
requests_method = getattr(original_requests, name)
3133
response = requests_method(*args, **kwargs)
3234

33-
if not response.ok:
34-
raise Exception(response.text)
35+
try:
36+
response.raise_for_status()
37+
except HTTPError as e:
38+
raise FilestackHTTPError(response.text) from e
3539

3640
return response
3741

tests/helpers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from requests.exceptions import HTTPError
12

23

34
class DummyHttpResponse:
@@ -11,3 +12,12 @@ def __init__(self, ok=True, status_code=200, json_dict=None, content=b'', header
1112

1213
def json(self):
1314
return self.json_dict
15+
16+
def raise_for_status(self):
17+
"""
18+
Return a dummy error if 'ok' was specified as False.
19+
20+
Note that in the original 'requests', response.ok calls raise_for_status() instead
21+
"""
22+
if not self.ok:
23+
raise HTTPError("HTTP request failed.")

0 commit comments

Comments
 (0)