From d5517350218d908593a8c62b4771069cb394eb5e Mon Sep 17 00:00:00 2001 From: Igor Chubin Date: Sat, 2 Jan 2021 17:35:01 +0100 Subject: [PATCH] Handle 404 properly (#500, #459, #372) --- lib/view/wttr.py | 30 ++++++++++++++++++++++++++---- lib/wttr_srv.py | 4 ++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/view/wttr.py b/lib/view/wttr.py index edb1375..1c0b205 100644 --- a/lib/view/wttr.py +++ b/lib/view/wttr.py @@ -36,9 +36,28 @@ def get_wetter(parsed_query): (returncode != 0 \ and ('Unable to find any matching weather' ' location to the parsed_query submitted') in stderr): - stdout, stderr, returncode = _wego_wrapper(NOT_FOUND_LOCATION, parsed_query) - location_not_found = True - stdout += get_message('NOT_FOUND_MESSAGE', lang) + stdout, stderr, returncode = _wego_wrapper(DEFAULT_LOCATION, parsed_query) + location_not_found = True + + not_found_header = """ +>>> _ _ ___ _ _ +>>> | || | / _ \| || | +>>> | || |_| | | | || |_ +>>> |__ _| |_| |__ _| +>>> |_| \___/ |_| +>>> +>>> 404 %s: %s +>>> +""" % (get_message("UNKNOWN_LOCATION", lang).upper(), parsed_query['override_location_name']) + + not_found_header = "\n".join("\033[48;5;91m" + x + " \033[0m" + for x in not_found_header.splitlines()[1:]) + + not_found_footer = get_message('NOT_FOUND_MESSAGE', lang) + not_found_footer = "\n".join("\033[48;5;91m " + x + " \033[0m" + for x in not_found_footer.splitlines() if x) + "\n" + + stdout = not_found_header + "\n----\n" + stdout + not_found_footer if "\n" in stdout: first_line, stdout = _wego_postprocessing(location, parsed_query, stdout) @@ -53,7 +72,10 @@ def get_wetter(parsed_query): def _wego_wrapper(location, parsed_query): lang = parsed_query['lang'] - location_name = parsed_query['override_location_name'] + if location == DEFAULT_LOCATION: + location_name = DEFAULT_LOCATION.capitalize() + else: + location_name = parsed_query['override_location_name'] cmd = [WEGO, '--city=%s' % location] diff --git a/lib/wttr_srv.py b/lib/wttr_srv.py index 531f6be..0d9eeef 100644 --- a/lib/wttr_srv.py +++ b/lib/wttr_srv.py @@ -375,6 +375,10 @@ def wttr(location, request): if not response: parsed_query = parse_request(location, request, query) response = _response(parsed_query, query) + + if parsed_query["location"] == NOT_FOUND_LOCATION: + http_code = 404 + # pylint: disable=broad-except except Exception as exception: logging.error("Exception has occured", exc_info=1)