Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apiutil: refine the log errs in apiutil #2722

Merged
merged 4 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions pkg/apiutil/serverapi/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strings"

"github.com/pingcap/log"
"github.com/pingcap/pd/v4/pkg/errs"
"github.com/pingcap/pd/v4/server"
"github.com/pingcap/pd/v4/server/config"
"github.com/urfave/negroni"
Expand Down Expand Up @@ -145,21 +146,21 @@ func (p *customReverseProxies) ServeHTTP(w http.ResponseWriter, r *http.Request)

resp, err := p.client.Do(r)
if err != nil {
log.Error("request failed", zap.Error(err))
log.Error("request failed", zap.Error(errs.ErrHTTPRequest.FastGenByArgs()), zap.NamedError("cause", err))
JmPotato marked this conversation as resolved.
Show resolved Hide resolved
continue
}

b, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Error("request failed", zap.Error(err))
log.Error("read failed", zap.Error(errs.ErrReadBody.FastGenByArgs()), zap.NamedError("cause", err))
continue
}

copyHeader(w.Header(), resp.Header)
w.WriteHeader(resp.StatusCode)
if _, err := w.Write(b); err != nil {
log.Error("write failed", zap.Error(err))
log.Error("write failed", zap.Error(errs.ErrWriteBody.FastGenByArgs()), zap.NamedError("cause", err))
continue
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/errs/errs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ var (
ClassPlacement = reg.RegisterErrorClass(5, "placement")
// ClassKV defines KV error class
ClassKV = reg.RegisterErrorClass(6, "kv")
// ClassAPIUtil defines apiutil error class
ClassAPIUtil = reg.RegisterErrorClass(7, "apiutil")
)

// tso errors
Expand Down Expand Up @@ -77,3 +79,10 @@ var (
ErrEtcdKvSave = ClassKV.DefineError().TextualCode("ErrEtcdKvSave").MessageTemplate("etcd kv save error, key is %s, value is %s").Build()
ErrEtcdKvRemove = ClassKV.DefineError().TextualCode("ErrEtcdKvRemove").MessageTemplate("etcd kv remove error, key is %s").Build()
)

// apiutil errors
var (
ErrHTTPRequest = ClassAPIUtil.DefineError().TextualCode("ErrHTTPRequest").MessageTemplate("fail to do a HTTP request").Build()
ErrReadBody = ClassAPIUtil.DefineError().TextualCode("ErrReadBody").MessageTemplate("fail to read a HTTP body").Build()
ErrWriteBody = ClassAPIUtil.DefineError().TextualCode("ErrWriteBody").MessageTemplate("fail to write a HTTP body").Build()
)