Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve JSON error handling in request wrappers #1803

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions apiserver/paasng/paasng/accessories/paas_analysis/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# to the current version of the project delivered to anyone in the future.

import datetime
import json
import logging
from contextlib import contextmanager
from dataclasses import dataclass
Expand All @@ -39,16 +38,16 @@
def wrap_request_exc():
try:
yield
except requests.JSONDecodeError as e:
logger.exception("The response from pa is not valid JSON.")
raise PAClientException("response not JSON") from e
except requests.RequestException as e:
# Handle the potential NoneType of e.request
request_info = e.request.url if e.request else "unknown"
logger.exception(f"Unable to fetch response from {request_info}")

error_msg = f"Something wrong happened when fetching {request_info}"
raise PAClientException(error_msg) from e
except json.decoder.JSONDecodeError as e:
logger.exception(f"invalid json response: {e.doc}")
raise PAClientException(f"invalid json response: {e.doc}") from e
except PAResponseError as e:
logger.exception(f"invalid response({e.status_code}) from {e.request_url}.Detail: {e.response_text}")
raise
Expand Down
11 changes: 5 additions & 6 deletions apiserver/paasng/paasng/accessories/servicehub/remote/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
# We undertake not to change the open source license (MIT license) applicable
# to the current version of the project delivered to anyone in the future.

"""Client for remote services
"""
import json
"""Client for remote services"""

import logging
from contextlib import contextmanager
from dataclasses import MISSING, dataclass
Expand Down Expand Up @@ -101,12 +100,12 @@ def wrap_request_exc(client: "RemoteServiceClient"):
try:
logger.debug("[servicehub] calling remote service<%s>", client.config)
yield
except requests.JSONDecodeError as e:
logger.exception(f"invalid json response from {client.config.index_url}")
raise RemoteClientError(f"invalid json response: {e}") from e
except RequestException as e:
logger.exception(f"unable to fetch remote services from {client.config.index_url}")
raise RemoteClientError(f"unable to fetch remote services: {e}") from e
except json.decoder.JSONDecodeError as e:
logger.exception(f"invalid json response from {client.config.index_url}")
raise RemoteClientError(f"invalid json response: {e}") from e


class RemoteServiceClient:
Expand Down
7 changes: 3 additions & 4 deletions apiserver/paasng/paasng/infras/oauth2/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# We undertake not to change the open source license (MIT license) applicable
# to the current version of the project delivered to anyone in the future.

import json
import logging
from contextlib import contextmanager
from dataclasses import dataclass
Expand Down Expand Up @@ -48,6 +47,9 @@ def __post_init__(self):
def wrap_request_exc():
try:
yield
except requests.JSONDecodeError as e:
logger.exception("The response from bk-oauth is not valid JSON.")
raise BkOauthApiException("response not JSON") from e
except requests.RequestException as e:
# Handle the potential NoneType of e.request
request_info = e.request.url if e.request else "unknown"
Expand All @@ -56,9 +58,6 @@ def wrap_request_exc():

error_msg = f"Something wrong happened when fetching {request_info}"
raise BkOauthApiException(error_msg) from e
except json.decoder.JSONDecodeError as e:
logger.exception(f"invalid json response: {e.doc}")
raise BkOauthApiException(f"invalid json response: {e.doc}") from e
except BkOauthApiResponseError as e:
logger.exception(
"invalid response(%s) from %s ,request_id: %s ,Detail: %s",
Expand Down