cli: improve HTTP status code error logging

Use a different message for each status code when the request is
not successful.
master
Alejandro Sirgo Rica 2024-12-10 11:26:22 +01:00
parent 4b77e1bca8
commit 12d965ce1c
1 changed files with 21 additions and 3 deletions

View File

@ -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}")