Use errdefs package
This commit is contained in:
@@ -7,9 +7,9 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/cpuguy83/strongerrors"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/virtual-kubelet/virtual-kubelet/errdefs"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
remoteutils "k8s.io/client-go/tools/remotecommand"
|
||||
api "k8s.io/kubernetes/pkg/apis/core"
|
||||
@@ -56,7 +56,7 @@ func HandleContainerExec(h ContainerExecHandlerFunc) http.HandlerFunc {
|
||||
|
||||
streamOpts, err := getExecOptions(req)
|
||||
if err != nil {
|
||||
return strongerrors.InvalidArgument(err)
|
||||
return errdefs.AsInvalidInput(err)
|
||||
}
|
||||
|
||||
idleTimeout := time.Second * 30
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/cpuguy83/strongerrors/status"
|
||||
"github.com/virtual-kubelet/virtual-kubelet/errdefs"
|
||||
"github.com/virtual-kubelet/virtual-kubelet/log"
|
||||
)
|
||||
|
||||
@@ -17,7 +17,7 @@ func handleError(f handlerFunc) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
code, _ := status.HTTPCode(err)
|
||||
code := httpStatusCode(err)
|
||||
w.WriteHeader(code)
|
||||
io.WriteString(w, err.Error())
|
||||
logger := log.G(req.Context()).WithError(err).WithField("httpStatusCode", code)
|
||||
@@ -55,3 +55,16 @@ func (fw *flushWriter) Write(p []byte) (int, error) {
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
func httpStatusCode(err error) int {
|
||||
switch {
|
||||
case err == nil:
|
||||
return http.StatusOK
|
||||
case errdefs.IsNotFound(err):
|
||||
return http.StatusNotFound
|
||||
case errdefs.IsInvalidInput(err):
|
||||
return http.StatusBadRequest
|
||||
default:
|
||||
return http.StatusInternalServerError
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,9 +7,9 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/cpuguy83/strongerrors"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/virtual-kubelet/virtual-kubelet/errdefs"
|
||||
"github.com/virtual-kubelet/virtual-kubelet/log"
|
||||
)
|
||||
|
||||
@@ -33,7 +33,7 @@ func HandleContainerLogs(h ContainerLogsHandlerFunc) http.HandlerFunc {
|
||||
return handleError(func(w http.ResponseWriter, req *http.Request) error {
|
||||
vars := mux.Vars(req)
|
||||
if len(vars) != 3 {
|
||||
return strongerrors.NotFound(errors.New("not found"))
|
||||
return errdefs.NotFound("not found")
|
||||
}
|
||||
|
||||
ctx := req.Context()
|
||||
@@ -47,7 +47,7 @@ func HandleContainerLogs(h ContainerLogsHandlerFunc) http.HandlerFunc {
|
||||
if queryTail := q.Get("tailLines"); queryTail != "" {
|
||||
t, err := strconv.Atoi(queryTail)
|
||||
if err != nil {
|
||||
return strongerrors.InvalidArgument(errors.Wrap(err, "could not parse \"tailLines\""))
|
||||
return errdefs.AsInvalidInput(errors.Wrap(err, "could not parse \"tailLines\""))
|
||||
}
|
||||
tail = t
|
||||
}
|
||||
@@ -73,7 +73,7 @@ func HandleContainerLogs(h ContainerLogsHandlerFunc) http.HandlerFunc {
|
||||
}
|
||||
|
||||
if _, err := io.Copy(flushOnWrite(w), logs); err != nil {
|
||||
return strongerrors.Unknown(errors.Wrap(err, "error writing response to client"))
|
||||
return errors.Wrap(err, "error writing response to client")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/cpuguy83/strongerrors"
|
||||
"github.com/virtual-kubelet/virtual-kubelet/log"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
@@ -36,13 +35,13 @@ func HandleRunningPods(getPods PodListerFunc) http.HandlerFunc {
|
||||
codec := codecs.LegacyCodec(v1.SchemeGroupVersion)
|
||||
data, err := runtime.Encode(codec, podList)
|
||||
if err != nil {
|
||||
return strongerrors.System(err)
|
||||
return err
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, err = w.Write(data)
|
||||
if err != nil {
|
||||
return strongerrors.System(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -5,8 +5,7 @@ import (
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/cpuguy83/strongerrors"
|
||||
pkgerrors "github.com/pkg/errors"
|
||||
"github.com/virtual-kubelet/virtual-kubelet/errdefs"
|
||||
testutil "github.com/virtual-kubelet/virtual-kubelet/test/util"
|
||||
"gotest.tools/assert"
|
||||
is "gotest.tools/assert/cmp"
|
||||
@@ -37,7 +36,7 @@ func (m *mockProvider) UpdatePod(ctx context.Context, pod *corev1.Pod) error {
|
||||
func (m *mockProvider) GetPod(ctx context.Context, namespace, name string) (*corev1.Pod, error) {
|
||||
p := m.pods[path.Join(namespace, name)]
|
||||
if p == nil {
|
||||
return nil, strongerrors.NotFound(pkgerrors.New("not found"))
|
||||
return nil, errdefs.NotFound("not found")
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
@@ -45,7 +44,7 @@ func (m *mockProvider) GetPod(ctx context.Context, namespace, name string) (*cor
|
||||
func (m *mockProvider) GetPodStatus(ctx context.Context, namespace, name string) (*corev1.PodStatus, error) {
|
||||
p := m.pods[path.Join(namespace, name)]
|
||||
if p == nil {
|
||||
return nil, strongerrors.NotFound(pkgerrors.New("not found"))
|
||||
return nil, errdefs.NotFound("not found")
|
||||
}
|
||||
return &p.Status, nil
|
||||
}
|
||||
|
||||
@@ -22,9 +22,9 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/cpuguy83/strongerrors"
|
||||
"github.com/cpuguy83/strongerrors/status/ocstatus"
|
||||
pkgerrors "github.com/pkg/errors"
|
||||
"github.com/virtual-kubelet/virtual-kubelet/errdefs"
|
||||
"github.com/virtual-kubelet/virtual-kubelet/log"
|
||||
"github.com/virtual-kubelet/virtual-kubelet/manager"
|
||||
"github.com/virtual-kubelet/virtual-kubelet/trace"
|
||||
@@ -117,13 +117,13 @@ type PodControllerConfig struct {
|
||||
|
||||
func NewPodController(cfg PodControllerConfig) (*PodController, error) {
|
||||
if cfg.PodClient == nil {
|
||||
return nil, strongerrors.InvalidArgument(pkgerrors.New("must set core client"))
|
||||
return nil, errdefs.InvalidInput("must set core client")
|
||||
}
|
||||
if cfg.EventRecorder == nil {
|
||||
return nil, strongerrors.InvalidArgument(pkgerrors.New("must set event recorder"))
|
||||
return nil, errdefs.InvalidInput("must set event recorder")
|
||||
}
|
||||
if cfg.PodInformer == nil {
|
||||
return nil, strongerrors.InvalidArgument(pkgerrors.New("must set informer"))
|
||||
return nil, errdefs.InvalidInput("must set informer")
|
||||
}
|
||||
|
||||
return &PodController{
|
||||
|
||||
Reference in New Issue
Block a user