From 10a3897f92ee058155987cea23eec28b28377d4b Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Thu, 21 Nov 2024 16:46:18 +0100 Subject: cli: ensure the program returns 0 on success and 1 on error propagate a returncode in each operation and make it the returncode of the program. Prevent sys.exit calls in post(), get() and delete() request handlers to enable cleanup code and error handling. Keep a basic error log inside the request functions if the connection can't be established or if the response contains an error code. --- cli/objects/server.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'cli/objects/server.py') diff --git a/cli/objects/server.py b/cli/objects/server.py index 468ff00..26b2c50 100644 --- a/cli/objects/server.py +++ b/cli/objects/server.py @@ -13,8 +13,11 @@ class OgServer(): @staticmethod def list_servers(rest): - r = rest.get('/server') - print_json(r.text) + res = rest.get('/server') + if not res: + return 1 + print_json(res.text) + return 0 @staticmethod def add_server(rest, args): @@ -27,10 +30,13 @@ class OgServer(): if not check_address(parsed_args.ip): print(f'Invalid IP address: {parsed_args.ip}') - return + return 1 payload = {'address': parsed_args.ip} - rest.post('/server', payload=payload) + res = rest.post('/server', payload=payload) + if not res: + return 1 + return 0 @staticmethod def delete_server(rest, args): @@ -42,4 +48,7 @@ class OgServer(): help='server id in the database') parsed_args = parser.parse_args(args) payload = {'id': parsed_args.id} - rest.delete('/server', payload=payload) + res = rest.delete('/server', payload=payload) + if not res: + return 1 + return 0 -- cgit v1.2.3-18-g5258