fix(http): do not discard non-json encoded errors when using `CheckError` (#13844)
If the error returned was normal text instead of JSON, the JSON parsing would fail and the JSON error would be returned instead of the actual error. This has been modified so that when it fails to parse the text as JSON, it will construct an error with the actual text and then wrap it using the JSON error.pull/13816/head
parent
c7e2dab0a5
commit
01e3317b02
|
@ -1,12 +1,16 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
stderrors "errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
platform "github.com/influxdata/influxdb"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -57,9 +61,17 @@ func CheckError(resp *http.Response) (err error) {
|
|||
}
|
||||
}
|
||||
pe := new(platform.Error)
|
||||
parseErr := json.NewDecoder(resp.Body).Decode(pe)
|
||||
|
||||
var buf bytes.Buffer
|
||||
if _, err := io.Copy(&buf, resp.Body); err != nil {
|
||||
return &platform.Error{
|
||||
Code: platform.EInternal,
|
||||
Msg: err.Error(),
|
||||
}
|
||||
}
|
||||
parseErr := json.Unmarshal(buf.Bytes(), pe)
|
||||
if parseErr != nil {
|
||||
return parseErr
|
||||
return errors.Wrap(stderrors.New(buf.String()), parseErr.Error())
|
||||
}
|
||||
return pe
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue