From 7f2c0a24b2930f9d9d7ada652b367dbe1288bc45 Mon Sep 17 00:00:00 2001 From: "andrii.kovalenko" Date: Wed, 21 Apr 2021 19:36:14 +0300 Subject: [PATCH] feature/issue11-handle-404-status-code: implemented --- README.md | 1 + scrapingant_client/__init__.py | 4 +++- scrapingant_client/client.py | 3 +++ scrapingant_client/errors.py | 6 ++++++ tests/test_exceptions.py | 11 +++++++++++ 5 files changed, 24 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d1af5c9..3066eb3 100644 --- a/README.md +++ b/README.md @@ -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 | * * * diff --git a/scrapingant_client/__init__.py b/scrapingant_client/__init__.py index e531cd4..345b0f9 100644 --- a/scrapingant_client/__init__.py +++ b/scrapingant_client/__init__.py @@ -1,4 +1,4 @@ -__version__ = "0.3.3" +__version__ = "0.3.4" from scrapingant_client.client import ScrapingAntClient from scrapingant_client.cookie import Cookie @@ -7,6 +7,7 @@ ScrapingantInvalidTokenException, ScrapingantInvalidInputException, ScrapingantInternalException, + ScrapingantSiteNotReachableException, ) from scrapingant_client.response import Response @@ -17,5 +18,6 @@ 'ScrapingantInvalidTokenException', 'ScrapingantInvalidInputException', 'ScrapingantInternalException', + 'ScrapingantSiteNotReachableException', 'Response', ] diff --git a/scrapingant_client/client.py b/scrapingant_client/client.py index dacbb7f..2e4f0c8 100644 --- a/scrapingant_client/client.py +++ b/scrapingant_client/client.py @@ -11,6 +11,7 @@ ScrapingantInvalidTokenException, ScrapingantInvalidInputException, ScrapingantInternalException, + ScrapingantSiteNotReachableException, ) from scrapingant_client.response import Response from scrapingant_client.utils import base64_encode_string @@ -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() diff --git a/scrapingant_client/errors.py b/scrapingant_client/errors.py index 1180261..0d70971 100644 --- a/scrapingant_client/errors.py +++ b/scrapingant_client/errors.py @@ -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' diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index cc59d57..dbc6887 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -6,6 +6,7 @@ ScrapingantInvalidTokenException, ScrapingantInvalidInputException, ScrapingantInternalException, + ScrapingantSiteNotReachableException, ) from scrapingant_client.constants import SCRAPINGANT_API_BASE_URL @@ -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)