From e6ecc665af625486d22b248a76aa1b6a3531ecec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 17 Jun 2024 10:50:26 +0200 Subject: [PATCH] Use JSON error messages on response with error status from UPS Closes #13341 (grafted from eb0d39161aa9b52dd593258b7cda2e517dfdf36b) --HG-- branch : 6.0 --- modules/stock_package_shipping_ups/stock.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/modules/stock_package_shipping_ups/stock.py b/modules/stock_package_shipping_ups/stock.py index 43788737de..98ec214b9d 100644 --- a/modules/stock_package_shipping_ups/stock.py +++ b/modules/stock_package_shipping_ups/stock.py @@ -182,15 +182,19 @@ class CreateShippingUPS(Wizard): try: while nb_tries < 5 and response is None: try: - req = requests.post(api_url, json=shipment_request) + response = requests.post(api_url, json=shipment_request) except ssl.SSLError as e: error_message = e.reason nb_tries += 1 continue - req.raise_for_status() - response = req.json() + response.raise_for_status() + response = response.json() except requests.HTTPError as e: - error_message = e.args[0] + try: + errors = e.response.json()['response']['errors'] + error_message = "\n".join(m['message'] for m in errors) + except (requests.JSONDecodeError, TypeError, KeyError): + error_message = e.args[0] if error_message: raise UPSError(