Skip to content

Commit 04b9f20

Browse files
authored
Merge pull request #12 from ScrapingAnt/feature/issue11-handle-404-status-code
feature/issue11-handle-404-status-code: implemented
2 parents 5501db4 + 7f2c0a2 commit 04b9f20

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Class defining response from API.
8585
| ScrapingantInvalidTokenException | The API token is wrong or you have exceeded the API calls request limit
8686
| ScrapingantInvalidInputException | Invalid value provided. Please, look into error message for more info |
8787
| ScrapingantInternalException | Something went wrong with the server side code. Try again later or contact ScrapingAnt support |
88+
| ScrapingantSiteNotReachableException | The requested URL is not reachable. Please, check it locally |
8889

8990
* * *
9091

scrapingant_client/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.3.3"
1+
__version__ = "0.3.4"
22

33
from scrapingant_client.client import ScrapingAntClient
44
from scrapingant_client.cookie import Cookie
@@ -7,6 +7,7 @@
77
ScrapingantInvalidTokenException,
88
ScrapingantInvalidInputException,
99
ScrapingantInternalException,
10+
ScrapingantSiteNotReachableException,
1011
)
1112
from scrapingant_client.response import Response
1213

@@ -17,5 +18,6 @@
1718
'ScrapingantInvalidTokenException',
1819
'ScrapingantInvalidInputException',
1920
'ScrapingantInternalException',
21+
'ScrapingantSiteNotReachableException',
2022
'Response',
2123
]

scrapingant_client/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
ScrapingantInvalidTokenException,
1212
ScrapingantInvalidInputException,
1313
ScrapingantInternalException,
14+
ScrapingantSiteNotReachableException,
1415
)
1516
from scrapingant_client.response import Response
1617
from scrapingant_client.utils import base64_encode_string
@@ -54,6 +55,8 @@ def general_request(
5455
raise ScrapingantInvalidTokenException()
5556
elif response.status_code == 422:
5657
raise ScrapingantInvalidInputException(response.text)
58+
elif response.status_code == 404:
59+
raise ScrapingantSiteNotReachableException(url)
5760
elif response.status_code == 500:
5861
raise ScrapingantInternalException()
5962
json_response = response.json()

scrapingant_client/errors.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ class ScrapingantInvalidInputException(ScrapingantClientException):
1313
pass
1414

1515

16+
class ScrapingantSiteNotReachableException(ScrapingantClientException):
17+
def __init__(self, url):
18+
message = f'The requested URL is not reachable ({url})'
19+
super().__init__(message)
20+
21+
1622
class ScrapingantInternalException(ScrapingantClientException):
1723
def __init__(self):
1824
message = 'Something went wrong with the server side. Please try later or contact support'

tests/test_exceptions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
ScrapingantInvalidTokenException,
77
ScrapingantInvalidInputException,
88
ScrapingantInternalException,
9+
ScrapingantSiteNotReachableException,
910
)
1011
from scrapingant_client.constants import SCRAPINGANT_API_BASE_URL
1112

@@ -36,3 +37,13 @@ def test_internal_server_error():
3637
client = ScrapingAntClient(token='some_token')
3738
with pytest.raises(ScrapingantInternalException):
3839
client.general_request('bad_url')
40+
41+
42+
@responses.activate
43+
def test_not_reachable():
44+
responses.add(responses.POST, SCRAPINGANT_API_BASE_URL + '/general',
45+
json={}, status=404)
46+
client = ScrapingAntClient(token='some_token')
47+
with pytest.raises(ScrapingantSiteNotReachableException) as e:
48+
client.general_request('example.com')
49+
assert 'The requested URL is not reachable (example.com)' in str(e)

0 commit comments

Comments
 (0)