Skip to content

Commit

Permalink
Merge pull request #176 from jpgill86/log-tracebacks
Browse files Browse the repository at this point in the history
Log fatal errors in standalone app instead of crashing
  • Loading branch information
jpgill86 committed Jan 19, 2020
2 parents 127aa75 + fcd6089 commit a692c58
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 24 deletions.
14 changes: 7 additions & 7 deletions neurotic/datasets/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,28 +80,28 @@ def download(url, local_file, overwrite_existing=False, show_progress=True, byte

if error_code == 404:
# not found
logger.critical(f'Skipping {os.path.basename(local_file)} (not found on server)')
logger.error(f'Skipping {os.path.basename(local_file)} (not found on server)')
return

elif error_code == 550:
# no such file or folder, or permission denied
logger.critical(f'Skipping {os.path.basename(local_file)} (not found on server, or user is unauthorized)')
logger.error(f'Skipping {os.path.basename(local_file)} (not found on server, or user is unauthorized)')
return

elif error_code == 10060:
# timeout
hostname = urllib.parse.urlparse(url).hostname
logger.critical(f'Skipping {os.path.basename(local_file)} (timed out when connecting to {hostname})')
logger.error(f'Skipping {os.path.basename(local_file)} (timed out when connecting to {hostname})')
return

elif error_code == 11001:
# could not reach server or resolve hostname
hostname = urllib.parse.urlparse(url).hostname
logger.critical(f'Skipping {os.path.basename(local_file)} (cannot connect to {hostname})')
logger.error(f'Skipping {os.path.basename(local_file)} (cannot connect to {hostname})')
return

else:
logger.critical(f'Encountered a problem: {error}')
logger.error(f'Encountered a problem: {error}')
return


Expand Down Expand Up @@ -253,7 +253,7 @@ def _authenticate(url):
raise error

if bad_login_attempts >= _max_bad_login_attempts:
logger.critical('Unauthorized: Aborting login')
logger.error('Unauthorized: Aborting login')
return False
else:
if bad_login_attempts == 0:
Expand All @@ -269,7 +269,7 @@ def _authenticate(url):
host, port = urllib.parse.splitport(netloc)
user = input(f'User name on {host}: ')
if not user:
logger.critical('No user given, aborting login')
logger.error('No user given, aborting login')
return False
passwd = getpass('Password: ')
handler.add_password(None, netloc, user, passwd)
6 changes: 3 additions & 3 deletions neurotic/datasets/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def select(self, selection):
Select a metadata set.
"""
if self.all_metadata is None:
logger.critical('Load metadata before selecting')
logger.error('Load metadata before selecting')
elif selection not in self.all_metadata:
raise ValueError('{} was not found in {}'.format(selection, self.file))
else:
Expand Down Expand Up @@ -451,7 +451,7 @@ def _download_file(metadata, file, **kwargs):
"""

if not _is_url(metadata['remote_data_dir']):
logger.critical('metadata[remote_data_dir] is not a full URL')
logger.error('metadata[remote_data_dir] is not a full URL')
return

if metadata[file]:
Expand All @@ -473,7 +473,7 @@ def _download_all_data_files(metadata, **kwargs):
"""

if not _is_url(metadata['remote_data_dir']):
logger.critical('metadata[remote_data_dir] is not a full URL')
logger.error('metadata[remote_data_dir] is not a full URL')
return

for file in [k for k in metadata if k.endswith('_file')]:
Expand Down
4 changes: 2 additions & 2 deletions neurotic/gui/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def show(self, name):
else:
logger.warning(self.viewer_settings[name]['reason'])
else:
logger.critical(f'"{name}" is not a viewer in viewer_settings')
logger.error(f'"{name}" is not a viewer in viewer_settings')

def hide(self, name):
"""
Expand All @@ -197,7 +197,7 @@ def hide(self, name):
if name in self.viewer_settings:
self.viewer_settings[name]['show'] = False
else:
logger.critical(f'"{name}" is not a viewer in viewer_settings')
logger.error(f'"{name}" is not a viewer in viewer_settings')

def show_all(self):
"""
Expand Down
22 changes: 13 additions & 9 deletions neurotic/gui/standalone.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def __init__(self, file=None, initial_selection=None, lazy=True, theme='light',
try:
self.metadata_selector.setCurrentRow(list(self.metadata_selector.all_metadata).index(initial_selection))
except (TypeError, ValueError) as e:
logger.critical(f'Bad dataset key, will ignore: {e}')
logger.error(f'Bad dataset key, will ignore: {e}')

def create_menus(self):
"""
Expand Down Expand Up @@ -228,7 +228,7 @@ def edit_metadata(self):
try:
open_path_with_default_program(self.metadata_selector.file)
except FileNotFoundError as e:
logger.critical(f'The metadata file was not found: {e}')
logger.error(f'The metadata file was not found: {e}')
return

def download_files(self):
Expand Down Expand Up @@ -261,9 +261,9 @@ def open_directory(self):
try:
open_path_with_default_program(self.metadata_selector['data_dir'])
except FileNotFoundError as e:
logger.critical('The directory for the selected dataset was not '
'found locally, perhaps because it does not exist '
f'yet: {e}')
logger.error('The directory for the selected dataset was not '
'found locally, perhaps because it does not exist '
f'yet: {e}')

def launch(self):
"""
Expand Down Expand Up @@ -293,8 +293,12 @@ def launch(self):

except FileNotFoundError as e:

logger.critical('Some files were not found locally and may need '
f'to be downloaded: {e}')
logger.error('Some files were not found locally and may need to '
f'be downloaded: {e}')

except Exception:

logger.exception('Encountered a fatal error. Traceback will be written to log file.')

def view_log_file(self):
"""
Expand All @@ -304,7 +308,7 @@ def view_log_file(self):
try:
open_path_with_default_program(log_file)
except FileNotFoundError as e:
logger.critical(f'The log file was not found: {e}')
logger.error(f'The log file was not found: {e}')
return

def show_about(self):
Expand Down Expand Up @@ -444,7 +448,7 @@ def load(self):
try:
MetadataSelector.load(self)
except Exception as e:
logger.critical(f'Bad metadata file: {e}')
logger.error(f'Bad metadata file\n{e}')

if self.all_metadata is not None:

Expand Down
6 changes: 3 additions & 3 deletions neurotic/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,16 @@ def launch_example_notebook():
out = subprocess.Popen(['jupyter', 'notebook', '--version'],
stdout=subprocess.PIPE).communicate()[0]
except FileNotFoundError as e:
logger.critical('Unable to verify Jupyter is installed using "jupyter '
'notebook --version". Is it installed?')
logger.error('Unable to verify Jupyter is installed using "jupyter '
'notebook --version". Is it installed?')

if out:
# run Jupyter on the example notebook
try:
out = subprocess.Popen(['jupyter', 'notebook', path],
stdout=subprocess.PIPE).communicate()[0]
except FileNotFoundError as e:
logger.critical(f'Unable to locate the example notebook at {path}')
logger.error(f'Unable to locate the example notebook at {path}')

def main():
"""
Expand Down

0 comments on commit a692c58

Please sign in to comment.