我正在嘗試找出一種方法來獲取 JSON 解碼時的錯誤原因http.Response.Bodyif err := json.NewDecoder(resp.Body).Decode(&lResp); err != nil {? ? // Get the cause of err}的類型err(并errors.Cause(err)使用github.com/pkg/errors或github.com/friendsofgo/errors)是*errors.errorString。所以我現在能做的與檢查錯誤類型完全相反,即:if strings.HasSuffix(cause.Error(), "(Client.Timeout exceeded while reading body)") {? ? //...}我可以嘗試使用ioutil.ReadAll(),然后*http.httpError當超時發生時我會得到一個錯誤。主要原因是我不想獲取錯誤中部分讀取的 JSON 結構 - 僅獲取錯誤的原因,并且以當前的方式,它正在完成(返回的錯誤)我得到:main.ListingResponse.DataSources: struct CustomType{ /* partially read JSON struct ... */ }.net/http: request canceled (Client.Timeout exceeded while reading body)
http超時時如何獲取json.NewDecoder(body).Decode()的錯誤原因?
慕工程0101907
2023-07-31 17:16:34