-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy patherror.go
More file actions
142 lines (124 loc) · 7.43 KB
/
error.go
File metadata and controls
142 lines (124 loc) · 7.43 KB
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
package api
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"encoding/json"
"fmt"
"net/http"
)
// CloudError represents a cloud error.
type CloudError struct {
// The status code.
StatusCode int `json:"-"`
// An error response from the service.
*CloudErrorBody `json:"error,omitempty"`
}
func (err *CloudError) Error() string {
var body string
if err.CloudErrorBody != nil {
body = ": " + err.String()
}
return fmt.Sprintf("%d%s", err.StatusCode, body)
}
// CloudErrorBody represents the body of a cloud error.
type CloudErrorBody struct {
// An identifier for the error. Codes are invariant and are intended to be consumed programmatically.
Code string `json:"code,omitempty"`
// A message describing the error, intended to be suitable for display in a user interface.
Message string `json:"message,omitempty"`
// The target of the particular error. For example, the name of the property in error.
Target string `json:"target,omitempty"`
// A list of additional details about the error.
Details []CloudErrorBody `json:"details,omitempty"`
}
func (b *CloudErrorBody) String() string {
var details string
if len(b.Details) > 0 {
details = " Details: "
for i, innerErr := range b.Details {
details += innerErr.String()
if i < len(b.Details)-1 {
details += ", "
}
}
}
return fmt.Sprintf("%s: %s: %s%s", b.Code, b.Target, b.Message, details)
}
// CloudErrorCodes
const (
CloudErrorCodeInternalServerError = "InternalServerError"
CloudErrorCodeDeploymentFailed = "DeploymentFailed"
CloudErrorCodeInvalidParameter = "InvalidParameter"
CloudErrorCodeInvalidRequestContent = "InvalidRequestContent"
CloudErrorCodeInvalidResource = "InvalidResource"
CloudErrorCodeDuplicateResourceGroup = "DuplicateResourceGroup"
CloudErrorCodeInvalidResourceNamespace = "InvalidResourceNamespace"
CloudErrorCodeInvalidResourceType = "InvalidResourceType"
CloudErrorCodeInvalidSubscriptionID = "InvalidSubscriptionID"
CloudErrorCodeMismatchingResourceID = "MismatchingResourceID"
CloudErrorCodeMismatchingResourceName = "MismatchingResourceName"
CloudErrorCodeMismatchingResourceType = "MismatchingResourceType"
CloudErrorCodePropertyChangeNotAllowed = "PropertyChangeNotAllowed"
CloudErrorCodeRequestNotAllowed = "RequestNotAllowed"
CloudErrorCodeResourceGroupNotFound = "ResourceGroupNotFound"
CloudErrorCodeClusterResourceGroupAlreadyExists = "ClusterResourceGroupAlreadyExists"
CloudErrorCodeResourceNotFound = "ResourceNotFound"
CloudErrorCodeUnsupportedMediaType = "UnsupportedMediaType"
CloudErrorCodeInvalidLinkedVNet = "InvalidLinkedVNet"
CloudErrorCodeInvalidLinkedSubnet = "InvalidLinkedSubnet"
CloudErrorCodeInvalidLinkedRouteTable = "InvalidLinkedRouteTable"
CloudErrorCodeInvalidLinkedNatGateway = "InvalidLinkedNatGateway"
CloudErrorCodeInvalidLinkedDiskEncryptionSet = "InvalidLinkedDiskEncryptionSet"
CloudErrorCodeNotFound = "NotFound"
CloudErrorCodeForbidden = "Forbidden"
CloudErrorCodeInvalidSubscriptionState = "InvalidSubscriptionState"
CloudErrorCodeInvalidServicePrincipalCredentials = "InvalidServicePrincipalCredentials"
CloudErrorCodeInvalidServicePrincipalToken = "InvalidServicePrincipalToken"
CloudErrorCodeInvalidServicePrincipalClaims = "InvalidServicePrincipalClaims"
CloudErrorCodeInvalidResourceProviderPermissions = "InvalidResourceProviderPermissions"
CloudErrorCodeInvalidServicePrincipalPermissions = "InvalidServicePrincipalPermissions"
CloudErrorCodeInvalidWorkloadIdentityPermissions = "InvalidWorkloadIdentityPermissions"
CloudErrorCodeInvalidClusterMSIPermissions = "InvalidClusterMSIPermissions"
CloudErrorCodeInvalidLocation = "InvalidLocation"
CloudErrorCodeInvalidOperationID = "InvalidOperationID"
CloudErrorCodeDuplicateClientID = "DuplicateClientID"
CloudErrorCodeDuplicateDomain = "DuplicateDomain"
CloudErrorCodeResourceQuotaExceeded = "ResourceQuotaExceeded"
CloudErrorCodeQuotaExceeded = "QuotaExceeded"
CloudErrorCodeResourceProviderNotRegistered = "ResourceProviderNotRegistered"
CloudErrorCodeCannotDeleteLoadBalancerByID = "CannotDeleteLoadBalancerWithPrivateLinkService"
CloudErrorCodeInUseSubnetCannotBeDeleted = "InUseSubnetCannotBeDeleted"
CloudErrorCodeScopeLocked = "ScopeLocked"
CloudErrorCodeRequestDisallowedByPolicy = "RequestDisallowedByPolicy"
CloudErrorCodeInvalidNetworkAddress = "InvalidNetworkAddress"
CloudErrorCodeThrottlingLimitExceeded = "ThrottlingLimitExceeded"
CloudErrorCodeInvalidCIDRRange = "InvalidCIDRRange"
CloudErrorCodePlatformWorkloadIdentityMismatch = "PlatformWorkloadIdentityMismatch"
CloudErrorCodePlatformWorkloadIdentityContainsInvalidFederatedCredential = "PlatformWorkloadIdentityContainsInvalidCredential"
CloudErrorCodeInvalidClusterMSICount = "InvalidClusterMSICount"
CloudErrorCodeInvalidPlatformWorkloadIdentity = "InvalidPlatformWorkloadIdentity"
CloudErrorCodeInvalidResourceID = "InvalidResourceID"
)
// NewCloudError creates a structured ARM-compliant CloudError,
// ensuring consistent error responses with status codes and targets.
func NewCloudError(statusCode int, code, target, message string) *CloudError {
return &CloudError{
StatusCode: statusCode,
CloudErrorBody: &CloudErrorBody{
Code: code,
Message: message,
Target: target,
},
}
}
// WriteError constructs and writes a CloudError to the given ResponseWriter
func WriteError(w http.ResponseWriter, statusCode int, code, target, message string) {
WriteCloudError(w, NewCloudError(statusCode, code, target, message))
}
// WriteCloudError writes a CloudError to the given ResponseWriter
func WriteCloudError(w http.ResponseWriter, err *CloudError) {
w.WriteHeader(err.StatusCode)
e := json.NewEncoder(w)
e.SetIndent("", " ")
_ = e.Encode(err)
}