Skip to content

Commit 832b190

Browse files
authored
Homogenize errors (#5)
* bug: fix catching of errors * fix: ensure return statement
1 parent 446738d commit 832b190

File tree

2 files changed

+18
-27
lines changed

2 files changed

+18
-27
lines changed

pymed_paperscraper/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from .api import PubMed
22

33
__all__ = ["PubMed"]
4-
__version__ = "1.0.2"
4+
__version__ = "1.0.3"

pymed_paperscraper/api.py

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
from typing import Union
88

99
import requests
10-
from requests.exceptions import ChunkedEncodingError
10+
from requests.exceptions import ChunkedEncodingError, ConnectionError, HTTPError
1111
from tqdm import tqdm
12+
from urllib3.exceptions import ProtocolError
1213

1314
from .article import PubMedArticle
1415
from .book import PubMedBookArticle
@@ -164,38 +165,28 @@ def _get(
164165
# Check for any errors
165166
response.raise_for_status()
166167
need_to_call = False
167-
except ChunkedEncodingError as e:
168+
except (
169+
ConnectionError,
170+
HTTPError,
171+
ChunkedEncodingError,
172+
OSError,
173+
ProtocolError,
174+
) as e:
175+
error_type = type(e).__name__
176+
tolog = "term" if "term" in parameters.keys() else "id"
168177
logger.info(
169-
f"ChunkedEncodingError: {e}.\tNow at {tries+1} attempts for {parameters['id']}: "
178+
f"{error_type}: {e}.\tNow at {tries+1} attempts for {parameters[tolog]}: "
170179
)
171180
tries += 1
172181
self._requestsMade.append(datetime.datetime.now())
173182
sleep(0.01)
174183
continue
175-
except requests.HTTPError as e:
176-
logger.info(
177-
f"HTTPError request error: {e}.\tNow at {tries+1} attempts for {parameters['id']}: "
178-
)
179-
tries += 1
180-
# Add this request to the list of requests made
181-
self._requestsMade.append(datetime.datetime.now())
182-
sleep(0.01)
183-
continue
184-
except ConnectionError as e:
185-
logger.info(
186-
f"ConnectionError: {e}.\tNow at {tries+1} attempts for {parameters['id']}: "
187-
)
188-
tries += 1
189-
# Add this request to the list of requests made
190-
self._requestsMade.append(datetime.datetime.now())
191-
sleep(0.01)
192-
continue
193184

194-
# Return the response
195-
if output == "json":
196-
return response.json()
197-
else:
198-
return response.text
185+
# Return the response
186+
if output == "json":
187+
return response.json()
188+
else:
189+
return response.text
199190

200191
def _getArticles(self: object, article_ids: list) -> list:
201192
"""Helper method that batches a list of article IDs and retrieves the content.

0 commit comments

Comments
 (0)