Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 6890c50

Browse files
authored
Merge pull request #1142 from jmank88/vcs_err_fix
gps: fix unwrapVcsErr to handle nil causes
2 parents 9db233a + 74c0818 commit 6890c50

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

internal/gps/source_errors.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,23 @@ import (
1313
// preserving the actual vcs command output and error, in addition to the message.
1414
// All other types pass through unchanged.
1515
func unwrapVcsErr(err error) error {
16+
var cause error
17+
var out, msg string
18+
1619
switch t := err.(type) {
1720
case *vcs.LocalError:
18-
cause, out, msg := t.Original(), t.Out(), t.Error()
19-
return errors.Wrap(errors.Wrap(cause, out), msg)
21+
cause, out, msg = t.Original(), t.Out(), t.Error()
2022
case *vcs.RemoteError:
21-
cause, out, msg := t.Original(), t.Out(), t.Error()
22-
return errors.Wrap(errors.Wrap(cause, out), msg)
23+
cause, out, msg = t.Original(), t.Out(), t.Error()
24+
2325
default:
2426
return err
2527
}
28+
29+
if cause == nil {
30+
cause = errors.New(out)
31+
} else {
32+
cause = errors.Wrap(cause, out)
33+
}
34+
return errors.Wrap(cause, msg)
2635
}

internal/gps/source_errors_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package gps
6+
7+
import (
8+
"testing"
9+
10+
"github.com/Masterminds/vcs"
11+
)
12+
13+
func TestUnwrapVcsErrNonNil(t *testing.T) {
14+
for _, err := range []error{
15+
vcs.NewRemoteError("msg", nil, "out"),
16+
vcs.NewRemoteError("msg", nil, ""),
17+
vcs.NewRemoteError("", nil, "out"),
18+
vcs.NewRemoteError("", nil, ""),
19+
vcs.NewLocalError("msg", nil, "out"),
20+
vcs.NewLocalError("msg", nil, ""),
21+
vcs.NewLocalError("", nil, "out"),
22+
vcs.NewLocalError("", nil, ""),
23+
&vcs.RemoteError{},
24+
&vcs.LocalError{},
25+
} {
26+
if unwrapVcsErr(err) == nil {
27+
t.Errorf("unexpected nil error unwrapping: %#v", err)
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)