|
7 | 7 | from typing import Union |
8 | 8 |
|
9 | 9 | import requests |
10 | | -from requests.exceptions import ChunkedEncodingError |
| 10 | +from requests.exceptions import ChunkedEncodingError, ConnectionError, HTTPError |
11 | 11 | from tqdm import tqdm |
| 12 | +from urllib3.exceptions import ProtocolError |
12 | 13 |
|
13 | 14 | from .article import PubMedArticle |
14 | 15 | from .book import PubMedBookArticle |
@@ -164,38 +165,28 @@ def _get( |
164 | 165 | # Check for any errors |
165 | 166 | response.raise_for_status() |
166 | 167 | 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" |
168 | 177 | 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]}: " |
170 | 179 | ) |
171 | 180 | tries += 1 |
172 | 181 | self._requestsMade.append(datetime.datetime.now()) |
173 | 182 | sleep(0.01) |
174 | 183 | 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 |
193 | 184 |
|
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 |
199 | 190 |
|
200 | 191 | def _getArticles(self: object, article_ids: list) -> list: |
201 | 192 | """Helper method that batches a list of article IDs and retrieves the content. |
|
0 commit comments