]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
kresctl: improved error message, with Host/Path docs-develop-mana-tp23ui/deployments/2758
authorOto Šťáva <oto.stava@nic.cz>
Mon, 25 Sep 2023 07:11:36 +0000 (09:11 +0200)
committerOto Šťáva <oto.stava@nic.cz>
Mon, 25 Sep 2023 10:58:17 +0000 (12:58 +0200)
manager/knot_resolver_manager/utils/requests.py

index 0b026bb45312308b6dcf7a852a8fb679f5397cae..c46ed5dd05e552c748a4be93c0a4560daa9a6733 100644 (file)
@@ -4,7 +4,7 @@ import sys
 from http.client import HTTPConnection
 from typing import Any, Optional, Union
 from urllib.error import HTTPError, URLError
-from urllib.parse import quote
+from urllib.parse import quote, unquote, urlparse
 from urllib.request import AbstractHTTPHandler, Request, build_opener, install_opener, urlopen
 
 from typing_extensions import Literal
@@ -34,13 +34,20 @@ class Response:
 
 
 def _print_conn_error(error_desc: str, url: str, socket_source: str) -> None:
+    host: str
+    try:
+        parsed_url = urlparse(url)
+        host = unquote(parsed_url.hostname or "(Unknown)")
+    except Exception as e:
+        host = f"(Invalid URL: {e})"
     msg = f"""
-        {error_desc}
-        \tURL: {url}
-        \tSourced from: {socket_source}
-        Is the URL correct?
-        \tUnix socket would start with http+unix:// and URL encoded path.
-        \tInet sockets would start with http:// and domain or ip
+{error_desc}
+\tURL:           {url}
+\tHost/Path:     {host}
+\tSourced from:  {socket_source}
+Is the URL correct?
+\tUnix socket would start with http+unix:// and URL encoded path.
+\tInet sockets would start with http:// and domain or ip
     """
     print(msg, file=sys.stderr)
 
@@ -74,7 +81,7 @@ def request(
         elif err.errno == errno.ENOENT or isinstance(err.reason, FileNotFoundError):
             _print_conn_error("No such file or directory.", url, socket_desc.source)
         else:
-            print(f"{err}: url={url}", file=sys.stderr)
+            _print_conn_error(str(err), url, socket_desc.source)
         sys.exit(1)