Skip to content

Commit

Permalink
Merge pull request #12 from ScrapingAnt/feature/issue11-handle-404-st…
Browse files Browse the repository at this point in the history
…atus-code

feature/issue11-handle-404-status-code: implemented
  • Loading branch information
megabotan authored Apr 21, 2021
2 parents 5501db4 + 7f2c0a2 commit 04b9f20
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Class defining response from API.
| ScrapingantInvalidTokenException | The API token is wrong or you have exceeded the API calls request limit
| ScrapingantInvalidInputException | Invalid value provided. Please, look into error message for more info |
| ScrapingantInternalException | Something went wrong with the server side code. Try again later or contact ScrapingAnt support |
| ScrapingantSiteNotReachableException | The requested URL is not reachable. Please, check it locally |

* * *

Expand Down
4 changes: 3 additions & 1 deletion scrapingant_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.3.3"
__version__ = "0.3.4"

from scrapingant_client.client import ScrapingAntClient
from scrapingant_client.cookie import Cookie
Expand All @@ -7,6 +7,7 @@
ScrapingantInvalidTokenException,
ScrapingantInvalidInputException,
ScrapingantInternalException,
ScrapingantSiteNotReachableException,
)
from scrapingant_client.response import Response

Expand All @@ -17,5 +18,6 @@
'ScrapingantInvalidTokenException',
'ScrapingantInvalidInputException',
'ScrapingantInternalException',
'ScrapingantSiteNotReachableException',
'Response',
]
3 changes: 3 additions & 0 deletions scrapingant_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
ScrapingantInvalidTokenException,
ScrapingantInvalidInputException,
ScrapingantInternalException,
ScrapingantSiteNotReachableException,
)
from scrapingant_client.response import Response
from scrapingant_client.utils import base64_encode_string
Expand Down Expand Up @@ -54,6 +55,8 @@ def general_request(
raise ScrapingantInvalidTokenException()
elif response.status_code == 422:
raise ScrapingantInvalidInputException(response.text)
elif response.status_code == 404:
raise ScrapingantSiteNotReachableException(url)
elif response.status_code == 500:
raise ScrapingantInternalException()
json_response = response.json()
Expand Down
6 changes: 6 additions & 0 deletions scrapingant_client/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ class ScrapingantInvalidInputException(ScrapingantClientException):
pass


class ScrapingantSiteNotReachableException(ScrapingantClientException):
def __init__(self, url):
message = f'The requested URL is not reachable ({url})'
super().__init__(message)


class ScrapingantInternalException(ScrapingantClientException):
def __init__(self):
message = 'Something went wrong with the server side. Please try later or contact support'
Expand Down
11 changes: 11 additions & 0 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
ScrapingantInvalidTokenException,
ScrapingantInvalidInputException,
ScrapingantInternalException,
ScrapingantSiteNotReachableException,
)
from scrapingant_client.constants import SCRAPINGANT_API_BASE_URL

Expand Down Expand Up @@ -36,3 +37,13 @@ def test_internal_server_error():
client = ScrapingAntClient(token='some_token')
with pytest.raises(ScrapingantInternalException):
client.general_request('bad_url')


@responses.activate
def test_not_reachable():
responses.add(responses.POST, SCRAPINGANT_API_BASE_URL + '/general',
json={}, status=404)
client = ScrapingAntClient(token='some_token')
with pytest.raises(ScrapingantSiteNotReachableException) as e:
client.general_request('example.com')
assert 'The requested URL is not reachable (example.com)' in str(e)

0 comments on commit 04b9f20

Please sign in to comment.