From 112b68001bc252e4ca794f06a452401f93f9ee68 Mon Sep 17 00:00:00 2001 From: tengqm Date: Sun, 25 Jan 2015 19:26:12 +0800 Subject: [PATCH] Override print_list function for exception catching The print_list method may trigger exceptions from server response because the list is generated duing printing. --- senlinclient/common/utils.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/senlinclient/common/utils.py b/senlinclient/common/utils.py index 13bb83f4..08350e68 100644 --- a/senlinclient/common/utils.py +++ b/senlinclient/common/utils.py @@ -23,6 +23,7 @@ from oslo_utils import importutils from heatclient.common import template_utils from senlinclient.common import exc from senlinclient.common.i18n import _ +from senlinclient.common import sdk from senlinclient.openstack.common import cliutils @@ -62,13 +63,31 @@ def format_nested_dict(d, fields, column_names): return pt.get_string() + def nested_dict_formatter(d, column_names): return lambda o: format_nested_dict(o, d, column_names) + def json_formatter(js): return jsonutils.dumps(js, indent=2, ensure_ascii=False) +def print_list(objs, fields, formatters=None, sortby_index=0, + mixed_case_fields=None, field_labels=None): + # This wrapper is needed because sdk may yield a generator that will + # escape the exception catching previously + if not objs: + objs = [] + + try: + cliutils.print_list(objs, fields, formatters=formatters, + sortby_index=sortby_index, + mixed_case_fields=mixed_case_fields, + field_labels=field_labels) + except sdk.exceptions.HttpException as ex: + exc.parse_exception(ex.details) + + def print_dict(d, formatters=None): formatters = formatters or {} pt = prettytable.PrettyTable(['Property', 'Value'],