@@ -311,24 +311,41 @@ func (c *Client) handleErrorResp(resp *http.Response) error {
311311 if err != nil {
312312 return fmt .Errorf ("error, reading response body: %w" , err )
313313 }
314- var errRes ErrorResponse
315- err = json .Unmarshal (body , & errRes )
316- if err != nil || errRes .Error == nil {
317- reqErr := & RequestError {
318- HTTPStatus : resp .Status ,
319- HTTPStatusCode : resp .StatusCode ,
320- Err : err ,
321- Body : body ,
322- }
323- if errRes .Error != nil {
324- reqErr .Err = errRes .Error
325- }
326- return reqErr
314+
315+ // 尝试将响应体反序列化为单个 ErrorResponse
316+ var singleErrRes ErrorResponse
317+ err = json .Unmarshal (body , & singleErrRes )
318+ //if err != nil || errRes.Error == nil {
319+ // reqErr := &RequestError{
320+ // HTTPStatus: resp.Status,
321+ // HTTPStatusCode: resp.StatusCode,
322+ // Err: err,
323+ // Body: body,
324+ // }
325+ // if errRes.Error != nil {
326+ // reqErr.Err = errRes.Error
327+ // }
328+ // return reqErr
329+ //}
330+ if err == nil && singleErrRes .Error != nil {
331+ // 如果反序列化成功且 ErrorResponse 的 Error 字段不为空
332+ singleErrRes .Error .HTTPStatus = resp .Status
333+ singleErrRes .Error .HTTPStatusCode = resp .StatusCode
334+ return singleErrRes .Error
327335 }
328336
329- errRes .Error .HTTPStatus = resp .Status
330- errRes .Error .HTTPStatusCode = resp .StatusCode
331- return errRes .Error
337+ //errRes.Error.HTTPStatus = resp.Status
338+ //errRes.Error.HTTPStatusCode = resp.StatusCode
339+ //return errRes.Error
340+
341+ // 如果以上两种尝试都失败,则使用兜底逻辑
342+ reqErr := & RequestError {
343+ HTTPStatus : resp .Status ,
344+ HTTPStatusCode : resp .StatusCode ,
345+ Err : fmt .Errorf ("failed to unmarshal response body to ErrorResponse or ErrorResponse array" ),
346+ Body : body ,
347+ }
348+ return reqErr
332349}
333350
334351func containsSubstr (s []string , e string ) bool {
0 commit comments