Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit 7cb6cf9

Browse files
authored
service/s3: Update BucketRegionError message to include more information (#2451)
Updates the BucketRegionError error message to include information about the endpoint and actual region the bucket is in if known. This error message is created by the SDK, but could produce a confusing error message if the user provided a region that doesn't match the endpoint. Fix #2426
1 parent 3f2533c commit 7cb6cf9

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

service/s3/unmarshal_error.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,16 @@ func unmarshalError(r *request.Request) {
2626
// Bucket exists in a different region, and request needs
2727
// to be made to the correct region.
2828
if r.HTTPResponse.StatusCode == http.StatusMovedPermanently {
29+
msg := fmt.Sprintf(
30+
"incorrect region, the bucket is not in '%s' region at endpoint '%s'",
31+
aws.StringValue(r.Config.Region),
32+
aws.StringValue(r.Config.Endpoint),
33+
)
34+
if v := r.HTTPResponse.Header.Get("x-amz-bucket-region"); len(v) != 0 {
35+
msg += fmt.Sprintf(", bucket is in '%s' region", v)
36+
}
2937
r.Error = awserr.NewRequestFailure(
30-
awserr.New("BucketRegionError",
31-
fmt.Sprintf("incorrect region, the bucket is not in '%s' region",
32-
aws.StringValue(r.Config.Region)),
33-
nil),
38+
awserr.New("BucketRegionError", msg, nil),
3439
r.HTTPResponse.StatusCode,
3540
r.RequestID,
3641
)

service/s3/unmarshal_error_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func TestUnmarshalError(t *testing.T) {
162162
if e, a := c.Code, err.(awserr.Error).Code(); e != a {
163163
t.Errorf("%d, Code: expect %s, got %s", i, e, a)
164164
}
165-
if e, a := c.Msg, err.(awserr.Error).Message(); e != a {
165+
if e, a := c.Msg, err.(awserr.Error).Message(); !strings.Contains(a, e) {
166166
t.Errorf("%d, Message: expect %s, got %s", i, e, a)
167167
}
168168
if e, a := c.ReqID, err.(awserr.RequestFailure).RequestID(); e != a {

0 commit comments

Comments
 (0)