Use errdefs package
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/cpuguy83/strongerrors"
|
||||
"github.com/pkg/errors"
|
||||
stats "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1"
|
||||
)
|
||||
@@ -21,20 +20,36 @@ func HandlePodStatsSummary(h PodStatsSummaryHandlerFunc) http.HandlerFunc {
|
||||
return handleError(func(w http.ResponseWriter, req *http.Request) error {
|
||||
stats, err := h(req.Context())
|
||||
if err != nil {
|
||||
if errors.Cause(err) == context.Canceled {
|
||||
return strongerrors.Cancelled(err)
|
||||
if isCancelled(err) {
|
||||
return err
|
||||
}
|
||||
return errors.Wrap(err, "error getting status from provider")
|
||||
}
|
||||
|
||||
b, err := json.Marshal(stats)
|
||||
if err != nil {
|
||||
return strongerrors.Unknown(errors.Wrap(err, "error marshalling stats"))
|
||||
return errors.Wrap(err, "error marshalling stats")
|
||||
}
|
||||
|
||||
if _, err := w.Write(b); err != nil {
|
||||
return strongerrors.Unknown(errors.Wrap(err, "could not write to client"))
|
||||
return errors.Wrap(err, "could not write to client")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func isCancelled(err error) bool {
|
||||
if err == context.Canceled {
|
||||
return true
|
||||
}
|
||||
|
||||
if e, ok := err.(causal); ok {
|
||||
return isCancelled(e.Cause())
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type causal interface {
|
||||
Cause() error
|
||||
error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user