-
Notifications
You must be signed in to change notification settings - Fork 189
/
errors.go
176 lines (160 loc) · 6.38 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package errhelp
import (
"encoding/json"
"fmt"
"strings"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/go-autorest/autorest/validation"
"k8s.io/apimachinery/pkg/api/errors"
)
const (
AccountNameInvalid = "AccountNameInvalid"
AlreadyExists = "AlreadyExists"
AsyncOpIncompleteError = "AsyncOpIncomplete"
BadRequest = "BadRequest"
CannotParseError = "CannotParseError"
ConflictingServerOperation = "ConflictingServerOperation"
ContainerOperationFailure = "ContainerOperationFailure"
ConsumerGroupNotFound = "ConsumerGroupNotFound"
CreationPending = "CreationPending"
FailoverGroupBusy = "FailoverGroupBusy"
Forbidden = "Forbidden"
InvalidAccessPolicy = "InvalidAccessPolicy"
InvalidCIDRNotation = "InvalidCIDRNotation"
InvalidFailoverGroupRegion = "InvalidFailoverGroupRegion"
InvalidParameters = "InvalidParameters"
InvalidRequestFormat = "InvalidRequestFormat"
InvalidResourceLocation = "InvalidResourceLocation"
InvalidServerName = "InvalidServerName"
InvalidResourceReference = "InvalidResourceReference"
KeyNotFound = "KeyNotFound"
LocationNotAvailableForResourceType = "LocationNotAvailableForResourceType"
ProvisioningDisabled = "ProvisioningDisabled"
NetcfgInvalidIPAddressPrefix = "NetcfgInvalidIPAddressPrefix"
NetcfgInvalidSubnet = "NetcfgInvalidSubnet"
NetcfgInvalidVirtualNetworkSite = "NetcfgInvalidVirtualNetworkSite"
NotFoundErrorCode = "NotFound"
NoSuchHost = "no such host"
ParentNotFoundErrorCode = "ParentResourceNotFound"
StorageAccountIsNotProvisioned = "StorageAccountIsNotProvisioned"
PreconditionFailed = "PreconditionFailed"
QuotaExceeded = "QuotaExceeded"
ResourceGroupNotFoundErrorCode = "ResourceGroupNotFound"
RegionDoesNotAllowProvisioning = "RegionDoesNotAllowProvisioning"
ResourceNotFound = "ResourceNotFound"
RequestConflictError = "Conflict"
ValidationError = "ValidationError"
SubscriptionDoesNotHaveServer = "SubscriptionDoesNotHaveServer"
NotSupported = "NotSupported"
SecretNotFound = "SecretNotFound"
RequestDisallowedByPolicy = "RequestDisallowedByPolicy"
ServiceBusy = "ServiceBusy"
NameNotAvailable = "NameNotAvailable"
PublicIPIdleTimeoutIsOutOfRange = "PublicIPIdleTimeoutIsOutOfRange"
InvalidRequestContent = "InvalidRequestContent"
InvalidMaxSizeTierCombination = "InvalidMaxSizeTierCombination"
InternalServerError = "InternalServerError"
NetworkAclsValidationFailure = "NetworkAclsValidationFailure"
SubnetHasServiceEndpointWithInvalidServiceName = "SubnetHasServiceEndpointWithInvalidServiceName"
InvalidAddressPrefixFormat = "InvalidAddressPrefixFormat"
FeatureNotSupportedForEdition = "FeatureNotSupportedForEdition"
VirtualNetworkRuleBadRequest = "VirtualNetworkRuleBadRequest"
LongTermRetentionPolicyInvalid = "LongTermRetentionPolicyInvalid"
BackupRetentionPolicyInvalid = "InvalidBackupRetentionPeriod"
OperationIdNotFound = "OperationIdNotFound"
ObjectIsBeingDeleted = "ObjectIsBeingDeleted"
ObjectIsDeletedButRecoverable = "ObjectIsDeletedButRecoverable"
)
func NewAzureError(err error) *AzureError {
var kind, reason string
if err == nil {
return nil
}
ae := AzureError{
Original: err,
}
switch v := err.(type) {
case autorest.DetailedError:
ae.Code = v.StatusCode.(int)
switch e := v.Original.(type) {
case *azure.RequestError:
if e.ServiceError != nil {
kind = e.ServiceError.Code
reason = e.ServiceError.Message
} else {
kind = CannotParseError
reason = CannotParseError
}
case azure.RequestError:
if e.ServiceError != nil {
kind = e.ServiceError.Code
reason = e.ServiceError.Message
} else {
kind = CannotParseError
reason = CannotParseError
}
case *azure.ServiceError:
kind = e.Code
reason = e.Message
if e.Code == "Failed" && len(e.AdditionalInfo) == 1 {
if v, ok := e.AdditionalInfo[0]["code"]; ok {
kind = v.(string)
}
}
case *errors.StatusError:
kind = "StatusError"
reason = "StatusError"
case *json.UnmarshalTypeError:
kind = NotFoundErrorCode
reason = NotFoundErrorCode
}
case azure.AsyncOpIncompleteError:
kind = "AsyncOpIncomplete"
reason = "AsyncOpIncomplete"
case validation.Error:
kind = "ValidationError"
reason = v.Message
default:
switch e := err.Error(); e {
case InvalidServerName, AlreadyExists, AccountNameInvalid:
kind = e
reason = e
default:
contains := []string{
InvalidAccessPolicy,
LocationNotAvailableForResourceType,
}
for _, val := range contains {
if strings.Contains(e, val) {
kind = val
reason = val
break
}
}
}
}
ae.Reason = reason
ae.Type = kind
return &ae
}
type AzureError struct {
Type string
Reason string
Code int
Original error
}
func (e AzureError) Error() string {
return e.Original.Error()
}
type AdminSecretNotFound struct {
Name string
}
func (e AdminSecretNotFound) Error() string {
return fmt.Sprintf("admin secret '%s' not found", e.Name)
}
func NewAdminSecretNotFoundError(name string) *AdminSecretNotFound {
return &AdminSecretNotFound{name}
}