Skip to content

Commit

Permalink
refactor errors
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Nov 3, 2022
1 parent d8ac04c commit a8b2981
Show file tree
Hide file tree
Showing 75 changed files with 281 additions and 250 deletions.
6 changes: 3 additions & 3 deletions auth/simple/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ func (b *BasicAuth) UnaryInterceptor(ctx context.Context, req interface{}, info
func (b *BasicAuth) doAuth(ctx context.Context) error {
meta, ok := metadata.FromIncomingContext(ctx)
if !ok {
return types.ErrBadMeta
return types.ErrInvaildGRPCRequestMeta
}
passwords, ok := meta[b.username]
if !ok {
return types.ErrInvaildUsername
return types.ErrInvaildGRPCUsername
}
if len(passwords) < 1 || passwords[0] != b.password {
return types.ErrInvaildPassword
return types.ErrInvaildGRPCPassword
}
return nil
}
2 changes: 1 addition & 1 deletion client/clientpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Pool struct {
// NewCoreRPCClientPool .
func NewCoreRPCClientPool(ctx context.Context, config *PoolConfig) (*Pool, error) {
if len(config.EruAddrs) == 0 {
return nil, types.ErrBadIPAddress
return nil, types.ErrInvaildEruIPAddress
}
c := &Pool{rpcClients: []*clientWithStatus{}}
for _, addr := range config.EruAddrs {
Expand Down
10 changes: 4 additions & 6 deletions cluster/calcium/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"os"
"time"

"github.com/pkg/errors"
"github.com/cockroachdb/errors"
enginetypes "github.com/projecteru2/core/engine/types"
"github.com/projecteru2/core/log"
"github.com/projecteru2/core/types"
Expand All @@ -21,8 +21,7 @@ func (c *Calcium) BuildImage(ctx context.Context, opts *types.BuildOptions) (ch
logger := log.WithField("Calcium", "BuildImage").WithField("opts", opts)
// Disable build API if scm not set
if c.source == nil {
logger.Error(ctx, types.ErrSCMNotSet)
return nil, types.ErrSCMNotSet
return nil, types.ErrNoSCMSetting
}
// select nodes
node, err := c.selectBuildNode(ctx)
Expand All @@ -45,8 +44,7 @@ func (c *Calcium) BuildImage(ctx context.Context, opts *types.BuildOptions) (ch
case types.BuildFromExist:
refs, node, resp, err = c.buildFromExist(ctx, opts)
default:
logger.Error(ctx, types.ErrUnknownBuildType)
return nil, types.ErrUnknownBuildType
return nil, types.ErrInvaildBuildType
}
if err != nil {
logger.Error(ctx, err)
Expand All @@ -71,7 +69,7 @@ func (c *Calcium) selectBuildNode(ctx context.Context) (*types.Node, error) {
}

if len(nodes) == 0 {
return nil, types.ErrInsufficientNodes
return nil, types.ErrInsufficientCapacity
}
// get idle max node
return c.getMostIdleNode(ctx, nodes)
Expand Down
6 changes: 3 additions & 3 deletions cluster/calcium/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestBuild(t *testing.T) {
c.config.Docker.BuildPod = "test"
// failed by ListPodNodes failed
store := c.store.(*storemocks.Store)
store.On("GetNodesByPod", mock.Anything, mock.Anything).Return(nil, types.ErrBadMeta).Once()
store.On("GetNodesByPod", mock.Anything, mock.Anything).Return(nil, types.ErrInvaildWorkloadMeta).Once()
ch, err := c.BuildImage(ctx, opts)
assert.Error(t, err)
// failed by no nodes
Expand All @@ -84,7 +84,7 @@ func TestBuild(t *testing.T) {
// failed by plugin error
rmgr := c.rmgr.(*resourcemocks.Manager)
rmgr.On("GetNodeResourceInfo", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, nil, nil, nil)
rmgr.On("GetMostIdleNode", mock.Anything, mock.Anything).Return("", types.ErrBadCount).Once()
rmgr.On("GetMostIdleNode", mock.Anything, mock.Anything).Return("", types.ErrInvaildCount).Once()
ch, err = c.BuildImage(ctx, opts)
assert.Error(t, err)
rmgr.On("GetMostIdleNode", mock.Anything, mock.Anything).Return("test", nil)
Expand All @@ -107,7 +107,7 @@ func TestBuild(t *testing.T) {
buildImageRespReader2 := io.NopCloser(bytes.NewReader(buildImageResp2))
engine.On("BuildRefs", mock.Anything, mock.Anything, mock.Anything).Return([]string{"t1", "t2"})
// failed by build context
engine.On("BuildContent", mock.Anything, mock.Anything, mock.Anything).Return("", nil, types.ErrBadCount).Once()
engine.On("BuildContent", mock.Anything, mock.Anything, mock.Anything).Return("", nil, types.ErrInvaildCount).Once()
ch, err = c.BuildImage(ctx, opts)
assert.Error(t, err)
b := io.NopCloser(bytes.NewReader([]byte{}))
Expand Down
4 changes: 2 additions & 2 deletions cluster/calcium/capacity.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/sanity-io/litter"
"golang.org/x/exp/maps"

"github.com/pkg/errors"
"github.com/cockroachdb/errors"
)

// CalculateCapacity calculates capacity
Expand Down Expand Up @@ -45,7 +45,7 @@ func (c *Calcium) CalculateCapacity(ctx context.Context, opts *types.DeployOptio
return err
}
if msg.Total <= 0 {
return errors.Wrap(types.ErrInsufficientRes, "no node meets all the resource requirements at the same time")
return errors.Wrap(types.ErrInsufficientResource, "no node meets all the resource requirements at the same time")
}
for node, info := range infos {
msg.NodeCapacities[node] = info.Capacity
Expand Down
2 changes: 1 addition & 1 deletion cluster/calcium/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (c *Calcium) ControlWorkload(ctx context.Context, ids []string, t string, f
message = append(message, startHook...)
return err
}
return types.ErrUnknownControlType
return types.ErrInvaildControlType
})
if err == nil {
logger.Infof(ctx, "[ControlWorkload] Workload %s %s", id, t)
Expand Down
3 changes: 1 addition & 2 deletions cluster/calcium/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ func (c *Calcium) CreateWorkload(ctx context.Context, opts *types.DeployOptions)
logger.Error(ctx, err)
return nil, err
}

opts.ProcessIdent = utils.RandomString(16)
logger.Infof(ctx, "[CreateWorkload %s] Creating workload with options:\n%s", opts.ProcessIdent, litter.Options{Compact: true}.Sdump(opts))
// Count 要大于0
if opts.Count <= 0 {
err := types.NewDetailedErr(types.ErrBadCount, opts.Count)
err := types.NewDetailedErr(types.ErrInvaildCount, opts.Count)
logger.Error(ctx, err)
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cluster/calcium/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func TestCreateWorkloadTxn(t *testing.T) {

// doAllocResource fails: Alloc
rmgr.On("Alloc", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(
nil, nil, types.ErrBadCPU,
nil, nil, types.ErrInvalidType,
).Once()
ch, err = c.CreateWorkload(ctx, opts)
assert.Nil(t, err)
Expand Down
2 changes: 1 addition & 1 deletion cluster/calcium/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestExecuteWorkload(t *testing.T) {
store := c.store.(*storemocks.Store)

// failed by GetWorkload
store.On("GetWorkload", mock.Anything, mock.Anything).Return(nil, types.ErrBadCount).Once()
store.On("GetWorkload", mock.Anything, mock.Anything).Return(nil, types.ErrInvaildCount).Once()
ID := "abc"
ch := c.ExecuteWorkload(ctx, &types.ExecuteWorkloadOptions{WorkloadID: ID}, nil)
for ac := range ch {
Expand Down
2 changes: 1 addition & 1 deletion cluster/calcium/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestLogStream(t *testing.T) {
}
store.On("GetWorkload", mock.Anything, mock.Anything).Return(workload, nil)
// failed by VirtualizationLogs
engine.On("VirtualizationLogs", mock.Anything, mock.Anything).Return(nil, nil, types.ErrNodeExist).Once()
engine.On("VirtualizationLogs", mock.Anything, mock.Anything).Return(nil, nil, types.ErrInvalidType).Once()
ch, err = c.LogStream(ctx, opts)
assert.NoError(t, err)
for c := range ch {
Expand Down
6 changes: 3 additions & 3 deletions cluster/calcium/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/projecteru2/core/types"
"github.com/projecteru2/core/utils"

"github.com/pkg/errors"
"github.com/cockroachdb/errors"
)

// ReplaceWorkload replace workloads with same resource
Expand Down Expand Up @@ -73,7 +73,7 @@ func (c *Calcium) ReplaceWorkload(ctx context.Context, opts *types.ReplaceOption
if err != nil {
return err
} else if !info.Running {
return types.NewDetailedErr(types.ErrNotSupport,
return types.NewDetailedErr(types.ErrInvaildWorkloadOps,
fmt.Sprintf("workload %s is not running, can not inherit", workload.ID),
)
}
Expand Down Expand Up @@ -114,7 +114,7 @@ func (c *Calcium) doReplaceWorkload(
}
// label filter
if !utils.LabelsFilter(workload.Labels, opts.FilterLabels) {
return nil, removeMessage, types.ErrNotFitLabels
return nil, removeMessage, types.ErrWorkloadIgnored
}
// prepare node
node, err := c.doGetAndPrepareNode(ctx, workload.Nodename, opts.Image)
Expand Down
2 changes: 1 addition & 1 deletion cluster/calcium/replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func TestReplaceWorkload(t *testing.T) {
store.On("GetNode", mock.Anything, mock.Anything).Return(node, nil)
// failed by VirtualizationCopyFrom
opts.Copy = map[string]string{"src": "dst"}
engine.On("VirtualizationCopyFrom", mock.Anything, mock.Anything, mock.Anything).Return(nil, 0, 0, int64(0), types.ErrBadWorkloadID).Once()
engine.On("VirtualizationCopyFrom", mock.Anything, mock.Anything, mock.Anything).Return(nil, 0, 0, int64(0), types.ErrInvalidType).Once()
ch, err = c.ReplaceWorkload(ctx, opts)
assert.NoError(t, err)
for r := range ch {
Expand Down
2 changes: 1 addition & 1 deletion cluster/calcium/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/projecteru2/core/types"
"github.com/projecteru2/core/utils"

"github.com/pkg/errors"
"github.com/cockroachdb/errors"
)

// WatchServiceStatus returns chan of available service address
Expand Down
2 changes: 1 addition & 1 deletion cluster/calcium/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"io"
"sync"

"github.com/pkg/errors"
"github.com/cockroachdb/errors"
"github.com/projecteru2/core/engine"
enginetypes "github.com/projecteru2/core/engine/types"
"github.com/projecteru2/core/log"
Expand Down
2 changes: 1 addition & 1 deletion cluster/calcium/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"sync"
"time"

"github.com/cockroachdb/errors"
"github.com/panjf2000/ants/v2"
"github.com/pkg/errors"
"github.com/projecteru2/core/cluster"
"github.com/projecteru2/core/log"
"github.com/projecteru2/core/store"
Expand Down
4 changes: 2 additions & 2 deletions cluster/calcium/wal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestHandleCreateWorkloadError(t *testing.T) {

store := c.store.(*storemocks.Store)

err = types.NewDetailedErr(types.ErrBadCount, fmt.Sprintf("keys: [%s]", wrkid))
err = types.NewDetailedErr(types.ErrInvaildCount, fmt.Sprintf("keys: [%s]", wrkid))
store.On("GetWorkload", mock.Anything, mock.Anything).Return(wrk, err).Once()
store.On("GetNode", mock.Anything, mock.Anything).Return(nil, err).Once()
c.wal.Recover(context.TODO())
Expand Down Expand Up @@ -133,7 +133,7 @@ func TestHandleCreateWorkloadHandled(t *testing.T) {
}

store := c.store.(*storemocks.Store)
err = types.NewDetailedErr(types.ErrBadCount, fmt.Sprintf("keys: [%s]", wrkid))
err = types.NewDetailedErr(types.ErrInvaildCount, fmt.Sprintf("keys: [%s]", wrkid))
store.On("GetWorkload", mock.Anything, wrkid).Return(nil, err).Once()
store.On("GetNode", mock.Anything, wrk.Nodename).Return(node, nil)

Expand Down
2 changes: 1 addition & 1 deletion discovery/helium/helium.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"sync"
"time"

"github.com/pkg/errors"
"github.com/cockroachdb/errors"
"github.com/projecteru2/core/log"
"github.com/projecteru2/core/store"
"github.com/projecteru2/core/types"
Expand Down
6 changes: 3 additions & 3 deletions engine/docker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (e *Engine) VirtualizationCreate(ctx context.Context, opts *enginetypes.Vir

// memory should more than 4MiB
if opts.Memory > 0 && opts.Memory < minMemory || opts.Memory < 0 {
return r, coretypes.ErrBadMemory
return r, coretypes.ErrInvaildMemory
}
// set default log driver if lambda
if opts.Lambda {
Expand Down Expand Up @@ -408,11 +408,11 @@ func (e *Engine) VirtualizationUpdateResource(ctx context.Context, ID string, op
}

if resourceOpts.Memory > 0 && resourceOpts.Memory < minMemory || resourceOpts.Memory < 0 {
return coretypes.ErrBadMemory
return coretypes.ErrInvaildMemory
}
if len(opts.Volumes) > 0 || resourceOpts.VolumeChanged {
log.Errorf(ctx, err, "[VirtualizationUpdateResource] docker engine not support rebinding volume resource: %+v", resourceOpts.Volumes)
return coretypes.ErrNotSupport
return coretypes.ErrInvaildWorkloadOps
}

memory := resourceOpts.Memory
Expand Down
2 changes: 1 addition & 1 deletion engine/docker/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (e *Engine) makeIPV4EndpointSetting(ipv4 string) (*dockernetwork.EndpointSe
if ipv4 != "" {
ip := net.ParseIP(ipv4)
if ip == nil {
return nil, coretypes.NewDetailedErr(coretypes.ErrBadIPAddress, ipv4)
return nil, coretypes.NewDetailedErr(coretypes.ErrInvaildIPAddress, ipv4)
}
config.IPAMConfig.IPv4Address = ip.String()
}
Expand Down
4 changes: 2 additions & 2 deletions engine/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func getEnginePrefix(endpoint string) (string, error) {
return prefix, nil
}
}
return "", types.NewDetailedErr(types.ErrNodeFormat, fmt.Sprintf("endpoint invalid %+v", endpoint))
return "", types.NewDetailedErr(types.ErrInvaildNodeEndpoint, fmt.Sprintf("endpoint invalid %+v", endpoint))
}

func getEngineCacheKey(endpoint, ca, cert, key string) string {
Expand All @@ -207,7 +207,7 @@ func newEngine(ctx context.Context, config types.Config, nodename, endpoint, ca,
}
e, ok := engines[prefix]
if !ok {
return nil, types.ErrNotSupport
return nil, types.ErrNotSupportEndpoint
}
utils.WithTimeout(ctx, config.ConnectionTimeout, func(ctx context.Context) {
client, err = e(ctx, config, nodename, endpoint, ca, cert, key)
Expand Down
4 changes: 2 additions & 2 deletions engine/virt/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strconv"
"strings"

"github.com/pkg/errors"
"github.com/cockroachdb/errors"
coretypes "github.com/projecteru2/core/types"
)

Expand All @@ -19,7 +19,7 @@ func (v *Virt) parseVolumes(volumes []string) ([]string, error) {
for _, bind := range volumes {
parts := strings.Split(bind, ":")
if len(parts) != 4 && len(parts) != 8 {
return nil, coretypes.NewDetailedErr(coretypes.ErrInvalidBind, bind)
return nil, coretypes.NewDetailedErr(coretypes.ErrInvalidVolumeBind, bind)
}

src := parts[0]
Expand Down
4 changes: 2 additions & 2 deletions engine/virt/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (v *Virt) ImageBuildFromExist(ctx context.Context, ID string, refs []string
return "", types.ErrNoImageUser
}
if len(refs) != 1 {
return "", types.ErrBadRefs
return "", types.ErrInvaildRefs
}

_, imgName, err := splitUserImage(refs[0])
Expand Down Expand Up @@ -154,7 +154,7 @@ func (v *Virt) ImageRemoteDigest(ctx context.Context, image string) (string, err
case err != nil:
return "", err
case len(digests) < 1:
return "", types.ErrNoRemoteDigest
return "", types.ErrInvaildRemoteDigest
default:
return digests[0], nil
}
Expand Down
2 changes: 1 addition & 1 deletion engine/virt/virt.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func MakeClient(ctx context.Context, config coretypes.Config, nodename, endpoint
case strings.HasPrefix(endpoint, GRPCPrefixKey):
uri = "grpc://" + strings.TrimPrefix(endpoint, GRPCPrefixKey)
default:
return nil, coretypes.ErrNotSupport
return nil, coretypes.ErrNotSupportEndpoint
}

cli, err := virtapi.New(uri)
Expand Down
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ require (
github.com/CMGS/statsd v0.0.0-20160223095033-48c421b3c1ab
github.com/alicebob/miniredis/v2 v2.14.3
github.com/cenkalti/backoff/v4 v4.1.1
github.com/cockroachdb/errors v1.9.0
github.com/cornelk/hashmap v1.0.9-0.20221102000129-36b3b9c2b7ec
github.com/docker/distribution v2.8.0+incompatible
github.com/docker/docker v20.10.0+incompatible
github.com/docker/go-connections v0.4.0
github.com/docker/go-units v0.4.0
github.com/getsentry/sentry-go v0.9.0
github.com/getsentry/sentry-go v0.12.0
github.com/go-git/go-git/v5 v5.2.0
github.com/go-ping/ping v0.0.0-20210407214646-e4e642a95741
github.com/go-redis/redis/v8 v8.8.2
Expand Down Expand Up @@ -53,6 +54,8 @@ require (
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20211118104740-dabe8e521a4f // indirect
github.com/cockroachdb/redact v1.1.3 // indirect
github.com/containerd/cgroups v1.0.3 // indirect
github.com/containerd/containerd v1.5.13 // indirect
github.com/containerd/continuity v0.1.0 // indirect
Expand Down Expand Up @@ -84,6 +87,8 @@ require (
github.com/json-iterator/go v1.1.11 // indirect
github.com/juju/errors v0.0.0-20220331221717-b38fca44723b // indirect
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
Expand All @@ -100,6 +105,7 @@ require (
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
Expand Down Expand Up @@ -136,7 +142,7 @@ require (
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
golang.org/x/tools v0.1.8-0.20211029000441-d6a9af8af023 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
google.golang.org/genproto v0.0.0-20210624195500-8bfb893ecb84 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
Loading

0 comments on commit a8b2981

Please sign in to comment.