From 12d965ce1c1497df6f5d5a63fd562171ce01b9e7 Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Tue, 10 Dec 2024 11:26:22 +0100 Subject: cli: improve HTTP status code error logging Use a different message for each status code when the request is not successful. --- cli/cli.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/cli/cli.py b/cli/cli.py index 4b808f5..25c1daf 100644 --- a/cli/cli.py +++ b/cli/cli.py @@ -26,6 +26,24 @@ import requests import sys +def _log_http_status_code(res): + if res.status_code == 400: + print('Error 400: invalid payload') + elif res.status_code == 404: + print('Error 404: object not found') + elif res.status_code == 405: + print('Error 405: method not allowed') + elif res.status_code == 409: + print('Error 409: object already exists') + elif res.status_code == 423: + print('Error 423: object in use') + elif res.status_code == 501: + print('Error 501: cannot connect to database') + elif res.status_code == 507: + print('Error 500: disk full') + else: + print(f'Received status code {res.status_code}') + class OgREST(): def __init__(self, ip, port, api_token): self.URL = f'http://{ip}:{port}' @@ -38,7 +56,7 @@ class OgREST(): json=payload) if res.status_code != 200: - print(f"Error: Request to ogServer failed with status code {res.status_code}") + _log_http_status_code(res) return None except requests.exceptions.RequestException as e: @@ -54,7 +72,7 @@ class OgREST(): json=payload) if res.status_code not in {200, 202}: - print(f"Error: Request to ogServer failed with status code {res.status_code}") + _log_http_status_code(res) return None except requests.exceptions.RequestException as e: @@ -69,7 +87,7 @@ class OgREST(): headers=self.HEADERS, json=payload) if res.status_code != 200: - print(f"Error: Request to ogServer failed with status code {res.status_code}") + _log_http_status_code(res) return None except IOError as e: print(f"Error: Cannot connect to ogServer: {e}") -- cgit v1.2.3-18-g5258