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

gps: fix unwrapVcsErr to handle nil causes #1142

Merged
merged 2 commits into from
Sep 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions internal/gps/source_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,23 @@ import (
// preserving the actual vcs command output and error, in addition to the message.
// All other types pass through unchanged.
func unwrapVcsErr(err error) error {
var cause error
var out, msg string

switch t := err.(type) {
case *vcs.LocalError:
cause, out, msg := t.Original(), t.Out(), t.Error()
return errors.Wrap(errors.Wrap(cause, out), msg)
cause, out, msg = t.Original(), t.Out(), t.Error()
case *vcs.RemoteError:
cause, out, msg := t.Original(), t.Out(), t.Error()
return errors.Wrap(errors.Wrap(cause, out), msg)
cause, out, msg = t.Original(), t.Out(), t.Error()

default:
return err
}

if cause == nil {
cause = errors.New(out)
} else {
cause = errors.Wrap(cause, out)
}
return errors.Wrap(cause, msg)
}
30 changes: 30 additions & 0 deletions internal/gps/source_errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package gps

import (
"testing"

"github.com/Masterminds/vcs"
)

func TestUnwrapVcsErrNonNil(t *testing.T) {
for _, err := range []error{
vcs.NewRemoteError("msg", nil, "out"),
vcs.NewRemoteError("msg", nil, ""),
vcs.NewRemoteError("", nil, "out"),
vcs.NewRemoteError("", nil, ""),
vcs.NewLocalError("msg", nil, "out"),
vcs.NewLocalError("msg", nil, ""),
vcs.NewLocalError("", nil, "out"),
vcs.NewLocalError("", nil, ""),
&vcs.RemoteError{},
&vcs.LocalError{},
} {
if unwrapVcsErr(err) == nil {
t.Errorf("unexpected nil error unwrapping: %#v", err)
}
}
}