forked from cubefs/cubefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult_error.go
184 lines (171 loc) · 15.1 KB
/
result_error.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
// Copyright 2019 The CubeFS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package objectnode
import (
"encoding/xml"
"net/http"
"strconv"
"strings"
)
var (
UnsupportedOperation = &ErrorCode{ErrorCode: "NotImplemented", ErrorMessage: "A header you provided implies functionality that is not implemented.", StatusCode: http.StatusNotImplemented}
AccessDenied = &ErrorCode{ErrorCode: "AccessDenied", ErrorMessage: "Access Denied", StatusCode: http.StatusForbidden}
BadDigest = &ErrorCode{ErrorCode: "BadDigest", ErrorMessage: "The Content-MD5 you specified did not match what we received.", StatusCode: http.StatusBadRequest}
BucketNotExisted = &ErrorCode{ErrorCode: "BucketNotExisted", ErrorMessage: "The requested bucket name is not existed.", StatusCode: http.StatusNotFound}
BucketNotExistedForHead = &ErrorCode{ErrorCode: "BucketNotExisted", ErrorMessage: "The requested bucket name is not existed.", StatusCode: http.StatusConflict}
BucketNotEmpty = &ErrorCode{ErrorCode: "BucketNotEmpty", ErrorMessage: "The bucket you tried to delete is not empty.", StatusCode: http.StatusConflict}
BucketNotOwnedByYou = &ErrorCode{ErrorCode: "BucketNotOwnedByYou", ErrorMessage: "The bucket is not owned by you.", StatusCode: http.StatusConflict}
InvalidKey = &ErrorCode{ErrorCode: "InvalidKey", ErrorMessage: "Object key is Illegal", StatusCode: http.StatusBadRequest}
EntityTooSmall = &ErrorCode{ErrorCode: "EntityTooSmall", ErrorMessage: "Your proposed upload is smaller than the minimum allowed object size.", StatusCode: http.StatusBadRequest}
EntityTooLarge = &ErrorCode{ErrorCode: "EntityTooLarge", ErrorMessage: "Your proposed upload exceeds the maximum allowed object size.", StatusCode: http.StatusBadRequest}
IncorrectNumberOfFilesInPostRequest = &ErrorCode{ErrorCode: "IncorrectNumberOfFilesInPostRequest", ErrorMessage: "POST requires exactly one file upload per request.", StatusCode: http.StatusBadRequest}
InvalidArgument = &ErrorCode{ErrorCode: "InvalidArgument", ErrorMessage: "Invalid Argument", StatusCode: http.StatusBadRequest}
DisableCreateBucketByS3 = &ErrorCode{ErrorCode: "DisableCreateBucketByS3", ErrorMessage: "DisableCreateBucketByS3.", StatusCode: http.StatusBadRequest}
InvalidBucketName = &ErrorCode{ErrorCode: "InvalidBucketName", ErrorMessage: "The specified bucket is not valid.", StatusCode: http.StatusBadRequest}
InvalidRange = &ErrorCode{ErrorCode: "InvalidRange", ErrorMessage: "The requested range cannot be satisfied.", StatusCode: http.StatusRequestedRangeNotSatisfiable}
MissingContentLength = &ErrorCode{ErrorCode: "MissingContentLength", ErrorMessage: "You must provide the Content-Length HTTP header.", StatusCode: http.StatusLengthRequired}
NoSuchBucket = &ErrorCode{ErrorCode: "NoSuchBucket", ErrorMessage: "The specified bucket does not exist.", StatusCode: http.StatusNotFound}
NoSuchKey = &ErrorCode{ErrorCode: "NoSuchKey", ErrorMessage: "The specified key does not exist.", StatusCode: http.StatusNotFound}
NoSuchBucketPolicy = &ErrorCode{ErrorCode: "NoSuchBucketPolicy", ErrorMessage: "The specified bucket does not have a bucket policy.", StatusCode: http.StatusNotFound}
PreconditionFailed = &ErrorCode{ErrorCode: "PreconditionFailed", ErrorMessage: "At least one of the preconditions you specified did not hold.", StatusCode: http.StatusPreconditionFailed}
MaxContentLength = &ErrorCode{ErrorCode: "MaxContentLength", ErrorMessage: "Content-Length is bigger than 20KB.", StatusCode: http.StatusLengthRequired}
BucketAlreadyOwnedByYou = &ErrorCode{ErrorCode: "BucketAlreadyOwnedByYou", ErrorMessage: "The bucket that you tried to create already exists, and you own it. ", StatusCode: http.StatusConflict}
InvalidLocationConstraint = &ErrorCode{ErrorCode: "CreateBucketFailed", ErrorMessage: "The specified location (Region) constraint is not valid.", StatusCode: http.StatusBadRequest}
ObjectModeConflict = &ErrorCode{ErrorCode: "ObjectModeConflict", ErrorMessage: "Object already exists but file mode conflicts", StatusCode: http.StatusConflict}
NotModified = &ErrorCode{ErrorCode: "MaxContentLength", ErrorMessage: "Not modified.", StatusCode: http.StatusNotModified}
NoSuchUpload = &ErrorCode{ErrorCode: "NoSuchUpload", ErrorMessage: "The specified upload does not exist.", StatusCode: http.StatusNotFound}
ConflictUploadRequest = &ErrorCode{ErrorCode: "ConflictUploadRequest", ErrorMessage: "Conflict request, please try again later.", StatusCode: http.StatusConflict}
OverMaxRecordSize = &ErrorCode{ErrorCode: "OverMaxRecordSize", ErrorMessage: "The length of a record in the input or result is greater than maxCharsPerRecord of 1 MB.", StatusCode: http.StatusBadRequest}
CopySourceSizeTooLarge = &ErrorCode{ErrorCode: "InvalidRequest", ErrorMessage: "The specified copy source is larger than the maximum allowable size for a copy source: 5368709120", StatusCode: http.StatusBadRequest}
InvalidPartOrder = &ErrorCode{ErrorCode: "InvalidPartOrder", ErrorMessage: "The list of parts was not in ascending order. Parts list must be specified in order by part number.", StatusCode: http.StatusBadRequest}
InvalidPartNumber = &ErrorCode{ErrorCode: "InvalidPartNumber", ErrorMessage: "The specified partNumber must be greater than 0.", StatusCode: http.StatusBadRequest}
InvalidPart = &ErrorCode{ErrorCode: "InvalidPart", ErrorMessage: "One or more of the specified parts could not be found. The part might not have been uploaded, or the specified entity tag might not have matched the part's entity tag.", StatusCode: http.StatusBadRequest}
InvalidCacheArgument = &ErrorCode{ErrorCode: "InvalidCacheArgument", ErrorMessage: "Invalid Cache-Control or Expires Argument", StatusCode: http.StatusBadRequest}
ExceedTagLimit = &ErrorCode{ErrorCode: "InvalidTagError", ErrorMessage: "Object tags cannot be greater than 10", StatusCode: http.StatusBadRequest}
InvalidTagKey = &ErrorCode{ErrorCode: "InvalidTag", ErrorMessage: "The TagKey you have provided is invalid", StatusCode: http.StatusBadRequest}
InvalidTagValue = &ErrorCode{ErrorCode: "InvalidTag", ErrorMessage: "The TagValue you have provided is invalid", StatusCode: http.StatusBadRequest}
MissingContentMD5 = &ErrorCode{ErrorCode: "InvalidRequest", ErrorMessage: "Missing required header for this request: Content-MD5.", StatusCode: http.StatusBadRequest}
UnexpectedContent = &ErrorCode{ErrorCode: "UnexpectedContent", ErrorMessage: "This request does not support content.", StatusCode: http.StatusBadRequest}
AccessDeniedBySTS = &ErrorCode{ErrorCode: "AccessDeniedBySTS", ErrorMessage: "Access Denied by STS.", StatusCode: http.StatusForbidden}
InvalidToken = &ErrorCode{ErrorCode: "InvalidToken", ErrorMessage: "The provided token is malformed or otherwise invalid.", StatusCode: http.StatusBadRequest}
ExpiredToken = &ErrorCode{ErrorCode: "ExpiredToken", ErrorMessage: "The provided token has expired.", StatusCode: http.StatusBadRequest}
MissingSecurityElement = &ErrorCode{ErrorCode: "MissingSecurityElement", ErrorMessage: "The request is missing a security element.", StatusCode: http.StatusBadRequest}
RequestTimeTooSkewed = &ErrorCode{ErrorCode: "RequestTimeTooSkewed", ErrorMessage: "The difference between the request time and the server's time is too large.", StatusCode: http.StatusBadRequest}
NoSuchTagSetError = &ErrorCode{ErrorCode: "NoSuchTagSetError", ErrorMessage: "The TagSet does not exist.", StatusCode: http.StatusNotFound}
InvalidTagError = &ErrorCode{ErrorCode: "InvalidTagError", ErrorMessage: "Missing tag in body.", StatusCode: http.StatusBadRequest}
NoSuchCORSConfiguration = &ErrorCode{ErrorCode: "NoSuchCORSConfiguration", ErrorMessage: "The CORS configuration does not exist.", StatusCode: http.StatusNotFound}
CORSRuleNotMatch = &ErrorCode{ErrorCode: "AccessForbidden", ErrorMessage: "CORSResponse: This CORS request is not allowed.", StatusCode: http.StatusForbidden}
ErrCORSNotEnabled = &ErrorCode{ErrorCode: "AccessForbidden", ErrorMessage: "CORSResponse: CORS is not enabled for this bucket.", StatusCode: http.StatusForbidden}
MissingOriginHeader = &ErrorCode{ErrorCode: "MissingOriginHeader", ErrorMessage: "Missing Origin header.", StatusCode: http.StatusBadRequest}
MalformedXML = &ErrorCode{ErrorCode: "MalformedXML", ErrorMessage: "The XML you provided was not well-formed or did not validate against our published schema.", StatusCode: http.StatusBadRequest}
TooManyCorsRules = &ErrorCode{ErrorCode: "TooManyCorsRules", ErrorMessage: "Too many cors rules.", StatusCode: http.StatusBadRequest}
InvalidDigest = &ErrorCode{ErrorCode: "InvalidDigest", ErrorMessage: "The Content-MD5 you specified is not valid.", StatusCode: http.StatusBadRequest}
KeyTooLong = &ErrorCode{ErrorCode: "KeyTooLongError", ErrorMessage: "Your key is too long.", StatusCode: http.StatusBadRequest}
InvalidAccessKeyId = &ErrorCode{ErrorCode: "InvalidAccessKeyId", ErrorMessage: "The access key Id you provided does not exist in our records.", StatusCode: http.StatusForbidden}
SignatureDoesNotMatch = &ErrorCode{ErrorCode: "SignatureDoesNotMatch", ErrorMessage: "The request signature we calculated does not match the signature you provided.", StatusCode: http.StatusForbidden}
InvalidMaxPartNumber = &ErrorCode{ErrorCode: "InvalidRequest", ErrorMessage: "The total part numbers exceed limit.", StatusCode: http.StatusBadRequest}
InvalidMinPartNumber = &ErrorCode{ErrorCode: "InvalidRequest", ErrorMessage: "You must specify at least one part.", StatusCode: http.StatusBadRequest}
DiskQuotaExceeded = &ErrorCode{"DiskQuotaExceeded", "Disk Quota Exceeded.", http.StatusBadRequest}
FileDeleteLock = &ErrorCode{"FileDeleteLock", "Operation not permitted.", http.StatusBadRequest}
LifeCycleRulesGreaterThen1K = &ErrorCode{ErrorCode: "InvalidRequest", ErrorMessage: "The number of lifecycle rules must not exceed the allowed limit of 1000 rules.", StatusCode: http.StatusBadRequest}
LifeCycleRulesLessThenOne = &ErrorCode{ErrorCode: "InvalidRequest", ErrorMessage: "At least one lifecycle rule must be specified.", StatusCode: http.StatusBadRequest}
LifeCycleAtLeastOneAction = &ErrorCode{ErrorCode: "InvalidRequest", ErrorMessage: "At least one action must be specified in a lifecycle rule.", StatusCode: http.StatusBadRequest}
LifeCycleRulesInvalid = &ErrorCode{ErrorCode: "InvalidRequest", ErrorMessage: "Lifecycle rule is invalid.", StatusCode: http.StatusBadRequest}
NoSuchObjectLockConfiguration = &ErrorCode{"NoSuchObjectLockConfiguration", "The specified object does not have a ObjectLock configuration", http.StatusNotFound}
NoContentMd5HeaderErr = &ErrorCode{"NoContentMd5Header", "Content-MD5 HTTP header is required for Upload Object/Part requests with Object Lock parameters", http.StatusBadRequest}
ObjectLockConfigurationNotFound = &ErrorCode{"ObjectLockConfigurationNotFoundError", "Object Lock configuration does not exist for this bucket", http.StatusNotFound}
TooManyRequests = &ErrorCode{"TooManyRequests", "too many requests, please retry later", http.StatusTooManyRequests}
MalformedPOSTRequest = &ErrorCode{ErrorCode: "MalformedPOSTRequest", ErrorMessage: "The body of your POST request is not well-formed multipart/form-data.", StatusCode: http.StatusBadRequest}
)
type ErrorCode struct {
ErrorCode string
ErrorMessage string
StatusCode int
}
type ErrorResponse struct {
XMLName xml.Name `xml:"Error"`
Code string `xml:"Code"`
Message string `xml:"Message"`
Resource string `xml:"Resource"`
RequestId string `xml:"RequestId"`
}
func NewError(errCode, errMsg string, statusCode int) *ErrorCode {
return &ErrorCode{
ErrorCode: errCode,
ErrorMessage: errMsg,
StatusCode: statusCode,
}
}
func (ec *ErrorCode) ServeResponse(w http.ResponseWriter, r *http.Request) {
// write status code to request context,
// traceMiddleWare send exception request to prometheus via status code
SetResponseStatusCode(r, strconv.Itoa(ec.StatusCode))
SetResponseErrorMessage(r, ec.ErrorMessage)
errorResponse := ErrorResponse{
Code: ec.ErrorCode,
Message: ec.ErrorMessage,
Resource: r.URL.String(),
RequestId: GetRequestID(r),
}
response, _ := xml.Marshal(errorResponse)
response = append([]byte(strings.TrimSuffix(xml.Header, "\n")), response...)
w.Header().Set(ContentType, ValueContentTypeXML)
w.Header().Set(ContentLength, strconv.Itoa(len(response)))
w.WriteHeader(ec.StatusCode)
_, _ = w.Write(response)
}
func (ec *ErrorCode) Error() string {
return ec.ErrorMessage
}
func (ec *ErrorCode) Copy() *ErrorCode {
return &ErrorCode{
ErrorCode: ec.ErrorCode,
ErrorMessage: ec.ErrorMessage,
StatusCode: ec.StatusCode,
}
}
func ServeInternalStaticErrorResponse(w http.ResponseWriter, r *http.Request) {
w.Header().Set(ContentType, ValueContentTypeXML)
w.WriteHeader(http.StatusInternalServerError)
sb := strings.Builder{}
sb.WriteString(xml.Header)
sb.WriteString("<Error><Code>InternalError</Code><Message>We encountered an internal error. Please try again.</Message><Resource>")
sb.WriteString(r.URL.String())
sb.WriteString("</Resource><RequestId>")
sb.WriteString(GetRequestID(r))
sb.WriteString("</RequestId></Error>")
_, _ = w.Write([]byte(sb.String()))
}
func HttpStatusErrorCode(code int) *ErrorCode {
statusText := http.StatusText(code)
statusTextWithoutSpace := strings.ReplaceAll(statusText, " ", "")
return &ErrorCode{
ErrorCode: statusTextWithoutSpace,
ErrorMessage: statusText,
StatusCode: code,
}
}
func InternalErrorCode(err error) *ErrorCode {
var errorMessage string
if err != nil {
errorMessage = err.Error()
} else {
errorMessage = "Internal Server Error"
}
return &ErrorCode{
ErrorCode: "InternalError",
ErrorMessage: errorMessage,
StatusCode: http.StatusInternalServerError,
}
}