File: errors.go

package info (click to toggle)
golang-github-masterminds-vcs-dev 1.12.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 216 kB
  • sloc: makefile: 37
file content (114 lines) | stat: -rw-r--r-- 3,125 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package vcs

import (
	"errors"
	"fmt"
)

// The vcs package provides ways to work with errors that hide the underlying
// implementation details but make them accessible if needed. For basic errors
// that do not have underlying implementation specific details or the underlying
// details are not necessary there are errors for comparison.
//
// For example:
//
//     ci, err := repo.CommitInfo("123")
//     if err == vcs.ErrRevisionUnavailable {
//         // The commit id was not available in the VCS.
//     }
//
// There are other times where getting the details are more useful. For example,
// if you're performing a repo.Get() and an error occurs. In general you'll want
// to consistently know it failed. But, you may want to know the underlying
// details (opt-in) to them. For those cases there is a different form of error
// handling.
//
// For example:
//
//     err := repo.Get()
//     if err != nil {
//         // A RemoteError was returned. This has access to the output of the
//         // vcs command, original error, and has a consistent cross vcs message.
//     }
//
// The errors returned here can be used in type switches to detect the underlying
// error. For example:
//
//     switch err.(type) {
//     case *vcs.RemoteError:
//         // This an error connecting to a remote system.
//     }
//
// For more information on using type switches to detect error types you can
// read the Go wiki at https://github.com/golang/go/wiki/Errors

var (
	// ErrWrongVCS is returned when an action is tried on the wrong VCS.
	ErrWrongVCS = errors.New("Wrong VCS detected")

	// ErrCannotDetectVCS is returned when VCS cannot be detected from URI string.
	ErrCannotDetectVCS = errors.New("Cannot detect VCS")

	// ErrWrongRemote occurs when the passed in remote does not match the VCS
	// configured endpoint.
	ErrWrongRemote = errors.New("The Remote does not match the VCS endpoint")

	// ErrRevisionUnavailable happens when commit revision information is
	// unavailable.
	ErrRevisionUnavailable = errors.New("Revision unavailable")
)

// RemoteError is returned when an operation fails against a remote repo
type RemoteError struct {
	vcsError
}

// NewRemoteError constructs a RemoteError
func NewRemoteError(msg string, err error, out string) error {
	e := &RemoteError{}
	e.s = msg
	e.e = err
	e.o = out

	return e
}

// LocalError is returned when a local operation has an error
type LocalError struct {
	vcsError
}

// NewLocalError constructs a LocalError
func NewLocalError(msg string, err error, out string) error {
	e := &LocalError{}
	e.s = msg
	e.e = err
	e.o = out

	return e
}

type vcsError struct {
	s string
	e error  // The original error
	o string // The output from executing the command
}

// Error implements the Error interface
func (e *vcsError) Error() string {
	if e.e == nil {
		return e.s
	}

	return fmt.Sprintf("%s: %v", e.s, e.e)
}

// Original retrieves the underlying implementation specific error.
func (e *vcsError) Original() error {
	return e.e
}

// Out retrieves the output of the original command that was run.
func (e *vcsError) Out() string {
	return e.o
}