-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
190 lines (164 loc) · 5.1 KB
/
errors.go
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2012-2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"fmt"
"strings"
"github.com/juju/errors"
"gopkg.in/juju/charm.v6"
"gopkg.in/mgo.v2/txn"
"github.com/juju/juju/core/network"
)
// ErrCharmAlreadyUploaded is returned by UpdateUploadedCharm() when
// the given charm is already uploaded and marked as not pending in
// state.
type ErrCharmAlreadyUploaded struct {
curl *charm.URL
}
func (e *ErrCharmAlreadyUploaded) Error() string {
return fmt.Sprintf("charm %q already uploaded", e.curl)
}
// IsCharmAlreadyUploadedError returns if the given error is
// ErrCharmAlreadyUploaded.
func IsCharmAlreadyUploadedError(err interface{}) bool {
if err == nil {
return false
}
// In case of a wrapped error, check the cause first.
value := err
cause := errors.Cause(err.(error))
if cause != nil {
value = cause
}
_, ok := value.(*ErrCharmAlreadyUploaded)
return ok
}
// ErrCharmRevisionAlreadyModified is returned when a pending or
// placeholder charm is no longer pending or a placeholder, signaling
// the charm is available in state with its full information.
var ErrCharmRevisionAlreadyModified = fmt.Errorf("charm revision already modified")
var ErrDead = fmt.Errorf("not found or dead")
type notAliveError struct {
entity string
}
func (e notAliveError) Error() string {
if e.entity == "" {
return "not found or not alive"
}
return fmt.Sprintf("%v is not found or not alive", e.entity)
}
// IsNotAlive returns true if err is cause by a not alive error.
func IsNotAlive(err error) bool {
_, ok := errors.Cause(err).(notAliveError)
return ok
}
var (
machineNotAliveErr = notAliveError{"machine"}
applicationNotAliveErr = notAliveError{"application"}
unitNotAliveErr = notAliveError{"unit"}
spaceNotAliveErr = notAliveError{"space"}
subnetNotAliveErr = notAliveError{"subnet"}
notAliveErr = notAliveError{""}
)
func onAbort(txnErr, err error) error {
if txnErr == txn.ErrAborted ||
errors.Cause(txnErr) == txn.ErrAborted {
return errors.Trace(err)
}
return errors.Trace(txnErr)
}
// ErrProviderIDNotUnique is a standard error to indicate the value specified
// for a ProviderID field is not unique within the current model.
type ErrProviderIDNotUnique struct {
duplicateIDs []string
}
func (e *ErrProviderIDNotUnique) Error() string {
idList := strings.Join(e.duplicateIDs, ", ")
return fmt.Sprintf("provider IDs not unique: %s", idList)
}
// NewProviderIDNotUniqueError returns an instance of ErrProviderIDNotUnique
// initialized with the given duplicate provider IDs.
func NewProviderIDNotUniqueError(providerIDs ...network.Id) error {
stringIDs := make([]string, len(providerIDs))
for i, providerID := range providerIDs {
stringIDs[i] = string(providerID)
}
return newProviderIDNotUniqueErrorFromStrings(stringIDs)
}
func newProviderIDNotUniqueErrorFromStrings(providerIDs []string) error {
return &ErrProviderIDNotUnique{
duplicateIDs: providerIDs,
}
}
// IsProviderIDNotUniqueError returns if the given error or its cause is
// ErrProviderIDNotUnique.
func IsProviderIDNotUniqueError(err interface{}) bool {
if err == nil {
return false
}
// In case of a wrapped error, check the cause first.
value := err
cause := errors.Cause(err.(error))
if cause != nil {
value = cause
}
_, ok := value.(*ErrProviderIDNotUnique)
return ok
}
// ErrParentDeviceHasChildren is a standard error to indicate a network
// link-layer device cannot be removed because other existing devices refer to
// it as their parent.
type ErrParentDeviceHasChildren struct {
parentName string
numChildren int
}
func (e *ErrParentDeviceHasChildren) Error() string {
return fmt.Sprintf("parent device %q has %d children", e.parentName, e.numChildren)
}
func newParentDeviceHasChildrenError(parentName string, numChildren int) error {
return &ErrParentDeviceHasChildren{
parentName: parentName,
numChildren: numChildren,
}
}
// IsParentDeviceHasChildrenError returns if the given error or its cause is
// ErrParentDeviceHasChildren.
func IsParentDeviceHasChildrenError(err interface{}) bool {
if err == nil {
return false
}
// In case of a wrapped error, check the cause first.
value := err
cause := errors.Cause(err.(error))
if cause != nil {
value = cause
}
_, ok := value.(*ErrParentDeviceHasChildren)
return ok
}
// ErrIncompatibleSeries is a standard error to indicate that the series
// requested is not compatible with the charm of the application.
type ErrIncompatibleSeries struct {
SeriesList []string
Series string
CharmName string
}
func (e *ErrIncompatibleSeries) Error() string {
return fmt.Sprintf("series %q not supported by charm %q, supported series are: %s",
e.Series, e.CharmName, strings.Join(e.SeriesList, ", "))
}
// IsIncompatibleSeriesError returns if the given error or its cause is
// ErrIncompatibleSeries.
func IsIncompatibleSeriesError(err interface{}) bool {
if err == nil {
return false
}
// In case of a wrapped error, check the cause first.
value := err
cause := errors.Cause(err.(error))
if cause != nil {
value = cause
}
_, ok := value.(*ErrIncompatibleSeries)
return ok
}