Skip to content

Commit dc94dff

Browse files
authored
Add constants for response errors (#2120)
1 parent 7a9dff0 commit dc94dff

21 files changed

+183
-128
lines changed

api-bucket-cors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (c *Client) GetBucketCors(ctx context.Context, bucketName string) (*cors.Co
9898
bucketCors, err := c.getBucketCors(ctx, bucketName)
9999
if err != nil {
100100
errResponse := ToErrorResponse(err)
101-
if errResponse.Code == "NoSuchCORSConfiguration" {
101+
if errResponse.Code == NoSuchCORSConfiguration {
102102
return nil, nil
103103
}
104104
return nil, err

api-bucket-policy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (c *Client) GetBucketPolicy(ctx context.Context, bucketName string) (string
104104
bucketPolicy, err := c.getBucketPolicy(ctx, bucketName)
105105
if err != nil {
106106
errResponse := ToErrorResponse(err)
107-
if errResponse.Code == "NoSuchBucketPolicy" {
107+
if errResponse.Code == NoSuchBucketPolicy {
108108
return "", nil
109109
}
110110
return "", err

api-error-response.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -136,39 +136,39 @@ func httpRespToErrorResponse(resp *http.Response, bucketName, objectName string)
136136
if objectName == "" {
137137
errResp = ErrorResponse{
138138
StatusCode: resp.StatusCode,
139-
Code: "NoSuchBucket",
140-
Message: "The specified bucket does not exist.",
139+
Code: NoSuchBucket,
140+
Message: s3ErrorResponseMap[NoSuchBucket],
141141
BucketName: bucketName,
142142
}
143143
} else {
144144
errResp = ErrorResponse{
145145
StatusCode: resp.StatusCode,
146-
Code: "NoSuchKey",
147-
Message: "The specified key does not exist.",
146+
Code: NoSuchKey,
147+
Message: s3ErrorResponseMap[NoSuchKey],
148148
BucketName: bucketName,
149149
Key: objectName,
150150
}
151151
}
152152
case http.StatusForbidden:
153153
errResp = ErrorResponse{
154154
StatusCode: resp.StatusCode,
155-
Code: "AccessDenied",
156-
Message: "Access Denied.",
155+
Code: AccessDenied,
156+
Message: s3ErrorResponseMap[AccessDenied],
157157
BucketName: bucketName,
158158
Key: objectName,
159159
}
160160
case http.StatusConflict:
161161
errResp = ErrorResponse{
162162
StatusCode: resp.StatusCode,
163-
Code: "Conflict",
164-
Message: "Bucket not empty.",
163+
Code: Conflict,
164+
Message: s3ErrorResponseMap[Conflict],
165165
BucketName: bucketName,
166166
}
167167
case http.StatusPreconditionFailed:
168168
errResp = ErrorResponse{
169169
StatusCode: resp.StatusCode,
170-
Code: "PreconditionFailed",
171-
Message: s3ErrorResponseMap["PreconditionFailed"],
170+
Code: PreconditionFailed,
171+
Message: s3ErrorResponseMap[PreconditionFailed],
172172
BucketName: bucketName,
173173
Key: objectName,
174174
}
@@ -209,7 +209,7 @@ func httpRespToErrorResponse(resp *http.Response, bucketName, objectName string)
209209
if errResp.Region == "" {
210210
errResp.Region = resp.Header.Get("x-amz-bucket-region")
211211
}
212-
if errResp.Code == "InvalidRegion" && errResp.Region != "" {
212+
if errResp.Code == InvalidRegion && errResp.Region != "" {
213213
errResp.Message = fmt.Sprintf("Region does not match, expecting region ‘%s’.", errResp.Region)
214214
}
215215

@@ -218,10 +218,11 @@ func httpRespToErrorResponse(resp *http.Response, bucketName, objectName string)
218218

219219
// errTransferAccelerationBucket - bucket name is invalid to be used with transfer acceleration.
220220
func errTransferAccelerationBucket(bucketName string) error {
221+
msg := "The name of the bucket used for Transfer Acceleration must be DNS-compliant and must not contain periods ‘.’."
221222
return ErrorResponse{
222223
StatusCode: http.StatusBadRequest,
223-
Code: "InvalidArgument",
224-
Message: "The name of the bucket used for Transfer Acceleration must be DNS-compliant and must not contain periods ‘.’.",
224+
Code: InvalidArgument,
225+
Message: msg,
225226
BucketName: bucketName,
226227
}
227228
}
@@ -231,7 +232,7 @@ func errEntityTooLarge(totalSize, maxObjectSize int64, bucketName, objectName st
231232
msg := fmt.Sprintf("Your proposed upload size ‘%d’ exceeds the maximum allowed object size ‘%d’ for single PUT operation.", totalSize, maxObjectSize)
232233
return ErrorResponse{
233234
StatusCode: http.StatusBadRequest,
234-
Code: "EntityTooLarge",
235+
Code: EntityTooLarge,
235236
Message: msg,
236237
BucketName: bucketName,
237238
Key: objectName,
@@ -243,7 +244,7 @@ func errEntityTooSmall(totalSize int64, bucketName, objectName string) error {
243244
msg := fmt.Sprintf("Your proposed upload size ‘%d’ is below the minimum allowed object size ‘0B’ for single PUT operation.", totalSize)
244245
return ErrorResponse{
245246
StatusCode: http.StatusBadRequest,
246-
Code: "EntityTooSmall",
247+
Code: EntityTooSmall,
247248
Message: msg,
248249
BucketName: bucketName,
249250
Key: objectName,
@@ -255,7 +256,7 @@ func errUnexpectedEOF(totalRead, totalSize int64, bucketName, objectName string)
255256
msg := fmt.Sprintf("Data read ‘%d’ is not equal to the size ‘%d’ of the input Reader.", totalRead, totalSize)
256257
return ErrorResponse{
257258
StatusCode: http.StatusBadRequest,
258-
Code: "UnexpectedEOF",
259+
Code: UnexpectedEOF,
259260
Message: msg,
260261
BucketName: bucketName,
261262
Key: objectName,
@@ -266,7 +267,7 @@ func errUnexpectedEOF(totalRead, totalSize int64, bucketName, objectName string)
266267
func errInvalidArgument(message string) error {
267268
return ErrorResponse{
268269
StatusCode: http.StatusBadRequest,
269-
Code: "InvalidArgument",
270+
Code: InvalidArgument,
270271
Message: message,
271272
RequestID: "minio",
272273
}
@@ -277,7 +278,7 @@ func errInvalidArgument(message string) error {
277278
func errAPINotSupported(message string) error {
278279
return ErrorResponse{
279280
StatusCode: http.StatusNotImplemented,
280-
Code: "APINotSupported",
281+
Code: APINotSupported,
281282
Message: message,
282283
RequestID: "minio",
283284
}

api-error-response_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func TestHttpRespToErrorResponse(t *testing.T) {
8989
genInvalidError := func(message string) error {
9090
errResp := ErrorResponse{
9191
StatusCode: http.StatusBadRequest,
92-
Code: "InvalidArgument",
92+
Code: InvalidArgument,
9393
Message: message,
9494
RequestID: "minio",
9595
}
@@ -134,7 +134,7 @@ func TestHttpRespToErrorResponse(t *testing.T) {
134134
// List of APIErrors used to generate/mock server side XML error response.
135135
APIErrors := []APIError{
136136
{
137-
Code: "NoSuchBucketPolicy",
137+
Code: NoSuchBucketPolicy,
138138
Description: "The specified bucket does not have a bucket policy.",
139139
HTTPStatusCode: http.StatusNotFound,
140140
},
@@ -145,10 +145,10 @@ func TestHttpRespToErrorResponse(t *testing.T) {
145145
expectedErrResponse := []error{
146146
genInvalidError("Empty http response. " + "Please report this issue at https://github.com/minio/minio-go/issues."),
147147
decodeXMLError(createAPIErrorResponse(APIErrors[0], "minio-bucket")),
148-
genErrResponse(setCommonHeaders(&http.Response{StatusCode: http.StatusNotFound}), "NoSuchBucket", "The specified bucket does not exist.", "minio-bucket", ""),
149-
genErrResponse(setCommonHeaders(&http.Response{StatusCode: http.StatusNotFound}), "NoSuchKey", "The specified key does not exist.", "minio-bucket", "Asia/"),
150-
genErrResponse(setCommonHeaders(&http.Response{StatusCode: http.StatusForbidden}), "AccessDenied", "Access Denied.", "minio-bucket", ""),
151-
genErrResponse(setCommonHeaders(&http.Response{StatusCode: http.StatusConflict}), "Conflict", "Bucket not empty.", "minio-bucket", ""),
148+
genErrResponse(setCommonHeaders(&http.Response{StatusCode: http.StatusNotFound}), NoSuchBucket, s3ErrorResponseMap[NoSuchBucket], "minio-bucket", ""),
149+
genErrResponse(setCommonHeaders(&http.Response{StatusCode: http.StatusNotFound}), NoSuchKey, s3ErrorResponseMap[NoSuchKey], "minio-bucket", "Asia/"),
150+
genErrResponse(setCommonHeaders(&http.Response{StatusCode: http.StatusForbidden}), AccessDenied, s3ErrorResponseMap[AccessDenied], "minio-bucket", ""),
151+
genErrResponse(setCommonHeaders(&http.Response{StatusCode: http.StatusConflict}), Conflict, s3ErrorResponseMap[Conflict], "minio-bucket", ""),
152152
genErrResponse(setCommonHeaders(&http.Response{StatusCode: http.StatusBadRequest}), "Bad Request", "Bad Request", "minio-bucket", ""),
153153
genErrResponse(setCommonHeaders(&http.Response{StatusCode: http.StatusInternalServerError}), "Internal Server Error", "my custom object store error", "minio-bucket", ""),
154154
genErrResponse(setCommonHeaders(&http.Response{StatusCode: http.StatusInternalServerError}), "Internal Server Error", "my custom object store error, with way too long body", "minio-bucket", ""),
@@ -199,7 +199,7 @@ func TestErrEntityTooLarge(t *testing.T) {
199199
msg := fmt.Sprintf("Your proposed upload size ‘%d’ exceeds the maximum allowed object size ‘%d’ for single PUT operation.", 1000000, 99999)
200200
expectedResult := ErrorResponse{
201201
StatusCode: http.StatusBadRequest,
202-
Code: "EntityTooLarge",
202+
Code: EntityTooLarge,
203203
Message: msg,
204204
BucketName: "minio-bucket",
205205
Key: "Asia/",
@@ -215,7 +215,7 @@ func TestErrEntityTooSmall(t *testing.T) {
215215
msg := fmt.Sprintf("Your proposed upload size ‘%d’ is below the minimum allowed object size ‘0B’ for single PUT operation.", -1)
216216
expectedResult := ErrorResponse{
217217
StatusCode: http.StatusBadRequest,
218-
Code: "EntityTooSmall",
218+
Code: EntityTooSmall,
219219
Message: msg,
220220
BucketName: "minio-bucket",
221221
Key: "Asia/",
@@ -232,7 +232,7 @@ func TestErrUnexpectedEOF(t *testing.T) {
232232
strconv.FormatInt(100, 10), strconv.FormatInt(101, 10))
233233
expectedResult := ErrorResponse{
234234
StatusCode: http.StatusBadRequest,
235-
Code: "UnexpectedEOF",
235+
Code: UnexpectedEOF,
236236
Message: msg,
237237
BucketName: "minio-bucket",
238238
Key: "Asia/",
@@ -247,7 +247,7 @@ func TestErrUnexpectedEOF(t *testing.T) {
247247
func TestErrInvalidArgument(t *testing.T) {
248248
expectedResult := ErrorResponse{
249249
StatusCode: http.StatusBadRequest,
250-
Code: "InvalidArgument",
250+
Code: InvalidArgument,
251251
Message: "Invalid Argument",
252252
RequestID: "minio",
253253
}
@@ -260,20 +260,20 @@ func TestErrInvalidArgument(t *testing.T) {
260260
// Tests if the Message field is missing.
261261
func TestErrWithoutMessage(t *testing.T) {
262262
errResp := ErrorResponse{
263-
Code: "AccessDenied",
263+
Code: AccessDenied,
264264
RequestID: "minio",
265265
}
266266

267-
if errResp.Error() != "Access Denied." {
268-
t.Errorf("Expected \"Access Denied.\", got %s", errResp)
267+
if errResp.Error() != s3ErrorResponseMap[AccessDenied] {
268+
t.Errorf("Expected \"%s\", got %s", s3ErrorResponseMap[AccessDenied], errResp)
269269
}
270270

271271
errResp = ErrorResponse{
272-
Code: "InvalidArgument",
272+
Code: InvalidArgument,
273273
RequestID: "minio",
274274
}
275275
if errResp.Error() != fmt.Sprintf("Error response code %s.", errResp.Code) {
276-
t.Errorf("Expected \"Error response code InvalidArgument.\", got \"%s\"", errResp)
276+
t.Errorf("Expected \"Error response code %s.\", got \"%s\"", InvalidArgument, errResp)
277277
}
278278
}
279279

api-get-object.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ func (c *Client) GetObject(ctx context.Context, bucketName, objectName string, o
3434
if err := s3utils.CheckValidBucketName(bucketName); err != nil {
3535
return nil, ErrorResponse{
3636
StatusCode: http.StatusBadRequest,
37-
Code: "InvalidBucketName",
37+
Code: InvalidBucketName,
3838
Message: err.Error(),
3939
}
4040
}
4141
if err := s3utils.CheckValidObjectName(objectName); err != nil {
4242
return nil, ErrorResponse{
4343
StatusCode: http.StatusBadRequest,
44-
Code: "XMinioInvalidObjectName",
44+
Code: XMinioInvalidObjectName,
4545
Message: err.Error(),
4646
}
4747
}
@@ -659,14 +659,14 @@ func (c *Client) getObject(ctx context.Context, bucketName, objectName string, o
659659
if err := s3utils.CheckValidBucketName(bucketName); err != nil {
660660
return nil, ObjectInfo{}, nil, ErrorResponse{
661661
StatusCode: http.StatusBadRequest,
662-
Code: "InvalidBucketName",
662+
Code: InvalidBucketName,
663663
Message: err.Error(),
664664
}
665665
}
666666
if err := s3utils.CheckValidObjectName(objectName); err != nil {
667667
return nil, ObjectInfo{}, nil, ErrorResponse{
668668
StatusCode: http.StatusBadRequest,
669-
Code: "XMinioInvalidObjectName",
669+
Code: XMinioInvalidObjectName,
670670
Message: err.Error(),
671671
}
672672
}

api-list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func (c *Client) listObjectsV2Query(ctx context.Context, bucketName, objectPrefi
285285
// sure proper responses are received.
286286
if listBucketResult.IsTruncated && listBucketResult.NextContinuationToken == "" {
287287
return listBucketResult, ErrorResponse{
288-
Code: "NotImplemented",
288+
Code: NotImplemented,
289289
Message: "Truncated response should have continuation token set",
290290
}
291291
}

api-prompt-object.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ func (c *Client) PromptObject(ctx context.Context, bucketName, objectName, promp
3535
if err := s3utils.CheckValidBucketName(bucketName); err != nil {
3636
return nil, ErrorResponse{
3737
StatusCode: http.StatusBadRequest,
38-
Code: "InvalidBucketName",
38+
Code: InvalidBucketName,
3939
Message: err.Error(),
4040
}
4141
}
4242
if err := s3utils.CheckValidObjectName(objectName); err != nil {
4343
return nil, ErrorResponse{
4444
StatusCode: http.StatusBadRequest,
45-
Code: "XMinioInvalidObjectName",
45+
Code: XMinioInvalidObjectName,
4646
Message: err.Error(),
4747
}
4848
}

api-put-bucket.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (c *Client) makeBucket(ctx context.Context, bucketName string, opts MakeBuc
3535

3636
err = c.doMakeBucket(ctx, bucketName, opts)
3737
if err != nil && (opts.Region == "" || opts.Region == "us-east-1") {
38-
if resp, ok := err.(ErrorResponse); ok && resp.Code == "AuthorizationHeaderMalformed" && resp.Region != "" {
38+
if resp, ok := err.(ErrorResponse); ok && resp.Code == AuthorizationHeaderMalformed && resp.Region != "" {
3939
opts.Region = resp.Region
4040
err = c.doMakeBucket(ctx, bucketName, opts)
4141
}

api-put-object-multipart.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (c *Client) putObjectMultipart(ctx context.Context, bucketName, objectName
4444
errResp := ToErrorResponse(err)
4545
// Verify if multipart functionality is not available, if not
4646
// fall back to single PutObject operation.
47-
if errResp.Code == "AccessDenied" && strings.Contains(errResp.Message, "Access Denied") {
47+
if errResp.Code == AccessDenied && strings.Contains(errResp.Message, "Access Denied") {
4848
// Verify if size of reader is greater than '5GiB'.
4949
if size > maxSinglePutObjectSize {
5050
return UploadInfo{}, errEntityTooLarge(size, maxSinglePutObjectSize, bucketName, objectName)

api-put-object-streaming.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (c *Client) putObjectMultipartStream(ctx context.Context, bucketName, objec
5656
errResp := ToErrorResponse(err)
5757
// Verify if multipart functionality is not available, if not
5858
// fall back to single PutObject operation.
59-
if errResp.Code == "AccessDenied" && strings.Contains(errResp.Message, "Access Denied") {
59+
if errResp.Code == AccessDenied && strings.Contains(errResp.Message, "Access Denied") {
6060
// Verify if size of reader is greater than '5GiB'.
6161
if size > maxSinglePutObjectSize {
6262
return UploadInfo{}, errEntityTooLarge(size, maxSinglePutObjectSize, bucketName, objectName)

api-remove.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func processRemoveMultiObjectsResponse(body io.Reader, resultCh chan<- RemoveObj
272272
for _, obj := range rmResult.UnDeletedObjects {
273273
// Version does not exist is not an error ignore and continue.
274274
switch obj.Code {
275-
case "InvalidArgument", "NoSuchVersion":
275+
case InvalidArgument, NoSuchVersion:
276276
continue
277277
}
278278
resultCh <- RemoveObjectResult{
@@ -573,7 +573,7 @@ func (c *Client) removeObjects(ctx context.Context, bucketName string, objectsCh
573573
if err := removeResult.Err; err != nil {
574574
// Version does not exist is not an error ignore and continue.
575575
switch ToErrorResponse(err).Code {
576-
case "InvalidArgument", "NoSuchVersion":
576+
case InvalidArgument, NoSuchVersion:
577577
continue
578578
}
579579
resultCh <- removeResult
@@ -702,7 +702,7 @@ func (c *Client) abortMultipartUpload(ctx context.Context, bucketName, objectNam
702702
// This is needed specifically for abort and it cannot
703703
// be converged into default case.
704704
errorResponse = ErrorResponse{
705-
Code: "NoSuchUpload",
705+
Code: NoSuchUpload,
706706
Message: "The specified multipart upload does not exist.",
707707
BucketName: bucketName,
708708
Key: objectName,

api-stat.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ func (c *Client) BucketExists(ctx context.Context, bucketName string) (bool, err
3939
})
4040
defer closeResponse(resp)
4141
if err != nil {
42-
if ToErrorResponse(err).Code == "NoSuchBucket" {
42+
if ToErrorResponse(err).Code == NoSuchBucket {
4343
return false, nil
4444
}
4545
return false, err
4646
}
4747
if resp != nil {
4848
resperr := httpRespToErrorResponse(resp, bucketName, "")
49-
if ToErrorResponse(resperr).Code == "NoSuchBucket" {
49+
if ToErrorResponse(resperr).Code == NoSuchBucket {
5050
return false, nil
5151
}
5252
if resp.StatusCode != http.StatusOK {
@@ -63,14 +63,14 @@ func (c *Client) StatObject(ctx context.Context, bucketName, objectName string,
6363
if err := s3utils.CheckValidBucketName(bucketName); err != nil {
6464
return ObjectInfo{}, ErrorResponse{
6565
StatusCode: http.StatusBadRequest,
66-
Code: "InvalidBucketName",
66+
Code: InvalidBucketName,
6767
Message: err.Error(),
6868
}
6969
}
7070
if err := s3utils.CheckValidObjectName(objectName); err != nil {
7171
return ObjectInfo{}, ErrorResponse{
7272
StatusCode: http.StatusBadRequest,
73-
Code: "XMinioInvalidObjectName",
73+
Code: XMinioInvalidObjectName,
7474
Message: err.Error(),
7575
}
7676
}
@@ -102,8 +102,8 @@ func (c *Client) StatObject(ctx context.Context, bucketName, objectName string,
102102
if resp.StatusCode == http.StatusMethodNotAllowed && opts.VersionID != "" && deleteMarker {
103103
errResp := ErrorResponse{
104104
StatusCode: resp.StatusCode,
105-
Code: "MethodNotAllowed",
106-
Message: "The specified method is not allowed against this resource.",
105+
Code: MethodNotAllowed,
106+
Message: s3ErrorResponseMap[MethodNotAllowed],
107107
BucketName: bucketName,
108108
Key: objectName,
109109
}

0 commit comments

Comments
 (0)