From 2bdc3a3ba1cebafc41ccbcb1042db64c929019d0 Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Feb 17 2021 12:54:24 +0000 Subject: cli: show connection exception For anonymous calls (whose use ensure_connection) is now shown also original exception if --debug is specified. Fixes: https://pagure.io/koji/issue/2592 --- diff --git a/cli/koji_cli/commands.py b/cli/koji_cli/commands.py index 7118990..dfc20cb 100644 --- a/cli/koji_cli/commands.py +++ b/cli/koji_cli/commands.py @@ -930,7 +930,7 @@ def anon_handle_mock_config(goptions, session, args): parser.add_option("--yum-proxy", help=_("Specify a yum proxy")) parser.add_option("-o", metavar="FILE", dest="ofile", help=_("Output to a file")) (options, args) = parser.parse_args(args) - ensure_connection(session) + ensure_connection(session, goptions) if args: # for historical reasons, we also accept buildroot name as first arg if not options.name: @@ -2359,7 +2359,7 @@ def anon_handle_latest_build(goptions, session, args): (options, args) = parser.parse_args(args) if len(args) == 0: parser.error(_("A tag name must be specified")) - ensure_connection(session) + ensure_connection(session, goptions) if options.all: if len(args) > 1: parser.error(_("A package name may not be combined with --all")) @@ -2427,7 +2427,7 @@ def anon_handle_list_api(goptions, session, args): (options, args) = parser.parse_args(args) if len(args) != 0: parser.error(_("This command takes no arguments")) - ensure_connection(session) + ensure_connection(session, goptions) for x in sorted(session._listapi(), key=lambda x: x['name']): if 'argdesc' in x: args = x['argdesc'] @@ -2473,7 +2473,7 @@ def anon_handle_list_tagged(goptions, session, args): parser.error(_("A tag name must be specified")) elif len(args) > 2: parser.error(_("Only one package name may be specified")) - ensure_connection(session) + ensure_connection(session, goptions) pathinfo = koji.PathInfo() package = None if len(args) > 1: @@ -2572,7 +2572,7 @@ def anon_handle_list_buildroot(goptions, session, args): (options, args) = parser.parse_args(args) if len(args) != 1: parser.error(_("Incorrect number of arguments")) - ensure_connection(session) + ensure_connection(session, goptions) buildrootID = int(args[0]) opts = {} if options.built: @@ -2599,7 +2599,7 @@ def anon_handle_list_untagged(goptions, session, args): (options, args) = parser.parse_args(args) if len(args) > 1: parser.error(_("Only one package name may be specified")) - ensure_connection(session) + ensure_connection(session, goptions) package = None if len(args) > 0: package = args[0] @@ -2667,7 +2667,7 @@ def anon_handle_list_groups(goptions, session, args): opts = {} if options.incl_blocked: opts['incl_blocked'] = True - ensure_connection(session) + ensure_connection(session, goptions) event = koji.util.eventFromOpts(session, options) if event: opts['event'] = event['id'] @@ -2796,7 +2796,7 @@ def anon_handle_list_channels(goptions, session, args): parser.add_option("--quiet", action="store_true", default=goptions.quiet, help=_("Do not print header information")) (options, args) = parser.parse_args(args) - ensure_connection(session) + ensure_connection(session, goptions) channels = session.listChannels() channels = sorted(channels, key=lambda x: x['name']) session.multicall = True @@ -2846,7 +2846,7 @@ def anon_handle_list_hosts(goptions, session, args): parser.add_option("--description", action="store_true", help=_("Show descriptions")) (options, args) = parser.parse_args(args) opts = {} - ensure_connection(session) + ensure_connection(session, goptions) if options.arch: opts['arches'] = options.arch if options.channel: @@ -2947,7 +2947,7 @@ def anon_handle_list_pkgs(goptions, session, args): (options, args) = parser.parse_args(args) if len(args) != 0: parser.error(_("This command takes no arguments")) - ensure_connection(session) + ensure_connection(session, goptions) opts = {} if options.owner: user = session.getUser(options.owner) @@ -3041,7 +3041,7 @@ def anon_handle_list_builds(goptions, session, args): (options, args) = parser.parse_args(args) if len(args) != 0: parser.error(_("This command takes no arguments")) - ensure_connection(session) + ensure_connection(session, goptions) opts = {} for key in ('type', 'prefix', 'pattern'): value = getattr(options, key) @@ -3141,7 +3141,7 @@ def anon_handle_rpminfo(goptions, session, args): (options, args) = parser.parse_args(args) if len(args) < 1: parser.error(_("Please specify an RPM")) - ensure_connection(session) + ensure_connection(session, goptions) for rpm in args: info = session.getRPM(rpm) if info is None: @@ -3213,7 +3213,7 @@ def anon_handle_buildinfo(goptions, session, args): (options, args) = parser.parse_args(args) if len(args) < 1: parser.error(_("Please specify a build")) - ensure_connection(session) + ensure_connection(session, goptions) for build in args: if build.isdigit(): build = int(build) @@ -3311,7 +3311,7 @@ def anon_handle_hostinfo(goptions, session, args): (options, args) = parser.parse_args(args) if len(args) < 1: parser.error(_("Please specify a host")) - ensure_connection(session) + ensure_connection(session, goptions) for host in args: if host.isdigit(): host = int(host) @@ -4025,7 +4025,7 @@ def anon_handle_list_targets(goptions, session, args): (options, args) = parser.parse_args(args) if len(args) != 0: parser.error(_("This command takes no arguments")) - ensure_connection(session) + ensure_connection(session, goptions) fmt = "%(name)-30s %(build_tag_name)-30s %(dest_tag_name)-30s" if not options.quiet: @@ -4107,7 +4107,7 @@ def anon_handle_list_tag_inheritance(goptions, session, args): (options, args) = parser.parse_args(args) if len(args) != 1: parser.error(_("This command takes exactly one argument: a tag name or ID")) - ensure_connection(session) + ensure_connection(session, goptions) event = koji.util.eventFromOpts(session, options) if event: event['timestr'] = time.asctime(time.localtime(event['ts'])) @@ -4160,7 +4160,7 @@ def anon_handle_list_tags(goptions, session, args): parser.add_option("--build", help=_("Show tags associated with a build")) parser.add_option("--package", help=_("Show tags associated with a package")) (options, args) = parser.parse_args(args) - ensure_connection(session) + ensure_connection(session, goptions) pkginfo = {} buildinfo = {} @@ -4523,7 +4523,7 @@ def anon_handle_list_history(goptions, session, args): if not limited and not options.all: parser.error(_("Please specify an option to limit the query")) - ensure_connection(session) + ensure_connection(session, goptions) if options.watch: if not kwargs.get('afterEvent') and not kwargs.get('after'): @@ -4844,7 +4844,7 @@ def anon_handle_taskinfo(goptions, session, args): if len(args) < 1: parser.error(_("You must specify at least one task ID")) - ensure_connection(session) + ensure_connection(session, goptions) for arg in args: task_id = int(arg) @@ -4862,7 +4862,7 @@ def anon_handle_taginfo(goptions, session, args): (options, args) = parser.parse_args(args) if len(args) < 1: parser.error(_("Please specify a tag")) - ensure_connection(session) + ensure_connection(session, goptions) event = koji.util.eventFromOpts(session, options) event_opts = {} if event: @@ -5358,7 +5358,7 @@ def anon_handle_show_groups(goptions, session, args): parser.error(_("Incorrect number of arguments")) if options.incl_blocked and (options.comps or options.spec): parser.error(_("--show-blocked doesn't make sense for comps/spec output")) - ensure_connection(session) + ensure_connection(session, goptions) tag = args[0] callopts = {} if options.incl_blocked: @@ -5393,7 +5393,7 @@ def anon_handle_list_external_repos(goptions, session, args): (options, args) = parser.parse_args(args) if len(args) > 0: parser.error(_("This command takes no arguments")) - ensure_connection(session) + ensure_connection(session, goptions) opts = {} event = koji.util.eventFromOpts(session, options) if event: @@ -6493,7 +6493,7 @@ def anon_handle_watch_task(goptions, session, args): if options.mine: activate_session(session, goptions) else: - ensure_connection(session) + ensure_connection(session, goptions) if selection: tasks = [task['id'] for task in _list_tasks(options, session)] if not tasks: @@ -6534,7 +6534,7 @@ def anon_handle_watch_logs(goptions, session, args): print(_("You've no active tasks.")) return else: - ensure_connection(session) + ensure_connection(session, goptions) tasks = [] for task in args: try: @@ -6776,7 +6776,7 @@ def anon_handle_download_build(options, session, args): elif len(args) > 1: parser.error(_("Only a single package N-V-R or build ID may be specified")) - ensure_connection(session) + ensure_connection(session, options) build = args[0] if build.isdigit(): @@ -7002,7 +7002,7 @@ def anon_handle_download_task(options, session, args): if len(suboptions.arches) > 0: suboptions.arches = ",".join(suboptions.arches).split(",") - ensure_connection(session) + ensure_connection(session, options) # get downloadable tasks @@ -7434,7 +7434,7 @@ def anon_handle_list_notifications(goptions, session, args): parser.error(_("Use --user or --mine.")) if options.user: - ensure_connection(session) + ensure_connection(session, goptions) user = session.getUser(options.user) if not user: error("User %s does not exist" % options.user) @@ -7692,7 +7692,7 @@ def handle_unblock_notification(goptions, session, args): def handle_version(goptions, session, args): """Report client and hub versions""" - ensure_connection(session) + ensure_connection(session, goptions) print('Client: %s' % koji.__version__) try: version = session.getKojiVersion() diff --git a/cli/koji_cli/lib.py b/cli/koji_cli/lib.py index 4c0531f..0cfa00c 100644 --- a/cli/koji_cli/lib.py +++ b/cli/koji_cli/lib.py @@ -135,11 +135,15 @@ def get_usage_str(usage): return usage + _("\n(Specify the --help global option for a list of other help options)") -def ensure_connection(session): +def ensure_connection(session, options=None): try: ret = session.getAPIVersion() - except requests.exceptions.ConnectionError: - error(_("Error: Unable to connect to server")) + except requests.exceptions.ConnectionError as ex: + warn(_("Error: Unable to connect to server")) + if options and getattr(options, 'debug', False): + error(str(ex)) + else: + error() if ret != koji.API_VERSION: warn(_("WARNING: The server is at API version %d and " "the client is at %d" % (ret, koji.API_VERSION))) diff --git a/hub/kojixmlrpc.py b/hub/kojixmlrpc.py index 5578a65..4cac232 100644 --- a/hub/kojixmlrpc.py +++ b/hub/kojixmlrpc.py @@ -746,6 +746,7 @@ def application(environ, start_response): except Exception: return offline_reply(start_response, msg="database outage") h = ModXMLRPCRequestHandler(registry) + h.logger.error("original: %s", h.__class__) if environ.get('CONTENT_TYPE') == 'application/octet-stream': response = h._wrap_handler(h.handle_upload, environ) else: diff --git a/tests/test_cli/test_download_task.py b/tests/test_cli/test_download_task.py index 3918454..d2312ed 100644 --- a/tests/test_cli/test_download_task.py +++ b/tests/test_cli/test_download_task.py @@ -80,7 +80,7 @@ class TestDownloadTask(utils.CliTestCase): expected = '' self.assertMultiLineEqual(actual, expected) # Finally, assert that things were called as we expected. - self.ensure_connection.assert_called_once_with(self.session) + self.ensure_connection.assert_called_once_with(self.session, self.options) self.session.getTaskInfo.assert_called_once_with(task_id) self.session.getTaskChildren.assert_not_called() self.list_task_output_all_volumes.assert_called_once_with(self.session, task_id) @@ -105,7 +105,7 @@ class TestDownloadTask(utils.CliTestCase): expected = 'No such task: #123333\n' self.assertMultiLineEqual(actual, expected) # Finally, assert that things were called as we expected. - self.ensure_connection.assert_called_once_with(self.session) + self.ensure_connection.assert_called_once_with(self.session, self.options) self.session.getTaskInfo.assert_called_once_with(task_id) self.session.getTaskChildren.assert_not_called() @@ -147,7 +147,7 @@ class TestDownloadTask(utils.CliTestCase): expected = '' self.assertMultiLineEqual(actual, expected) # Finally, assert that things were called as we expected. - self.ensure_connection.assert_called_once_with(self.session) + self.ensure_connection.assert_called_once_with(self.session, self.options) self.session.getTaskInfo.assert_called_once_with(task_id) self.session.getTaskChildren.assert_called_once_with(task_id) self.assertEqual(self.list_task_output_all_volumes.mock_calls, [ @@ -188,7 +188,7 @@ class TestDownloadTask(utils.CliTestCase): expected = '' self.assertMultiLineEqual(actual, expected) # Finally, assert that things were called as we expected. - self.ensure_connection.assert_called_once_with(self.session) + self.ensure_connection.assert_called_once_with(self.session, self.options) self.session.getTaskInfo.assert_called_once_with(task_id) self.session.getTaskChildren.assert_not_called() self.list_task_output_all_volumes.assert_called_once_with(self.session, task_id) @@ -223,7 +223,7 @@ class TestDownloadTask(utils.CliTestCase): expected = 'No files for download found.\n' self.assertMultiLineEqual(actual, expected) # Finally, assert that things were called as we expected. - self.ensure_connection.assert_called_once_with(self.session) + self.ensure_connection.assert_called_once_with(self.session, self.options) self.session.getTaskInfo.assert_called_once_with(task_id) self.session.getTaskChildren.assert_not_called() self.list_task_output_all_volumes.assert_called_once_with(self.session, task_id) @@ -256,7 +256,7 @@ class TestDownloadTask(utils.CliTestCase): expected = 'Task 123333 has not finished yet.\n' self.assertMultiLineEqual(actual, expected) # Finally, assert that things were called as we expected. - self.ensure_connection.assert_called_once_with(self.session) + self.ensure_connection.assert_called_once_with(self.session, self.options) self.session.getTaskInfo.assert_called_once_with(task_id) self.session.getTaskChildren.assert_not_called() self.list_task_output_all_volumes.assert_called_once_with(self.session, task_id) @@ -287,7 +287,7 @@ class TestDownloadTask(utils.CliTestCase): expected = 'Child task 22222 has not finished yet.\n' self.assertMultiLineEqual(actual, expected) # Finally, assert that things were called as we expected. - self.ensure_connection.assert_called_once_with(self.session) + self.ensure_connection.assert_called_once_with(self.session, self.options) self.session.getTaskInfo.assert_called_once_with(task_id) self.session.getTaskChildren.assert_called_once_with(task_id) self.list_task_output_all_volumes.assert_called_once_with(self.session, 22222) @@ -314,7 +314,7 @@ class TestDownloadTask(utils.CliTestCase): expected = 'Invalid file name: somerpm..src.rpm\n' self.assertMultiLineEqual(actual, expected) # Finally, assert that things were called as we expected. - self.ensure_connection.assert_called_once_with(self.session) + self.ensure_connection.assert_called_once_with(self.session, self.options) self.session.getTaskInfo.assert_called_once_with(task_id) self.session.getTaskChildren.assert_not_called() self.list_task_output_all_volumes.assert_called_once_with(self.session, task_id) diff --git a/tests/test_cli/test_list_channels.py b/tests/test_cli/test_list_channels.py index a2a256a..dfe8f89 100644 --- a/tests/test_cli/test_list_channels.py +++ b/tests/test_cli/test_list_channels.py @@ -44,4 +44,4 @@ default 3 1 0 1 6 22% test 2 2 1 1 6 28% """ self.assertMultiLineEqual(actual, expected) - ensure_connection_mock.assert_called_once_with(self.session) + ensure_connection_mock.assert_called_once_with(self.session, self.options) diff --git a/tests/test_cli/test_list_notifications.py b/tests/test_cli/test_list_notifications.py index c2264d5..b324804 100644 --- a/tests/test_cli/test_list_notifications.py +++ b/tests/test_cli/test_list_notifications.py @@ -91,7 +91,7 @@ Notification blocks self.maxDiff=None self.assertMultiLineEqual(actual, expected) - ensure_connection_mock.assert_called_once_with(self.session) + ensure_connection_mock.assert_called_once_with(self.session, self.options) self.session.getTag.assert_has_calls([mock.call(1), mock.call(1)]) self.session.getPackage.assert_has_calls([mock.call(11), mock.call(11)]) self.session.getUser.assert_called_once_with('random_name') diff --git a/tests/test_cli/test_list_tagged.py b/tests/test_cli/test_list_tagged.py index 1b68113..20bf24d 100644 --- a/tests/test_cli/test_list_tagged.py +++ b/tests/test_cli/test_list_tagged.py @@ -70,7 +70,7 @@ class TestCliListTagged(utils.CliTestCase): args = ['tag', 'pkg', '--latest', '--inherit', '--event=1000'] anon_handle_list_tagged(self.options, self.session, args) - ensure_connection_mock.assert_called_once_with(self.session) + ensure_connection_mock.assert_called_once_with(self.session, self.options) self.session.getTag.assert_called_once_with('tag', event=1000) self.session.listTagged.assert_called_once_with('tag', event=1000, @@ -106,7 +106,7 @@ class TestCliListTagged(utils.CliTestCase): '--arch=x86_64', '--arch=noarch'] anon_handle_list_tagged(self.options, self.session, args) - ensure_connection_mock.assert_called_once_with(self.session) + ensure_connection_mock.assert_called_once_with(self.session, self.options) self.session.getTag.assert_called_once_with('tag', event=None) self.session.listTaggedRPMS.assert_called_once_with('tag', package='pkg', @@ -161,7 +161,7 @@ class TestCliListTagged(utils.CliTestCase): 'maven_artifact_id': 'artifact'}] anon_handle_list_tagged(self.options, self.session, args) - ensure_connection_mock.assert_called_once_with(self.session) + ensure_connection_mock.assert_called_once_with(self.session, self.options) self.session.getTag.assert_called_once_with('tag', event=None) self.session.listTagged.assert_called_once_with('tag', package='pkg',