chg: Improve error reporting

Related https://github.com/Lookyloo/PyLookyloo/issues/32
pull/746/head
Raphaël Vinot 2023-07-24 12:48:41 +02:00
parent 177474e874
commit 36f9d3dc60
2 changed files with 25 additions and 8 deletions

View File

@ -364,6 +364,9 @@ class CapturesIndex(Mapping):
tree = None
cache: Dict[str, Union[str, int]] = {'uuid': uuid, 'capture_dir': capture_dir_str}
if capture_settings.get('url'):
cache['url'] = capture_settings['url']
if (capture_dir / 'error.txt').exists():
# Something went wrong
with (capture_dir / 'error.txt').open() as _error:
@ -375,7 +378,7 @@ class CapturesIndex(Mapping):
except json.decoder.JSONDecodeError:
# old format
error_to_cache = content
cache['error'] = f'The capture {capture_dir.name} has an error: {error_to_cache}'
cache['error'] = f'The capture {uuid} ({capture_dir.name}) has an error: {error_to_cache}'
if not (har_files := sorted(capture_dir.rglob('*.har'))):
har_files = sorted(capture_dir.rglob('*.har.gz'))
@ -384,10 +387,12 @@ class CapturesIndex(Mapping):
har = HarFile(har_files[0], uuid)
cache['title'] = har.initial_title
cache['timestamp'] = har.initial_start_time
cache['url'] = capture_settings['url'] if capture_settings.get('url') else har.root_url
cache['redirects'] = json.dumps(tree.redirects) if tree else ''
cache['incomplete_redirects'] = 0
cache['user_agent'] = har.root_user_agent if har.root_user_agent else 'No User Agent.'
if 'url' not in cache:
# if all went well, we already filled that one above.
cache['url'] = har.root_url
if har.root_referrer:
cache['referer'] = har.root_referrer
except Har2TreeError as e:

View File

@ -239,12 +239,24 @@ class Lookyloo():
'''Get basic information about the capture.'''
cache = self.capture_cache(capture_uuid)
if not cache:
# NOTE: Return an exception?
return {}
to_return = {'url': cache.url, 'title': cache.title,
'capture_time': cache.timestamp.isoformat(),
'user_agent': cache.user_agent,
'referer': cache.referer if cache.referer else ''}
return {'error': f'Unable to find UUID {capture_uuid} in the cache.'}
if not hasattr(cache, 'uuid'):
self.logger.critical(f'Cache for {capture_uuid} is broken: {cache}.')
return {'error': f'Sorry, the capture {capture_uuid} is broken, please report it to the admin.'}
to_return = {'uuid': cache.uuid,
'url': cache.url if hasattr(cache, 'url') else 'Unable to get URL for the capture'}
if hasattr(cache, 'error'):
to_return['error'] = cache.error
if hasattr(cache, 'title'):
to_return['title'] = cache.title
if hasattr(cache, 'timestamp'):
to_return['capture_time'] = cache.timestamp.isoformat()
if hasattr(cache, 'user_agent'):
to_return['user_agent'] = cache.user_agent
if hasattr(cache, 'referer'):
to_return['referer'] = cache.referer if cache.referer else ''
return to_return
def get_meta(self, capture_uuid: str, /) -> Dict[str, str]: