From 87a3ceb983620002b50a19c87cf4f97479ae11ad Mon Sep 17 00:00:00 2001 From: Kay0u Date: Sat, 25 Jan 2020 20:49:29 +0700 Subject: [PATCH 1/3] [FIX] bad response from the server --- data/hooks/diagnosis/14-ports.py | 22 +++++++++++++--------- data/hooks/diagnosis/16-http.py | 18 +++++++++++------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/data/hooks/diagnosis/14-ports.py b/data/hooks/diagnosis/14-ports.py index f9694a9de..aaf31d561 100644 --- a/data/hooks/diagnosis/14-ports.py +++ b/data/hooks/diagnosis/14-ports.py @@ -26,16 +26,20 @@ class PortsDiagnoser(Diagnoser): ports[port] = service try: - r = requests.post('https://diagnosis.yunohost.org/check-ports', json={'ports': ports.keys()}, timeout=30).json() - if "status" not in r.keys(): - raise Exception("Bad syntax for response ? Raw json: %s" % str(r)) - elif r["status"] == "error": - if "content" in r.keys(): - raise Exception(r["content"]) - else: + r = requests.post('https://diagnosis.yunohost.org/check-ports', json={'ports': ports.keys()}, timeout=30) + if r.status_code == 200: + r = r.json() + if "status" not in r.keys(): raise Exception("Bad syntax for response ? Raw json: %s" % str(r)) - elif r["status"] != "ok" or "ports" not in r.keys() or not isinstance(r["ports"], dict): - raise Exception("Bad syntax for response ? Raw json: %s" % str(r)) + elif r["status"] == "error": + if "content" in r.keys(): + raise Exception(r["content"]) + else: + raise Exception("Bad syntax for response ? Raw json: %s" % str(r)) + elif r["status"] != "ok" or "ports" not in r.keys() or not isinstance(r["ports"], dict): + raise Exception("Bad syntax for response ? Raw json: %s" % str(r)) + else: + raise Exception("Bad response from the server https://diagnosis.yunohost.org : %s" % str(r.status_code)) except Exception as e: raise YunohostError("diagnosis_ports_could_not_diagnose", error=e) diff --git a/data/hooks/diagnosis/16-http.py b/data/hooks/diagnosis/16-http.py index c7955c805..ac30dad78 100644 --- a/data/hooks/diagnosis/16-http.py +++ b/data/hooks/diagnosis/16-http.py @@ -28,14 +28,18 @@ class HttpDiagnoser(Diagnoser): os.system("touch /tmp/.well-known/ynh-diagnosis/%s" % nonce) try: - r = requests.post('https://diagnosis.yunohost.org/check-http', json={'domain': domain, "nonce": nonce}, timeout=30).json() - if "status" not in r.keys(): - raise Exception("Bad syntax for response ? Raw json: %s" % str(r)) - elif r["status"] == "error" and ("code" not in r.keys() or not r["code"].startswith("error_http_check_")): - if "content" in r.keys(): - raise Exception(r["content"]) - else: + r = requests.post('https://diagnosis.yunohost.org/check-http', json={'domain': domain, "nonce": nonce}, timeout=30) + if r.status_code == 200: + r = r.json() + if "status" not in r.keys(): raise Exception("Bad syntax for response ? Raw json: %s" % str(r)) + elif r["status"] == "error" and ("code" not in r.keys() or not r["code"].startswith("error_http_check_")): + if "content" in r.keys(): + raise Exception(r["content"]) + else: + raise Exception("Bad syntax for response ? Raw json: %s" % str(r)) + else: + raise Exception("Bad response from the server https://diagnosis.yunohost.org : %s" % str(r.status_code)) except Exception as e: raise YunohostError("diagnosis_http_could_not_diagnose", error=e) From 4399c9b740b5c4901de76f5b2245df4b349eae89 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sun, 22 Mar 2020 01:40:02 +0100 Subject: [PATCH 2/3] Tweak exception messages to hopefully help debugging if this happens --- data/hooks/diagnosis/14-ports.py | 2 +- data/hooks/diagnosis/16-http.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/data/hooks/diagnosis/14-ports.py b/data/hooks/diagnosis/14-ports.py index aaf31d561..9c0e94721 100644 --- a/data/hooks/diagnosis/14-ports.py +++ b/data/hooks/diagnosis/14-ports.py @@ -39,7 +39,7 @@ class PortsDiagnoser(Diagnoser): elif r["status"] != "ok" or "ports" not in r.keys() or not isinstance(r["ports"], dict): raise Exception("Bad syntax for response ? Raw json: %s" % str(r)) else: - raise Exception("Bad response from the server https://diagnosis.yunohost.org : %s" % str(r.status_code)) + raise Exception("Bad response from the server https://diagnosis.yunohost.org/check-ports : %s - %s" % (str(r.status_code), r.content)) except Exception as e: raise YunohostError("diagnosis_ports_could_not_diagnose", error=e) diff --git a/data/hooks/diagnosis/16-http.py b/data/hooks/diagnosis/16-http.py index ac30dad78..d36e85f41 100644 --- a/data/hooks/diagnosis/16-http.py +++ b/data/hooks/diagnosis/16-http.py @@ -39,7 +39,7 @@ class HttpDiagnoser(Diagnoser): else: raise Exception("Bad syntax for response ? Raw json: %s" % str(r)) else: - raise Exception("Bad response from the server https://diagnosis.yunohost.org : %s" % str(r.status_code)) + raise Exception("Bad response from the server https://diagnosis.yunohost.org/check-http : %s - %s" % (str(r.status_code), r.content)) except Exception as e: raise YunohostError("diagnosis_http_could_not_diagnose", error=e) From fff2fcd67cf66a6dfab02baa9c3ec036e9941394 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sun, 22 Mar 2020 01:41:58 +0100 Subject: [PATCH 3/3] Simplify identation (and diff) by reversing the if statement --- data/hooks/diagnosis/14-ports.py | 23 +++++++++++------------ data/hooks/diagnosis/16-http.py | 19 +++++++++---------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/data/hooks/diagnosis/14-ports.py b/data/hooks/diagnosis/14-ports.py index 9c0e94721..d81fd39cc 100644 --- a/data/hooks/diagnosis/14-ports.py +++ b/data/hooks/diagnosis/14-ports.py @@ -27,19 +27,18 @@ class PortsDiagnoser(Diagnoser): try: r = requests.post('https://diagnosis.yunohost.org/check-ports', json={'ports': ports.keys()}, timeout=30) - if r.status_code == 200: - r = r.json() - if "status" not in r.keys(): - raise Exception("Bad syntax for response ? Raw json: %s" % str(r)) - elif r["status"] == "error": - if "content" in r.keys(): - raise Exception(r["content"]) - else: - raise Exception("Bad syntax for response ? Raw json: %s" % str(r)) - elif r["status"] != "ok" or "ports" not in r.keys() or not isinstance(r["ports"], dict): - raise Exception("Bad syntax for response ? Raw json: %s" % str(r)) - else: + if r.status_code != 200: raise Exception("Bad response from the server https://diagnosis.yunohost.org/check-ports : %s - %s" % (str(r.status_code), r.content)) + r = r.json() + if "status" not in r.keys(): + raise Exception("Bad syntax for response ? Raw json: %s" % str(r)) + elif r["status"] == "error": + if "content" in r.keys(): + raise Exception(r["content"]) + else: + raise Exception("Bad syntax for response ? Raw json: %s" % str(r)) + elif r["status"] != "ok" or "ports" not in r.keys() or not isinstance(r["ports"], dict): + raise Exception("Bad syntax for response ? Raw json: %s" % str(r)) except Exception as e: raise YunohostError("diagnosis_ports_could_not_diagnose", error=e) diff --git a/data/hooks/diagnosis/16-http.py b/data/hooks/diagnosis/16-http.py index d36e85f41..bd862b408 100644 --- a/data/hooks/diagnosis/16-http.py +++ b/data/hooks/diagnosis/16-http.py @@ -29,17 +29,16 @@ class HttpDiagnoser(Diagnoser): try: r = requests.post('https://diagnosis.yunohost.org/check-http', json={'domain': domain, "nonce": nonce}, timeout=30) - if r.status_code == 200: - r = r.json() - if "status" not in r.keys(): - raise Exception("Bad syntax for response ? Raw json: %s" % str(r)) - elif r["status"] == "error" and ("code" not in r.keys() or not r["code"].startswith("error_http_check_")): - if "content" in r.keys(): - raise Exception(r["content"]) - else: - raise Exception("Bad syntax for response ? Raw json: %s" % str(r)) - else: + if r.status_code != 200: raise Exception("Bad response from the server https://diagnosis.yunohost.org/check-http : %s - %s" % (str(r.status_code), r.content)) + r = r.json() + if "status" not in r.keys(): + raise Exception("Bad syntax for response ? Raw json: %s" % str(r)) + elif r["status"] == "error" and ("code" not in r.keys() or not r["code"].startswith("error_http_check_")): + if "content" in r.keys(): + raise Exception(r["content"]) + else: + raise Exception("Bad syntax for response ? Raw json: %s" % str(r)) except Exception as e: raise YunohostError("diagnosis_http_could_not_diagnose", error=e)