Skip to content

Commit 09cbad5

Browse files
[v2.0.4] Extended exceptions for Cloudflare error codes (#33)
* bumped version * type hinting for exception mapping * retry for cloudflare error codes * circleci config update for pytest
1 parent c037ca1 commit 09cbad5

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
- run:
4040
name: Pytest Test Cases
4141
command: | # Run test suite, uses SCALE_TEST_API_KEY env variable
42-
pytest -v
42+
pytest -v -s
4343
- run:
4444
name: Twine PyPI Check
4545
command: | # Validate distribution and setup.py configuration

scaleapi/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
__version__ = "2.0.3"
1+
__version__ = "2.0.4"
22
__package_name__ = "scaleapi"

scaleapi/api.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,19 @@
1212
# Parameters for HTTP retry
1313
HTTP_TOTAL_RETRIES = 3 # Number of total retries
1414
HTTP_RETRY_BACKOFF_FACTOR = 2 # Wait 1, 2, 4 seconds between retries
15-
HTTP_STATUS_FORCE_LIST = [429, 500, 503, 504] # Status codes to force retry
15+
HTTP_STATUS_FORCE_LIST = [
16+
429,
17+
500,
18+
502,
19+
503,
20+
504,
21+
520,
22+
521,
23+
522,
24+
523,
25+
524,
26+
525,
27+
] # Status codes to force retry
1628
HTTP_RETRY_ALLOWED_METHODS = frozenset({"GET", "POST"})
1729

1830

@@ -73,11 +85,8 @@ def _raise_on_respose(res: Response):
7385
except ValueError:
7486
message = res.text
7587

76-
try:
77-
exception = ExceptionMap[res.status_code]
78-
raise exception(message)
79-
except KeyError as err:
80-
raise ScaleException(message, res.status_code) from err
88+
exception = ExceptionMap.get(res.status_code, ScaleException)
89+
raise exception(message, res.status_code)
8190

8291
def _api_request(
8392
self, method, endpoint, headers=None, auth=None, params=None, body=None

scaleapi/exceptions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from typing import Dict
2+
3+
14
class ScaleException(Exception):
25
"""Generic ScaleException class"""
36

@@ -81,7 +84,7 @@ class ScaleTimeoutError(ScaleException):
8184
code = 504
8285

8386

84-
ExceptionMap = {
87+
ExceptionMap: Dict[int, ScaleException] = {
8588
ScaleInvalidRequest.code: ScaleInvalidRequest,
8689
ScaleUnauthorized.code: ScaleUnauthorized,
8790
ScaleNotEnabled.code: ScaleNotEnabled,

0 commit comments

Comments
 (0)