Skip to content

Commit a6bd9b4

Browse files
committed
error: simplify errors
1 parent 6311233 commit a6bd9b4

File tree

11 files changed

+32
-62
lines changed

11 files changed

+32
-62
lines changed

command.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ func (w *limitDualWriter) Write(p []byte) (int, error) {
8484
// RunInDirPipelineWithTimeout executes the command in given directory and timeout duration.
8585
// It pipes stdout and stderr to supplied io.Writer. DefaultTimeout will be used if the timeout
8686
// duration is less than time.Nanosecond (i.e. less than or equal to 0).
87-
func (c *Command) RunInDirPipelineWithTimeout(timeout time.Duration, stdout, stderr io.Writer, dir string) error {
87+
// It returns an ErrExecTimeout if the execution was timed out.
88+
func (c *Command) RunInDirPipelineWithTimeout(timeout time.Duration, stdout, stderr io.Writer, dir string) (err error) {
8889
if timeout < time.Nanosecond {
8990
timeout = DefaultTimeout
9091
}
@@ -109,7 +110,12 @@ func (c *Command) RunInDirPipelineWithTimeout(timeout time.Duration, stdout, std
109110
}()
110111

111112
ctx, cancel := context.WithTimeout(context.Background(), timeout)
112-
defer cancel()
113+
defer func() {
114+
cancel()
115+
if err == context.DeadlineExceeded {
116+
err = ErrExecTimeout
117+
}
118+
}()
113119

114120
cmd := exec.CommandContext(ctx, c.name, c.args...)
115121
if c.envs != nil {
@@ -118,7 +124,7 @@ func (c *Command) RunInDirPipelineWithTimeout(timeout time.Duration, stdout, std
118124
cmd.Dir = dir
119125
cmd.Stdout = w
120126
cmd.Stderr = stderr
121-
if err := cmd.Start(); err != nil {
127+
if err = cmd.Start(); err != nil {
122128
return err
123129
}
124130

@@ -136,8 +142,8 @@ func (c *Command) RunInDirPipelineWithTimeout(timeout time.Duration, stdout, std
136142
}
137143

138144
<-result
139-
return ErrExecTimeout{timeout}
140-
case err := <-result:
145+
return ErrExecTimeout
146+
case err = <-result:
141147
return err
142148
}
143149
}

command_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package git
22

33
import (
4-
"context"
54
"testing"
65
"time"
76

@@ -58,5 +57,5 @@ func TestCommand_AddEnvs(t *testing.T) {
5857

5958
func TestCommand_RunWithTimeout(t *testing.T) {
6059
_, err := NewCommand("version").RunWithTimeout(time.Nanosecond)
61-
assert.Equal(t, context.DeadlineExceeded, err)
60+
assert.Equal(t, ErrExecTimeout, err)
6261
}

commit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ func (c *Commit) ParentsCount() int {
6060
}
6161

6262
// ParentID returns the SHA-1 hash of the n-th parent (0-based) of this commit.
63-
// It returns ErrRevisionNotExist if no such parent exists.
63+
// It returns an ErrParentNotExist if no such parent exists.
6464
func (c *Commit) ParentID(n int) (*SHA1, error) {
6565
if n >= len(c.parents) {
66-
return nil, ErrRevisionNotExist{"", ""}
66+
return nil, ErrParentNotExist
6767
}
6868
return c.parents[n], nil
6969
}

commit_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ func TestCommit_Parent(t *testing.T) {
5858
t.Run("Parent", func(t *testing.T) {
5959
t.Run("no such parent", func(t *testing.T) {
6060
_, err := c.Parent(c.ParentsCount() + 1)
61-
assert.NotNil(t, err)
62-
assert.Equal(t, `revision does not exist [rev: , path: ]`, err.Error())
61+
assert.Equal(t, ErrParentNotExist, err)
6362
})
6463

6564
tests := []struct {

error.go

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,12 @@ package git
66

77
import (
88
"errors"
9-
"fmt"
10-
"time"
119
)
1210

13-
var ErrSubmoduleNotExist = errors.New("submodule does not exist")
14-
15-
type ErrExecTimeout struct {
16-
Duration time.Duration
17-
}
18-
19-
func IsErrExecTimeout(err error) bool {
20-
_, ok := err.(ErrExecTimeout)
21-
return ok
22-
}
23-
24-
func (err ErrExecTimeout) Error() string {
25-
return fmt.Sprintf("execution timed out [duration: %v]", err.Duration)
26-
}
27-
28-
type ErrRevisionNotExist struct {
29-
Rev string
30-
Path string
31-
}
32-
33-
func IsErrRevesionNotExist(err error) bool {
34-
_, ok := err.(ErrRevisionNotExist)
35-
return ok
36-
}
37-
38-
func (err ErrRevisionNotExist) Error() string {
39-
return fmt.Sprintf("revision does not exist [rev: %s, path: %s]", err.Rev, err.Path)
40-
}
41-
42-
type ErrNoMergeBase struct{}
43-
44-
func IsErrNoMergeBase(err error) bool {
45-
_, ok := err.(ErrNoMergeBase)
46-
return ok
47-
}
48-
49-
func (err ErrNoMergeBase) Error() string {
50-
return "no merge based found"
51-
}
11+
var (
12+
ErrParentNotExist = errors.New("parent does not exist")
13+
ErrSubmoduleNotExist = errors.New("submodule does not exist")
14+
ErrRevisionNotExist = errors.New("revision does not exist")
15+
ErrExecTimeout = errors.New("execution was timed out")
16+
ErrNoMergeBase = errors.New("no merge based was found")
17+
)

repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ func (r *Repository) RevParse(rev string, opts ...RevParseOptions) (string, erro
430430
commitID, err := NewCommand("rev-parse", rev).RunInDirWithTimeout(opt.Timeout, r.path)
431431
if err != nil {
432432
if strings.Contains(err.Error(), "exit status 128") {
433-
return "", ErrRevisionNotExist{rev, ""}
433+
return "", ErrRevisionNotExist
434434
}
435435
return "", err
436436
}

repo_commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func (r *Repository) CommitByRevision(rev string, opts ...CommitByRevisionOption
196196
if err != nil {
197197
return nil, err
198198
} else if len(commits) == 0 {
199-
return nil, ErrRevisionNotExist{rev, opt.Path}
199+
return nil, ErrRevisionNotExist
200200
}
201201
return commits[0], nil
202202
}

repo_pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (r *Repository) MergeBase(base, head string, opts ...MergeBaseOptions) (str
2626
stdout, err := NewCommand("merge-base", base, head).RunInDirWithTimeout(opt.Timeout, r.path)
2727
if err != nil {
2828
if strings.Contains(err.Error(), "exit status 1") {
29-
return "", ErrNoMergeBase{}
29+
return "", ErrNoMergeBase
3030
}
3131
return "", err
3232
}

repo_tree.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (r *Repository) LsTree(rev string, opts ...LsTreeOptions) (*Tree, error) {
110110

111111
stdout, err := NewCommand("ls-tree", rev).RunInDirWithTimeout(opt.Timeout, r.path)
112112
if err != nil {
113-
return nil, ErrRevisionNotExist{rev, ""}
113+
return nil, ErrRevisionNotExist
114114
}
115115

116116
t.entries, err = parseTree(t, stdout)

tree_blob.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (t *Tree) TreeEntry(subpath string, opts ...LsTreeOptions) (*TreeEntry, err
4242
}
4343
}
4444
}
45-
return nil, ErrRevisionNotExist{"", subpath}
45+
return nil, ErrRevisionNotExist
4646
}
4747

4848
// Blob returns the blob object by given subpath of the tree.
@@ -56,5 +56,5 @@ func (t *Tree) Blob(subpath string, opts ...LsTreeOptions) (*Blob, error) {
5656
return e.Blob(), nil
5757
}
5858

59-
return nil, ErrRevisionNotExist{"", subpath}
59+
return nil, ErrRevisionNotExist
6060
}

tree_entry.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func (es Entries) CommitsInfoWithCustomConcurrency(timeout time.Duration, commit
204204
Timeout: timeout,
205205
})
206206
if err != nil {
207-
cinfo.err = fmt.Errorf("CommitByPath (%s/%s): %v", treePath, es[i].Name(), err)
207+
cinfo.err = fmt.Errorf("get commit by path (%s/%s): %v", treePath, es[i].Name(), err)
208208
} else {
209209
cinfo.infos = []interface{}{es[i], c}
210210
}
@@ -218,8 +218,8 @@ func (es Entries) CommitsInfoWithCustomConcurrency(timeout time.Duration, commit
218218
go func(i int) {
219219
cinfo := commitInfo{entryName: es[i].Name()}
220220
sm, err := commit.Submodule(path.Join(treePath, es[i].Name()))
221-
if err != nil && !IsErrRevesionNotExist(err) {
222-
cinfo.err = fmt.Errorf("GetSubModule (%s/%s): %v", treePath, es[i].Name(), err)
221+
if err != nil && err != ErrSubmoduleNotExist {
222+
cinfo.err = fmt.Errorf("get submodule (%s/%s): %v", treePath, es[i].Name(), err)
223223
revChan <- cinfo
224224
return
225225
}
@@ -234,7 +234,7 @@ func (es Entries) CommitsInfoWithCustomConcurrency(timeout time.Duration, commit
234234
Timeout: timeout,
235235
})
236236
if err != nil {
237-
cinfo.err = fmt.Errorf("CommitByPath (%s/%s): %v", treePath, es[i].Name(), err)
237+
cinfo.err = fmt.Errorf("get commit by path (%s/%s): %v", treePath, es[i].Name(), err)
238238
} else {
239239
cinfo.infos = []interface{}{
240240
es[i],

0 commit comments

Comments
 (0)