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
Jonathan A. Sternberg 2019-05-13 12:02:05 -05:00 committed by GitHub
parent c7e2dab0a5
commit 01e3317b02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 2 deletions

View File

@ -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
}