From 2b8132b4066a0ac909da9abeb0caa14bb9202f3c Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Wed, 25 Oct 2023 10:44:44 -0700 Subject: [PATCH] Use server supplied error strings The zuul-web server produces a wealth of information about token auth errors, so in our error handling, if we recieve any such information in the returned json doc, pass that on to the user. The existing catch-all messages are retained as defaults. Also, avoid printing double tracebacks on error. Change-Id: I6a1f316c6bc380b36b31670ab495e81ac736f6e1 --- zuulclient/api/__init__.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/zuulclient/api/__init__.py b/zuulclient/api/__init__.py index 66833d5..d2f0f93 100644 --- a/zuulclient/api/__init__.py +++ b/zuulclient/api/__init__.py @@ -90,16 +90,28 @@ class ZuulRESTClient(object): def _check_request_status(self, req): try: req.raise_for_status() + msg = None except Exception as e: if req.status_code == 401: - raise ZuulRESTException( - 'Unauthorized - your token might be invalid or expired.') + msg = \ + 'Unauthorized - your token might be invalid or expired.' elif req.status_code == 403: - raise ZuulRESTException( - 'Insufficient privileges to perform the action.') + msg = \ + 'Insufficient privileges to perform the action.' else: - raise ZuulRESTException( - 'Unknown error code %s: "%s"' % (req.status_code, e)) + msg = \ + 'Unknown error code %s: "%s"' % (req.status_code, e) + + try: + doc = req.json() + msg = '%s: %s' % (doc['error'], doc['description']) + except Exception: + pass + # This is outside the above handler in order to suppress the + # original exception (this one will still have an appropriate + # traceback; we don't need both). + if msg: + raise ZuulRESTException(msg) def _check_scope(self, tenant): scope = self.info.get("tenant", None)