Skip to content

Commit 5399d07

Browse files
wider coverage of error stack (#358)
1 parent da7a7e3 commit 5399d07

File tree

7 files changed

+39
-27
lines changed

7 files changed

+39
-27
lines changed

lock/etcdlock/mutex.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"sync"
77
"time"
88

9+
"github.com/pkg/errors"
910
"github.com/projecteru2/core/types"
1011
"go.etcd.io/etcd/v3/clientv3"
1112
"go.etcd.io/etcd/v3/clientv3/concurrency"
@@ -37,13 +38,13 @@ func (c *lockContext) Err() error {
3738
if c.err != nil {
3839
return c.err
3940
}
40-
return c.Context.Err()
41+
return errors.WithStack(c.Context.Err())
4142
}
4243

4344
// New new a lock
4445
func New(cli *clientv3.Client, key string, ttl time.Duration) (*Mutex, error) {
4546
if key == "" {
46-
return nil, types.ErrKeyIsEmpty
47+
return nil, errors.WithStack(types.ErrKeyIsEmpty)
4748
}
4849

4950
if !strings.HasPrefix(key, "/") {

types/node.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"math"
77

8+
"github.com/pkg/errors"
89
engine "github.com/projecteru2/core/engine"
910
enginetypes "github.com/projecteru2/core/engine/types"
1011
)
@@ -66,9 +67,10 @@ func (n *Node) Init() {
6667
// Info show node info
6768
func (n *Node) Info(ctx context.Context) (*enginetypes.Info, error) {
6869
if n.Engine == nil {
69-
return nil, ErrNilEngine
70+
return nil, errors.WithStack(ErrNilEngine)
7071
}
71-
return n.Engine.Info(ctx)
72+
info, err := n.Engine.Info(ctx)
73+
return info, errors.WithStack(err)
7274
}
7375

7476
// SetCPUUsed set cpuusage

types/resource.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package types
33
import (
44
"encoding/json"
55
"sort"
6+
7+
"github.com/pkg/errors"
68
)
79

810
// ResourceOptions for create/realloc/replace
@@ -163,12 +165,12 @@ func (p *VolumePlan) UnmarshalJSON(b []byte) (err error) {
163165
}
164166
plan := map[string]VolumeMap{}
165167
if err = json.Unmarshal(b, &plan); err != nil {
166-
return err
168+
return errors.WithStack(err)
167169
}
168170
for volume, vmap := range plan {
169171
vb, err := NewVolumeBinding(volume)
170172
if err != nil {
171-
return err
173+
return errors.WithStack(err)
172174
}
173175
(*p)[*vb] = vmap
174176
}
@@ -181,7 +183,8 @@ func (p VolumePlan) MarshalJSON() ([]byte, error) {
181183
for vb, vmap := range p {
182184
plan[vb.ToString(false)] = vmap
183185
}
184-
return json.Marshal(plan)
186+
bs, err := json.Marshal(plan)
187+
return bs, errors.WithStack(err)
185188
}
186189

187190
// ToLiteral returns literal VolumePlan

types/stream.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"io"
66
"io/ioutil"
77
"sync"
8+
9+
"github.com/pkg/errors"
810
)
911

1012
// ReaderManager return Reader under concurrency
@@ -17,7 +19,7 @@ func NewReaderManager(r io.Reader) (ReaderManager, error) {
1719
bs, err := ioutil.ReadAll(r)
1820
return &readerManager{
1921
r: bytes.NewReader(bs),
20-
}, err
22+
}, errors.WithStack(err)
2123
}
2224

2325
type readerManager struct {
@@ -30,8 +32,8 @@ func (rm *readerManager) GetReader() (_ io.Reader, err error) {
3032
defer rm.mux.Unlock()
3133
buf := &bytes.Buffer{}
3234
if _, err = io.Copy(buf, rm.r); err != nil {
33-
return
35+
return nil, errors.WithStack(err)
3436
}
3537
_, err = rm.r.Seek(0, io.SeekStart)
36-
return buf, err
38+
return buf, errors.WithStack(err)
3739
}

types/volume.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ func NewVolumeBinding(volume string) (_ *VolumeBinding, err error) {
3535
case 4:
3636
src, dst, flags = parts[0], parts[1], parts[2]
3737
if size, err = strconv.ParseInt(parts[3], 10, 64); err != nil {
38-
return nil, err
38+
return nil, errors.WithStack(err)
3939
}
4040
default:
41-
return nil, fmt.Errorf("invalid volume: %v", volume)
41+
return nil, errors.WithStack(fmt.Errorf("invalid volume: %v", volume))
4242
}
4343

4444
flagParts := strings.Split(flags, "")
@@ -56,10 +56,10 @@ func NewVolumeBinding(volume string) (_ *VolumeBinding, err error) {
5656
// Validate return error if invalid
5757
func (vb VolumeBinding) Validate() error {
5858
if vb.Destination == "" {
59-
return errors.Errorf("invalid volume, dest must be provided: %v", vb)
59+
return errors.WithStack(errors.Errorf("invalid volume, dest must be provided: %v", vb))
6060
}
6161
if vb.RequireScheduleMonopoly() && vb.RequireScheduleUnlimitedQuota() {
62-
return errors.Errorf("invalid volume, monopoly volume must not be limited: %v", vb)
62+
return errors.WithStack(errors.Errorf("invalid volume, monopoly volume must not be limited: %v", vb))
6363
}
6464
return nil
6565
}
@@ -131,7 +131,7 @@ func (vbs VolumeBindings) ToStringSlice(sorted, normalize bool) (volumes []strin
131131
func (vbs *VolumeBindings) UnmarshalJSON(b []byte) (err error) {
132132
volumes := []string{}
133133
if err = json.Unmarshal(b, &volumes); err != nil {
134-
return err
134+
return errors.WithStack(err)
135135
}
136136
*vbs, err = NewVolumeBindings(volumes)
137137
return
@@ -143,7 +143,8 @@ func (vbs VolumeBindings) MarshalJSON() ([]byte, error) {
143143
for _, vb := range vbs {
144144
volumes = append(volumes, vb.ToString(false))
145145
}
146-
return json.Marshal(volumes)
146+
bs, err := json.Marshal(volumes)
147+
return bs, errors.WithStack(err)
147148
}
148149

149150
// ApplyPlan creates new VolumeBindings according to volume plan

types/workload.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"time"
66

7+
"github.com/pkg/errors"
78
engine "github.com/projecteru2/core/engine"
89
enginetypes "github.com/projecteru2/core/engine/types"
910
)
@@ -47,37 +48,38 @@ type Workload struct {
4748
// Inspect a workload
4849
func (c *Workload) Inspect(ctx context.Context) (*enginetypes.VirtualizationInfo, error) {
4950
if c.Engine == nil {
50-
return nil, ErrNilEngine
51+
return nil, errors.WithStack(ErrNilEngine)
5152
}
52-
return c.Engine.VirtualizationInspect(ctx, c.ID)
53+
info, err := c.Engine.VirtualizationInspect(ctx, c.ID)
54+
return info, errors.WithStack(err)
5355
}
5456

5557
// Start a workload
5658
func (c *Workload) Start(ctx context.Context) error {
5759
if c.Engine == nil {
58-
return ErrNilEngine
60+
return errors.WithStack(ErrNilEngine)
5961
}
60-
return c.Engine.VirtualizationStart(ctx, c.ID)
62+
return errors.WithStack(c.Engine.VirtualizationStart(ctx, c.ID))
6163
}
6264

6365
// Stop a workload
6466
func (c *Workload) Stop(ctx context.Context, force bool) error {
6567
if c.Engine == nil {
66-
return ErrNilEngine
68+
return errors.WithStack(ErrNilEngine)
6769
}
6870
gracefulTimeout := time.Duration(-1) // -1 indicates use engine default timeout
6971
if force {
7072
gracefulTimeout = 0 // don't wait, kill -15 && kill -9
7173
}
72-
return c.Engine.VirtualizationStop(ctx, c.ID, gracefulTimeout)
74+
return errors.WithStack(c.Engine.VirtualizationStop(ctx, c.ID, gracefulTimeout))
7375
}
7476

7577
// Remove a workload
7678
func (c *Workload) Remove(ctx context.Context, force bool) error {
7779
if c.Engine == nil {
7880
return ErrNilEngine
7981
}
80-
return c.Engine.VirtualizationRemove(ctx, c.ID, true, force)
82+
return errors.WithStack(c.Engine.VirtualizationRemove(ctx, c.ID, true, force))
8183
}
8284

8385
// WorkloadStatus store deploy status

utils/utils.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212
"strings"
1313

14+
"github.com/pkg/errors"
1415
"github.com/projecteru2/core/cluster"
1516
"github.com/projecteru2/core/log"
1617
"github.com/projecteru2/core/types"
@@ -45,7 +46,7 @@ func Tail(path string) string {
4546
// GetGitRepoName return git repo name
4647
func GetGitRepoName(url string) (string, error) {
4748
if !(strings.Contains(url, "git@") || strings.Contains(url, "gitlab@") || strings.Contains(url, "https://")) || !strings.HasSuffix(url, ".git") {
48-
return "", types.NewDetailedErr(types.ErrInvalidGitURL, url)
49+
return "", errors.WithStack(types.NewDetailedErr(types.ErrInvalidGitURL, url))
4950
}
5051

5152
return strings.TrimSuffix(Tail(url), ".git"), nil
@@ -92,7 +93,7 @@ func ParseWorkloadName(workloadName string) (string, string, string, error) {
9293
if length >= 3 {
9394
return strings.Join(splits[0:length-2], "_"), splits[length-2], splits[length-1], nil
9495
}
95-
return "", "", "", types.NewDetailedErr(types.ErrInvalidWorkloadName, workloadName)
96+
return "", "", "", errors.WithStack(types.NewDetailedErr(types.ErrInvalidWorkloadName, workloadName))
9697
}
9798

9899
// MakePublishInfo generate publish info
@@ -178,13 +179,13 @@ func CleanStatsdMetrics(k string) string {
178179
func TempFile(stream io.ReadCloser) (string, error) {
179180
f, err := ioutil.TempFile(os.TempDir(), "")
180181
if err != nil {
181-
return "", err
182+
return "", errors.WithStack(err)
182183
}
183184
defer f.Close()
184185
defer stream.Close()
185186

186187
_, err = io.Copy(f, stream)
187-
return f.Name(), err
188+
return f.Name(), errors.WithStack(err)
188189
}
189190

190191
// Round for float64 to int

0 commit comments

Comments
 (0)