diff --git a/tldr.py b/tldr.py index bccf57b..f5b98d7 100755 --- a/tldr.py +++ b/tldr.py @@ -75,8 +75,8 @@ def load_page_from_cache(command, platform, language): with open(get_cache_file_path( command, platform, - language), 'rb' - ) as cache_file: + language + ), 'rb') as cache_file: cache_file_contents = cache_file.read() return cache_file_contents except Exception: @@ -176,6 +176,7 @@ def get_language_list(): def get_page(command, remote=None, platforms=None, languages=None): + returnErr = {} if platforms is None: platforms = get_platform_list() if languages is None: @@ -185,15 +186,23 @@ def get_page(command, remote=None, platforms=None, languages=None): if platform is None: continue try: - return get_page_for_platform(command, platform, remote, language) + return { + "entry": get_page_for_platform( + command, + platform, + remote, + language + ), + "error": False + } except HTTPError as err: if err.code != 404: - raise - except URLError: + returnErr = {"entry": err, "error": True} + except URLError as err: if not PAGES_SOURCE_LOCATION.startswith('file://'): - raise + returnErr = {"entry": err, "error": True} - return False + return returnErr DEFAULT_COLORS = { @@ -269,7 +278,7 @@ def output(page): colored( line.replace('>', '').replace('<', ''), *colors_of('description') - ) + ) sys.stdout.buffer.write(line.encode('utf-8')) elif line[0] == '-': line = '\n' + ' ' * LEADING_SPACES_NUM + \ @@ -405,24 +414,23 @@ def main(): with open(command, encoding='utf-8') as open_file: output(open_file.read().encode('utf-8').splitlines()) else: - try: - command = '-'.join(options.command) - result = get_page( - command, - options.source, - options.platform, - options.language - ) - if not result: - sys.exit(( - "`{cmd}` documentation is not available. " - "Consider contributing Pull Request to " - "https://github.com/tldr-pages/tldr" - ).format(cmd=command)) - else: - output(result) - except URLError as e: - sys.exit("Error fetching from tldr: {}".format(e)) + command = '-'.join(options.command) + result = get_page( + command, + options.source, + options.platform, + options.language + ) + if result.get('error'): + sys.exit("Error fetching from tldr: {}".format(result['entry'])) + elif result.get("entry"): + output(result['entry']) + else: + sys.exit(( + "`{cmd}` documentation is not available. " + "Consider contributing Pull Request to " + "https://github.com/tldr-pages/tldr" + ).format(cmd=command)) def cli():