diff --git a/backend/utils/assets_client.py b/backend/utils/assets_client.py index 713772a..ca306da 100644 --- a/backend/utils/assets_client.py +++ b/backend/utils/assets_client.py @@ -66,15 +66,23 @@ def _do_request(action: str, body_dict: dict) -> dict: try: resp = service.json(action, {}, body) except Exception as e: - error_str = str(e) + # SDK raises Exception(resp.text.encode("utf-8")) on non-200; + # str(e) becomes b'...' which isn't valid JSON. Decode it first. + raw = e.args[0] if e.args else '' + error_str = raw.decode('utf-8') if isinstance(raw, bytes) else str(raw) + logger.warning('Assets API %s raw error: %s', action, error_str) try: error_data = json.loads(error_str) + err_meta = error_data.get('ResponseMetadata', {}).get('Error', {}) + if err_meta: + raise AssetsAPIError(err_meta.get('Code', 'Unknown'), err_meta.get('Message', error_str)) err = error_data.get('error', {}) raise AssetsAPIError(err.get('code', 'Unknown'), err.get('message', error_str)) except (json.JSONDecodeError, AssetsAPIError): raise except Exception: - raise AssetsAPIError('RequestError', error_str) + pass + raise AssetsAPIError('RequestError', error_str or 'Empty response from API') data = json.loads(resp) if isinstance(resp, str) else resp