Skip to content

Commit

Permalink
more errorhandling
Browse files Browse the repository at this point in the history
  • Loading branch information
T0jan committed Jun 28, 2023
1 parent 2960c56 commit 937dd35
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
8 changes: 5 additions & 3 deletions kintree/common/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def download(url, filetype='API data', fileoutput='', timeout=3, enable_headers=

import socket
import urllib.request
import requests

# Set default timeout for download socket
socket.setdefaulttimeout(timeout)
Expand All @@ -76,7 +77,6 @@ def download(url, filetype='API data', fileoutput='', timeout=3, enable_headers=
if filetype == 'Image' or filetype == 'PDF':
# Enable use of requests library for downloading files (some URLs do NOT work with urllib)
if requests_lib:
import requests
headers = {'User-agent': 'Mozilla/5.0'}
response = requests.get(url, headers=headers, timeout=timeout, allow_redirects=True)
if filetype.lower() not in response.headers['Content-Type'].lower():
Expand All @@ -95,12 +95,14 @@ def download(url, filetype='API data', fileoutput='', timeout=3, enable_headers=
data = url_data.read()
data_json = json.loads(data.decode('utf-8'))
return data_json
except socket.timeout:
except (socket.timeout, requests.exceptions.ConnectTimeout, requests.exceptions.ReadTimeout):
cprint(f'[INFO]\tWarning: {filetype} download socket timed out ({timeout}s)', silent=silent)
except urllib.error.HTTPError:
except (urllib.error.HTTPError, requests.exceptions.ConnectionError):
cprint(f'[INFO]\tWarning: {filetype} download failed (HTTP Error)', silent=silent)
except (urllib.error.URLError, ValueError):
cprint(f'[INFO]\tWarning: {filetype} download failed (URL Error)', silent=silent)
except requests.exceptions.SSLError:
cprint(f'[INFO]\tWarning: {filetype} download failed (SSL Error)', silent=silent)
except FileNotFoundError:
cprint(f'[INFO]\tWarning: {os.path.dirname(fileoutput)} folder does not exist', silent=silent)
return None
Expand Down
1 change: 1 addition & 0 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ def check_result(status: str, new_part: bool) -> bool:
password='admin',
enable_proxy=False,
proxies={},
datasheet_upload=True,
user_config_path=settings.INVENTREE_CONFIG,
):
method_success = False
Expand Down

0 comments on commit 937dd35

Please sign in to comment.