Skip to content

Commit 1cfffc8

Browse files
Improve error handling of the API commands
1 parent 0646830 commit 1cfffc8

File tree

6 files changed

+39
-19
lines changed

6 files changed

+39
-19
lines changed

cloudinary_cli/cli.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ def main():
4444
else:
4545
log_exception(e, "Command execution failed")
4646

47+
if type(exit_status) == int:
48+
return exit_status
49+
4750
return 0 if exit_status or exit_status is None else 1
4851

4952

cloudinary_cli/cli_group.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ def cli(config, config_saved):
4040
if not is_valid_cloudinary_config():
4141
logger.warning("No Cloudinary configuration found.")
4242

43-
return 0
43+
return True

cloudinary_cli/core/utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ def utils(params, optional_parameter, optional_parameter_parsed, ls):
2222
if ls or len(params) < 1:
2323
return print_api_help(cld_utils, allow_list=utils_list)
2424

25-
echo(handle_command(params, optional_parameter, optional_parameter_parsed, cld_utils, "Utils"))
25+
res = handle_command(params, optional_parameter, optional_parameter_parsed, cld_utils, "Utils")
26+
if not res:
27+
return False
28+
29+
echo(res)
30+
31+
return True
2632

2733

2834
@command("url", help="Generate a Cloudinary URL, which you can optionally open in your browser.")

cloudinary_cli/utils/api_utils.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,11 @@ def asset_source(asset_details):
118118

119119

120120
def call_api(func, args, kwargs):
121-
return func(*args, **kwargs)
121+
try:
122+
return func(*args, **kwargs)
123+
except Exception as e:
124+
log_exception(e, f"Failed calling '{func.__name__}' with args: {args} and optional args {kwargs}")
125+
raise
122126

123127

124128
def handle_command(
@@ -127,11 +131,13 @@ def handle_command(
127131
optional_parameter_parsed,
128132
module,
129133
module_name):
130-
func, args, kwargs = get_command_params(params,
131-
optional_parameter,
132-
optional_parameter_parsed,
133-
module,
134-
module_name)
134+
try:
135+
func, args, kwargs = get_command_params(params, optional_parameter, optional_parameter_parsed, module,
136+
module_name)
137+
except Exception as e:
138+
log_exception(e)
139+
return False
140+
135141
return call_api(func, args, kwargs)
136142

137143

@@ -157,17 +163,20 @@ def handle_api_command(
157163
if ls or len(params) < 1:
158164
return print_api_help(api_instance)
159165

160-
func, args, kwargs = get_command_params(
161-
params,
162-
optional_parameter,
163-
optional_parameter_parsed,
164-
api_instance,
165-
api_name)
166+
try:
167+
func, args, kwargs = get_command_params(params, optional_parameter, optional_parameter_parsed, api_instance,
168+
api_name)
169+
except Exception as e:
170+
log_exception(e)
171+
return False
166172

167173
if not is_valid_cloudinary_config():
168174
raise ConfigurationError("No Cloudinary configuration found.")
169175

170-
res = call_api(func, args, kwargs)
176+
try:
177+
res = call_api(func, args, kwargs)
178+
except Exception:
179+
return False
171180

172181
if auto_paginate:
173182
res = handle_auto_pagination(res, func, args, kwargs, force, filter_fields)

cloudinary_cli/utils/utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ def get_help_str(module, block_list=(), allow_list=()):
5454

5555
funcs = OrderedDict(sorted(funcs.items()))
5656

57-
template = "{0:" + str(len(max(funcs.keys(), key=len)) + 1) + "}({1:30} {2}" # Gets the max length of the functions' names
57+
# Gets the max length of the functions' names
58+
template = "{0:" + str(len(max(funcs.keys(), key=len)) + 1) + "}({1:30} {2}"
5859

5960
return '\n'.join(
6061
[
@@ -115,7 +116,8 @@ def parse_args_kwargs(func, params):
115116

116117
n_req = n_args - n_defaults
117118
if len(params) < n_req:
118-
raise Exception("Function '{}' requires {} arguments".format(func.__name__, n_req))
119+
func_sig = signature(func)
120+
raise Exception(f"Function '{func.__name__}{func_sig}' requires {n_req} positional arguments")
119121
# consume required args
120122
args = [parse_option_value(p) for p in params[:n_req]]
121123
kwargs = {}
@@ -205,7 +207,7 @@ def get_command_params(
205207
if not callable(func):
206208
raise Exception(f"{params[0]} is not callable.")
207209

208-
args, kwargs = parse_args_kwargs(func, params[1:]) if len(params) > 1 else ([], {})
210+
args, kwargs = parse_args_kwargs(func, params[1:])
209211

210212
kwargs = {
211213
**kwargs,

test/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def test_parse_args_kwargs(self):
3030
self.assertListEqual(["a1", "a2"], args)
3131
self.assertEqual(0, len(kwargs))
3232

33-
with self.assertRaisesRegex(Exception, "requires 2 arguments"):
33+
with self.assertRaisesRegex(Exception, "requires 2 positional arguments"):
3434
parse_args_kwargs(_only_args_test_func, ["a1"])
3535

3636
args, kwargs = parse_args_kwargs(_args_kwargs_test_func, ["a1", 'arg2=a2'])

0 commit comments

Comments
 (0)