我正在尝试找出一种方法来获取 JSON 解码时的错误原因http.Response.Body
if 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)
慕工程0101907
慕妹3242003
随时随地看视频慕课网APP
相关分类