@@ -515,33 +515,37 @@ async def _request(
515515 headers ["Authorization" ] = f"Bearer { token } "
516516 try :
517517 async with self ._session .request (method , target , headers = headers , json = body ) as resp :
518- # Backend.AI's app-proxy fronts every deployment endpoint, and on
519- # 5xx it can emit HTML / plain-text bodies (e.g. cloud LB error
520- # pages) instead of JSON. Read text up front so the raw body is
521- # available either as the JSON-parse input or as context in the
522- # raised error.
523- raw = await resp .text ()
524- try :
525- payload = json .loads (raw ) if raw else None
526- except json .JSONDecodeError as e :
527- if resp .status >= 400 :
528- raise BackendAPIError (
529- resp .status , resp .reason or "HTTP error" , {"detail" : raw }
530- ) from e
531- raise BackendClientError (
532- f"deployment endpoint returned non-JSON response "
533- f"(status={ resp .status } ): { raw !r} "
534- ) from e
535- if not isinstance (payload , dict ):
536- raise BackendClientError (
537- f"deployment endpoint returned non-object payload "
538- f"(type={ type (payload ).__name__ } , status={ resp .status } ): { payload !r} "
539- )
518+ payload = await self ._parse_response (resp )
540519 self ._raise_for_status (resp , payload )
541520 return payload
542521 except aiohttp .ClientConnectionError as e :
543522 raise BackendClientError (f"failed to reach deployment endpoint: { e !r} " ) from e
544523
524+ @staticmethod
525+ async def _parse_response (resp : aiohttp .ClientResponse ) -> dict [str , Any ]:
526+ # Backend.AI's app-proxy fronts every deployment endpoint, and on
527+ # 5xx it can emit HTML / plain-text bodies (e.g. cloud LB error
528+ # pages) instead of JSON. Read text up front so the raw body is
529+ # available either as the JSON-parse input or as context in the
530+ # raised error.
531+ raw = await resp .text ()
532+ try :
533+ payload = json .loads (raw ) if raw else None
534+ except json .JSONDecodeError as e :
535+ if resp .status >= 400 :
536+ raise BackendAPIError (
537+ resp .status , resp .reason or "HTTP error" , {"detail" : raw }
538+ ) from e
539+ raise BackendClientError (
540+ f"deployment endpoint returned non-JSON response (status={ resp .status } ): { raw !r} "
541+ ) from e
542+ if not isinstance (payload , dict ):
543+ raise BackendClientError (
544+ f"deployment endpoint returned non-object payload "
545+ f"(type={ type (payload ).__name__ } , status={ resp .status } ): { payload !r} "
546+ )
547+ return payload
548+
545549 @staticmethod
546550 def _build_url (endpoint_url : str , path : str ) -> str :
547551 base = URL (endpoint_url )
0 commit comments