add error handling for wrong credentials

Unfortunately, the taz website does not respond with an http error code if the credentials are wrong. So we have to check if the response is a pdf file or the html page with an error message.
This commit is contained in:
Marc Koch 2021-09-12 15:23:01 +02:00
parent 70e5d574c5
commit e1380d81c2
1 changed files with 19 additions and 5 deletions

View File

@ -1,11 +1,11 @@
import os import os
import requests import requests
from requests.exceptions import HTTPError from requests.exceptions import HTTPError
from exceptions import TazDownloadFormatException from exceptions import TazDownloadFormatException, TazConfigurationError, TazDownloadError
from exceptions import TazDownloadError
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from envyaml import EnvYAML from envyaml import EnvYAML
import argparse import argparse
import filetype
dir_path = os.path.dirname(os.path.realpath(__file__)) dir_path = os.path.dirname(os.path.realpath(__file__))
@ -170,8 +170,22 @@ class TazDownloader:
with open(os.path.join(download_folder, taz), "wb") as f: with open(os.path.join(download_folder, taz), "wb") as f:
for chunk in r.iter_content(chunk_size=8192): for chunk in r.iter_content(chunk_size=8192):
f.write(chunk) f.write(chunk)
# Unfortunately, the taz website does not respond with an http error code if the credentials are wrong.
# So we have to check if the response is a pdf file or the html page with an error message.
try:
if filetype.guess(os.path.join(download_folder, taz)).mime != 'application/pdf':
raise TazDownloadError()
except (AttributeError, TazDownloadError) as e:
# Try to get the error message from the html file to put it in the log
with open(os.path.join(download_folder, taz), 'r') as f:
soup = BeautifulSoup(f.read(), 'html.parser')
error_displayed_on_page = soup.find('p', class_='error').text
if error_displayed_on_page:
os.remove(os.path.join(download_folder, taz))
raise TazDownloadError(error_displayed_on_page)
else:
os.remove(os.path.join(download_folder, taz))
raise TazDownloadError(e)
return True return True
except HTTPError as http_e: except HTTPError as http_e:
raise TazDownloadError(f"Could not download taz:\n{http_e}") raise TazDownloadError(http_e)
except Exception as e:
raise TazDownloadError(f"Something went wrong:\n{e}")