From 09fe6ddd09c2b5d50f899b4be8621f842c294edc Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Tue, 2 Feb 2021 13:43:20 +0000
Subject: [PATCH] Make BadRequestErrors more verbose

---
 ai_web_common/rpc/core.py | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/ai_web_common/rpc/core.py b/ai_web_common/rpc/core.py
index edaf45d..b4719e6 100644
--- a/ai_web_common/rpc/core.py
+++ b/ai_web_common/rpc/core.py
@@ -27,11 +27,14 @@ HTTP_STATUS_CODE = attributes_helper.COMMON_ATTRIBUTES['HTTP_STATUS_CODE']
 
 class StatusError(Exception):
 
-    def __init__(self, url, resp):
+    def __init__(self, url, resp, extra_msg=None):
         self.url = url.url
         self.code = resp.status
-        super(StatusError, self).__init__('%s: HTTP error %d: %s' % (
-            self.url, self.code, resp.reason))
+        msg = '%s: HTTP error %d: %s' % (self.url, self.code, resp.reason)
+        if extra_msg:
+            msg += ' '
+            msg += extra_msg
+        super(StatusError, self).__init__(msg)
 
 
 class RetriableStatusError(StatusError):
@@ -41,16 +44,18 @@ class RetriableStatusError(StatusError):
 class BadRequestError(StatusError):
 
     def __init__(self, url, resp):
-        super(BadRequestError, self).__init__(url, resp)
-
         # Some backends return structured 400 errors with a
         # JSON-encoded body containing field-level validation
         # errors. Detect this and make it available in the 'errors'
         # attribute of the exception object.
         try:
             self.errors = _json_response_decoder(resp)
+            msg = json.dumps(self.errors)
         except ValueError:
             self.errors = {}
+            msg = None
+
+        super(BadRequestError, self).__init__(url, resp, extra_msg=msg)
 
 
 class AuthenticationError(StatusError):
-- 
GitLab