Skip to content

Commit 1b70534

Browse files
committed
Commonize the errors across VCS systems
- Error types depend if the action is local or remote - Same error messages from differing VCS - Underlying error and output are available
1 parent d677873 commit 1b70534

File tree

6 files changed

+129
-75
lines changed

6 files changed

+129
-75
lines changed

bzr.go

+21-15
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func NewBzrRepo(remote, local string) (*BzrRepo, error) {
4040
c.Env = envForDir(c.Dir)
4141
out, err := c.CombinedOutput()
4242
if err != nil {
43-
return nil, err
43+
return nil, NewLocalError("Unable to retrieve local repo information", err, string(out))
4444
}
4545
m := bzrDetectURL.FindStringSubmatch(string(out))
4646

@@ -71,40 +71,46 @@ func (s *BzrRepo) Get() error {
7171
if _, err := os.Stat(basePath); os.IsNotExist(err) {
7272
err = os.MkdirAll(basePath, 0755)
7373
if err != nil {
74-
return NewGetError(err, "")
74+
return NewLocalError("Unable to create directory", err, "")
7575
}
7676
}
7777

7878
out, err := s.run("bzr", "branch", s.Remote(), s.LocalPath())
7979
if err != nil {
80-
return NewGetError(err, string(out))
80+
return NewRemoteError("Unable to get repository", err, string(out))
8181
}
8282

83-
return err
83+
return nil
8484
}
8585

8686
// Update performs a Bzr pull and update to an existing checkout.
8787
func (s *BzrRepo) Update() error {
88-
_, err := s.RunFromDir("bzr", "pull")
88+
out, err := s.RunFromDir("bzr", "pull")
8989
if err != nil {
90-
return err
90+
return NewRemoteError("Unable to update repository", err, string(out))
9191
}
92-
_, err = s.RunFromDir("bzr", "update")
93-
return err
92+
out, err = s.RunFromDir("bzr", "update")
93+
if err != nil {
94+
return NewRemoteError("Unable to update repository", err, string(out))
95+
}
96+
return nil
9497
}
9598

9699
// UpdateVersion sets the version of a package currently checked out via Bzr.
97100
func (s *BzrRepo) UpdateVersion(version string) error {
98-
_, err := s.RunFromDir("bzr", "update", "-r", version)
99-
return err
101+
out, err := s.RunFromDir("bzr", "update", "-r", version)
102+
if err != nil {
103+
return NewLocalError("Unable to update checked out version", err, string(out))
104+
}
105+
return nil
100106
}
101107

102108
// Version retrieves the current version.
103109
func (s *BzrRepo) Version() (string, error) {
104110

105111
out, err := s.RunFromDir("bzr", "revno", "--tree")
106112
if err != nil {
107-
return "", err
113+
return "", NewLocalError("Unable to retrieve checked out version", err, string(out))
108114
}
109115

110116
return strings.TrimSpace(string(out)), nil
@@ -114,11 +120,11 @@ func (s *BzrRepo) Version() (string, error) {
114120
func (s *BzrRepo) Date() (time.Time, error) {
115121
out, err := s.RunFromDir("bzr", "version-info", "--custom", "--template={date}")
116122
if err != nil {
117-
return time.Time{}, err
123+
return time.Time{}, NewLocalError("Unable to retrieve revision date", err, string(out))
118124
}
119125
t, err := time.Parse(longForm, string(out))
120126
if err != nil {
121-
return time.Time{}, err
127+
return time.Time{}, NewLocalError("Unable to retrieve revision date", err, string(out))
122128
}
123129
return t, nil
124130
}
@@ -145,7 +151,7 @@ func (s *BzrRepo) Branches() ([]string, error) {
145151
func (s *BzrRepo) Tags() ([]string, error) {
146152
out, err := s.RunFromDir("bzr", "tags")
147153
if err != nil {
148-
return []string{}, err
154+
return []string{}, NewLocalError("Unable to retrieve tags", err, string(out))
149155
}
150156
tags := s.referenceList(string(out), `(?m-s)^(\S+)`)
151157
return tags, nil
@@ -193,7 +199,7 @@ func (s *BzrRepo) CommitInfo(id string) (*CommitInfo, error) {
193199
ts := strings.TrimSpace(strings.TrimPrefix(l, "timestamp:"))
194200
ci.Date, err = time.Parse(format, ts)
195201
if err != nil {
196-
return nil, err
202+
return nil, NewLocalError("Unable to retrieve commit information", err, string(out))
197203
}
198204
} else if strings.TrimSpace(l) == "message:" {
199205
track = i

errors.go

+21-6
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,30 @@ var (
5555
ErrRevisionUnavailable = errors.New("Revision unavailable")
5656
)
5757

58-
// GetError is returned when unable to retrieve a repository
59-
type GetError struct {
58+
// RemoteError is returned when an operation fails against a remote repo
59+
type RemoteError struct {
6060
vcsError
6161
}
6262

63-
// NewGetError constructs a GetError
64-
func NewGetError(err error, out string) error {
65-
e := &GetError{}
66-
e.s = "Unable to get repository"
63+
// NewRemoteError constructs a RemoteError
64+
func NewRemoteError(msg string, err error, out string) error {
65+
e := &RemoteError{}
66+
e.s = msg
67+
e.e = err
68+
e.o = out
69+
70+
return e
71+
}
72+
73+
// LocalError is returned when a local operation has an error
74+
type LocalError struct {
75+
vcsError
76+
}
77+
78+
// NewLocalError constructs a LocalError
79+
func NewLocalError(msg string, err error, out string) error {
80+
e := &LocalError{}
81+
e.s = msg
6782
e.e = err
6883
e.o = out
6984

errors_test.go

+20-4
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,32 @@ import (
55
"testing"
66
)
77

8-
func TestNewGetError(t *testing.T) {
8+
func TestNewRemoteError(t *testing.T) {
99
base := errors.New("Foo error")
1010
out := "This is a test"
11+
msg := "remote error msg"
1112

12-
e := NewGetError(base, out)
13+
e := NewRemoteError(msg, base, out)
1314

1415
switch e.(type) {
15-
case *GetError:
16+
case *RemoteError:
1617
// This is the right error type
1718
default:
18-
t.Error("Wrong error type returned from NewGetError")
19+
t.Error("Wrong error type returned from NewRemoteError")
20+
}
21+
}
22+
23+
func TestNewLocalError(t *testing.T) {
24+
base := errors.New("Foo error")
25+
out := "This is a test"
26+
msg := "local error msg"
27+
28+
e := NewLocalError(msg, base, out)
29+
30+
switch e.(type) {
31+
case *LocalError:
32+
// This is the right error type
33+
default:
34+
t.Error("Wrong error type returned from NewLocalError")
1935
}
2036
}

git.go

+25-20
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func NewGitRepo(remote, local string) (*GitRepo, error) {
3333
c.Env = envForDir(c.Dir)
3434
out, err := c.CombinedOutput()
3535
if err != nil {
36-
return nil, err
36+
return nil, NewLocalError("Unable to retrieve local repo information", err, string(out))
3737
}
3838

3939
localRemote := strings.TrimSpace(string(out))
@@ -75,58 +75,63 @@ func (s *GitRepo) Get() error {
7575
if _, err := os.Stat(basePath); os.IsNotExist(err) {
7676
err = os.MkdirAll(basePath, 0755)
7777
if err != nil {
78-
return NewGetError(err, "")
78+
return NewLocalError("Unable to create directory", err, "")
7979
}
8080

8181
out, err = s.run("git", "clone", s.Remote(), s.LocalPath())
8282
if err != nil {
83-
return NewGetError(err, string(out))
83+
return NewRemoteError("Unable to get repository", err, string(out))
8484
}
8585
return err
8686
}
8787

8888
} else if err != nil {
89-
return NewGetError(err, string(out))
89+
return NewRemoteError("Unable to get repository", err, string(out))
9090
}
9191

92-
return err
92+
return nil
9393
}
9494

9595
// Update performs an Git fetch and pull to an existing checkout.
9696
func (s *GitRepo) Update() error {
9797
// Perform a fetch to make sure everything is up to date.
98-
_, err := s.RunFromDir("git", "fetch", s.RemoteLocation)
98+
out, err := s.RunFromDir("git", "fetch", s.RemoteLocation)
9999
if err != nil {
100-
return err
100+
return NewRemoteError("Unable to update repository", err, string(out))
101101
}
102102

103103
// When in a detached head state, such as when an individual commit is checked
104104
// out do not attempt a pull. It will cause an error.
105105
detached, err := isDetachedHead(s.LocalPath())
106-
107106
if err != nil {
108-
return err
107+
return NewLocalError("Unable to update repository", err, "")
109108
}
110109

111110
if detached == true {
112111
return nil
113112
}
114113

115-
_, err = s.RunFromDir("git", "pull")
116-
return err
114+
out, err = s.RunFromDir("git", "pull")
115+
if err != nil {
116+
return NewRemoteError("Unable to update repository", err, string(out))
117+
}
118+
return nil
117119
}
118120

119121
// UpdateVersion sets the version of a package currently checked out via Git.
120122
func (s *GitRepo) UpdateVersion(version string) error {
121-
_, err := s.RunFromDir("git", "checkout", version)
122-
return err
123+
out, err := s.RunFromDir("git", "checkout", version)
124+
if err != nil {
125+
return NewLocalError("Unable to update checked out version", err, string(out))
126+
}
127+
return nil
123128
}
124129

125130
// Version retrieves the current version.
126131
func (s *GitRepo) Version() (string, error) {
127132
out, err := s.RunFromDir("git", "rev-parse", "HEAD")
128133
if err != nil {
129-
return "", err
134+
return "", NewLocalError("Unable to retrieve checked out version", err, string(out))
130135
}
131136

132137
return strings.TrimSpace(string(out)), nil
@@ -136,11 +141,11 @@ func (s *GitRepo) Version() (string, error) {
136141
func (s *GitRepo) Date() (time.Time, error) {
137142
out, err := s.RunFromDir("git", "log", "-1", "--date=iso", "--pretty=format:%cd")
138143
if err != nil {
139-
return time.Time{}, err
144+
return time.Time{}, NewLocalError("Unable to retrieve revision date", err, string(out))
140145
}
141146
t, err := time.Parse(longForm, string(out))
142147
if err != nil {
143-
return time.Time{}, err
148+
return time.Time{}, NewLocalError("Unable to retrieve revision date", err, string(out))
144149
}
145150
return t, nil
146151
}
@@ -149,7 +154,7 @@ func (s *GitRepo) Date() (time.Time, error) {
149154
func (s *GitRepo) Branches() ([]string, error) {
150155
out, err := s.RunFromDir("git", "show-ref")
151156
if err != nil {
152-
return []string{}, err
157+
return []string{}, NewLocalError("Unable to retrieve branches", err, string(out))
153158
}
154159
branches := s.referenceList(string(out), `(?m-s)(?:`+s.RemoteLocation+`)/(\S+)$`)
155160
return branches, nil
@@ -159,7 +164,7 @@ func (s *GitRepo) Branches() ([]string, error) {
159164
func (s *GitRepo) Tags() ([]string, error) {
160165
out, err := s.RunFromDir("git", "show-ref")
161166
if err != nil {
162-
return []string{}, err
167+
return []string{}, NewLocalError("Unable to retrieve tags", err, string(out))
163168
}
164169
tags := s.referenceList(string(out), `(?m-s)(?:tags)/(\S+)$`)
165170
return tags, nil
@@ -216,12 +221,12 @@ func (s *GitRepo) CommitInfo(id string) (*CommitInfo, error) {
216221
}{}
217222
err = xml.Unmarshal(out, &cis)
218223
if err != nil {
219-
return nil, err
224+
return nil, NewLocalError("Unable to retrieve commit information", err, string(out))
220225
}
221226

222227
t, err := time.Parse("Mon, _2 Jan 2006 15:04:05 -0700", cis.Date)
223228
if err != nil {
224-
return nil, err
229+
return nil, NewLocalError("Unable to retrieve commit information", err, string(out))
225230
}
226231

227232
ci := &CommitInfo{

0 commit comments

Comments
 (0)