~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/state/errors.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2012-2015 Canonical Ltd.
 
1
// Copyright 2012-2016 Canonical Ltd.
2
2
// Licensed under the AGPLv3, see LICENCE file for details.
3
3
 
4
4
package state
5
5
 
6
6
import (
7
7
        "fmt"
 
8
        "strings"
8
9
 
9
10
        "github.com/juju/errors"
10
11
        "gopkg.in/juju/charm.v6-unstable"
11
12
        "gopkg.in/mgo.v2/txn"
 
13
 
 
14
        "github.com/juju/juju/network"
12
15
)
13
16
 
14
17
// ErrCharmAlreadyUploaded is returned by UpdateUploadedCharm() when
53
56
        }
54
57
        return errors.Trace(txnErr)
55
58
}
 
59
 
 
60
// ErrProviderIDNotUnique is a standard error to indicate the value specified
 
61
// for a ProviderID field is not unique within the current model.
 
62
type ErrProviderIDNotUnique struct {
 
63
        duplicateIDs []string
 
64
}
 
65
 
 
66
func (e *ErrProviderIDNotUnique) Error() string {
 
67
        idList := strings.Join(e.duplicateIDs, ", ")
 
68
        return fmt.Sprintf("ProviderID(s) not unique: %s", idList)
 
69
}
 
70
 
 
71
// NewProviderIDNotUniqueError returns an instance of ErrProviderIDNotUnique
 
72
// initialized with the given duplicate provider IDs.
 
73
func NewProviderIDNotUniqueError(providerIDs ...network.Id) error {
 
74
        stringIDs := make([]string, len(providerIDs))
 
75
        for i, providerID := range providerIDs {
 
76
                stringIDs[i] = string(providerID)
 
77
        }
 
78
        return newProviderIDNotUniqueErrorFromStrings(stringIDs)
 
79
}
 
80
 
 
81
func newProviderIDNotUniqueErrorFromStrings(providerIDs []string) error {
 
82
        return &ErrProviderIDNotUnique{
 
83
                duplicateIDs: providerIDs,
 
84
        }
 
85
}
 
86
 
 
87
// IsProviderIDNotUniqueError returns if the given error or its cause is
 
88
// ErrProviderIDNotUnique.
 
89
func IsProviderIDNotUniqueError(err interface{}) bool {
 
90
        if err == nil {
 
91
                return false
 
92
        }
 
93
        // In case of a wrapped error, check the cause first.
 
94
        value := err
 
95
        cause := errors.Cause(err.(error))
 
96
        if cause != nil {
 
97
                value = cause
 
98
        }
 
99
        _, ok := value.(*ErrProviderIDNotUnique)
 
100
        return ok
 
101
}
 
102
 
 
103
// ErrParentDeviceHasChildren is a standard error to indicate a network
 
104
// link-layer device cannot be removed because other existing devices refer to
 
105
// it as their parent.
 
106
type ErrParentDeviceHasChildren struct {
 
107
        parentName  string
 
108
        numChildren int
 
109
}
 
110
 
 
111
func (e *ErrParentDeviceHasChildren) Error() string {
 
112
        return fmt.Sprintf("parent device %q has %d children", e.parentName, e.numChildren)
 
113
}
 
114
 
 
115
func newParentDeviceHasChildrenError(parentName string, numChildren int) error {
 
116
        return &ErrParentDeviceHasChildren{
 
117
                parentName:  parentName,
 
118
                numChildren: numChildren,
 
119
        }
 
120
}
 
121
 
 
122
// IsParentDeviceHasChildrenError returns if the given error or its cause is
 
123
// ErrParentDeviceHasChildren.
 
124
func IsParentDeviceHasChildrenError(err interface{}) bool {
 
125
        if err == nil {
 
126
                return false
 
127
        }
 
128
        // In case of a wrapped error, check the cause first.
 
129
        value := err
 
130
        cause := errors.Cause(err.(error))
 
131
        if cause != nil {
 
132
                value = cause
 
133
        }
 
134
        _, ok := value.(*ErrParentDeviceHasChildren)
 
135
        return ok
 
136
}