Skip to content

Commit 8ed1bb9

Browse files
authored
Handle S3 200 errors for all operations #2 (#3040)
1 parent 6d9d5eb commit 8ed1bb9

File tree

3 files changed

+117
-96
lines changed

3 files changed

+117
-96
lines changed

gems/aws-sdk-s3/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Unreleased Changes
22
------------------
33

4+
* Issue - Handle 200 errors for all S3 operations that do not have streaming responses.
5+
46
1.152.0 (2024-06-05)
57
------------------
68

gems/aws-sdk-s3/lib/aws-sdk-s3/plugins/http_200_errors.rb

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,40 @@ class Handler < Seahorse::Client::Handler
1515

1616
def call(context)
1717
@handler.call(context).on(200) do |response|
18-
if error = check_for_error(context)
19-
context.http_response.status_code = 500
20-
response.data = nil
21-
response.error = error
22-
end
18+
return response if streaming_output?(context.operation.output)
19+
20+
error = check_for_error(context)
21+
return response unless error
22+
23+
context.http_response.status_code = 500
24+
response.data = nil
25+
response.error = error
26+
end
27+
end
28+
29+
private
30+
31+
def streaming_output?(output)
32+
if (payload = output[:payload_member]) # checking ref and shape
33+
payload['streaming'] || payload.shape['streaming'] ||
34+
payload.eventstream
35+
else
36+
false
2337
end
2438
end
2539

40+
def members_in_body?(output)
41+
output.shape.members.any? { |_, k| k.location.nil? }
42+
end
43+
2644
def check_for_error(context)
2745
xml = context.http_response.body_contents
28-
if xml.match(/<Error>/)
46+
if xml.match(/\?>\s*<Error>/)
2947
error_code = xml.match(/<Code>(.+?)<\/Code>/)[1]
3048
error_message = xml.match(/<Message>(.+?)<\/Message>/)[1]
3149
S3::Errors.error_class(error_code).new(context, error_message)
32-
elsif !xml.match(/<\w/) # Must have the start of an XML Tag
50+
elsif members_in_body?(context.operation.output) && !xml.match(/<\w/)
51+
# Must have a body member and have the start of an XML Tag
3352
# Other incomplete xml bodies will result in XML ParsingError
3453
Seahorse::Client::NetworkingError.new(
3554
S3::Errors
@@ -40,15 +59,7 @@ def check_for_error(context)
4059
end
4160
end
4261

43-
handler(
44-
Handler,
45-
step: :sign,
46-
operations: [
47-
:complete_multipart_upload,
48-
:copy_object,
49-
:upload_part_copy,
50-
]
51-
)
62+
handler(Handler, step: :sign)
5263
end
5364
end
5465
end

gems/aws-sdk-s3/spec/client_spec.rb

Lines changed: 88 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,16 @@ module S3
4545
'rw0nS41rawnLDzkf+PKXmmt/uEi4bzvNMr72o=',
4646
'x-amz-request-id' => 'BE9C18E622969B17'
4747
},
48-
body: ''
49-
)
48+
body: <<-XML)
49+
<?xml version="1.0" encoding="UTF-8"?>
50+
<ListAllMyBucketsResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">
51+
<Buckets>
52+
<Bucket>
53+
<Name>aws-sdk-ruby</Name>
54+
</Bucket>
55+
</Buckets>
56+
</ListAllMyBucketsResult>
57+
XML
5058
Seahorse::Client::Response.new(context: context)
5159
end
5260
resp = s3.list_buckets
@@ -423,14 +431,14 @@ module S3
423431
status_code: 409,
424432
headers: {},
425433
body: <<-XML.strip
426-
<?xml version="1.0" encoding="UTF-8"?>
427-
<Error>
428-
<Code>BucketNotEmpty</Code>
429-
<Message>The bucket you tried to delete is not empty</Message>
430-
<BucketName>aws-sdk</BucketName>
431-
<RequestId>740BE6AB575EACED</RequestId>
432-
<HostId>MQVg1RMI+d93Hps1E8qpIuDb9Gd2TzkDhm0wE40981DjxSHP1UfLBB7qOAlwAqJB</HostId>
433-
</Error>
434+
<?xml version="1.0" encoding="UTF-8"?>
435+
<Error>
436+
<Code>BucketNotEmpty</Code>
437+
<Message>The bucket you tried to delete is not empty</Message>
438+
<BucketName>aws-sdk</BucketName>
439+
<RequestId>740BE6AB575EACED</RequestId>
440+
<HostId>MQVg1RMI+d93Hps1E8qpIuDb9Gd2TzkDhm0wE40981DjxSHP1UfLBB7qOAlwAqJB</HostId>
441+
</Error>
434442
XML
435443
)
436444
Seahorse::Client::Response.new(context: context)
@@ -452,8 +460,8 @@ module S3
452460
status_code: 200,
453461
headers: {},
454462
body: <<-XML.strip
455-
<?xml version="1.0" encoding="UTF-8"?>
456-
<LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/">EU</LocationConstraint>
463+
<?xml version="1.0" encoding="UTF-8"?>
464+
<LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/">EU</LocationConstraint>
457465
XML
458466
)
459467
Seahorse::Client::Response.new(context: context)
@@ -469,8 +477,8 @@ module S3
469477
status_code: 200,
470478
headers: {},
471479
body: <<-XML.strip
472-
<?xml version="1.0" encoding="UTF-8"?>
473-
<LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/"/>
480+
<?xml version="1.0" encoding="UTF-8"?>
481+
<LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/"/>
474482
XML
475483
)
476484
Seahorse::Client::Response.new(context: context)
@@ -504,19 +512,19 @@ module S3
504512
status_code: 200,
505513
headers: {},
506514
body: <<-XML.strip)
507-
<?xml version="1.0" encoding="UTF-8"?>
508-
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
509-
<Prefix>a%26</Prefix>
510-
<Delimiter>b%26</Delimiter>
511-
<Marker>c%26</Marker>
512-
<NextMarker>d%26</NextMarker>
513-
<Contents>
514-
<Key>e%26</Key>
515-
</Contents>
516-
<CommonPrefixes>
517-
<Prefix>f%26</Prefix>
518-
</CommonPrefixes>
519-
</ListBucketResult>
515+
<?xml version="1.0" encoding="UTF-8"?>
516+
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
517+
<Prefix>a%26</Prefix>
518+
<Delimiter>b%26</Delimiter>
519+
<Marker>c%26</Marker>
520+
<NextMarker>d%26</NextMarker>
521+
<Contents>
522+
<Key>e%26</Key>
523+
</Contents>
524+
<CommonPrefixes>
525+
<Prefix>f%26</Prefix>
526+
</CommonPrefixes>
527+
</ListBucketResult>
520528
XML
521529
Seahorse::Client::Response.new(context: context)
522530
end
@@ -541,12 +549,12 @@ module S3
541549
status_code: 200,
542550
headers: {},
543551
body: <<-XML.strip)
544-
<?xml version="1.0" encoding="UTF-8"?>
545-
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
546-
<Contents>
547-
<Key>a%26</Key>
548-
</Contents>
549-
</ListBucketResult>
552+
<?xml version="1.0" encoding="UTF-8"?>
553+
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
554+
<Contents>
555+
<Key>a%26</Key>
556+
</Contents>
557+
</ListBucketResult>
550558
XML
551559
Seahorse::Client::Response.new(context: context)
552560
end
@@ -562,22 +570,22 @@ module S3
562570
status_code: 200,
563571
headers: {},
564572
body: <<-XML.strip)
565-
<?xml version="1.0" encoding="UTF-8"?>
566-
<ListVersionsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01">
567-
<Prefix>a%26</Prefix>
568-
<Delimiter>b%26</Delimiter>
569-
<KeyMarker>c%26</KeyMarker>
570-
<NextKeyMarker>d%26</NextKeyMarker>
571-
<Version>
572-
<Key>e%26</Key>
573-
</Version>
574-
<DeleteMarker>
575-
<Key>f%26</Key>
576-
</DeleteMarker>
577-
<CommonPrefixes>
578-
<Prefix>g%26</Prefix>
579-
</CommonPrefixes>
580-
</ListVersionsResult>
573+
<?xml version="1.0" encoding="UTF-8"?>
574+
<ListVersionsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01">
575+
<Prefix>a%26</Prefix>
576+
<Delimiter>b%26</Delimiter>
577+
<KeyMarker>c%26</KeyMarker>
578+
<NextKeyMarker>d%26</NextKeyMarker>
579+
<Version>
580+
<Key>e%26</Key>
581+
</Version>
582+
<DeleteMarker>
583+
<Key>f%26</Key>
584+
</DeleteMarker>
585+
<CommonPrefixes>
586+
<Prefix>g%26</Prefix>
587+
</CommonPrefixes>
588+
</ListVersionsResult>
581589
XML
582590
Seahorse::Client::Response.new(context: context)
583591
end
@@ -602,19 +610,19 @@ module S3
602610
status_code: 200,
603611
headers: {},
604612
body: <<-XML.strip)
605-
<?xml version="1.0" encoding="UTF-8"?>
606-
<ListVersionsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01">
607-
<Prefix>a%26</Prefix>
608-
<Delimiter>b%26</Delimiter>
609-
<KeyMarker>c%26</KeyMarker>
610-
<NextKeyMarker>d%26</NextKeyMarker>
611-
<Upload>
612-
<Key>e%26</Key>
613-
</Upload>
614-
<CommonPrefixes>
615-
<Prefix>f%26</Prefix>
616-
</CommonPrefixes>
617-
</ListVersionsResult>
613+
<?xml version="1.0" encoding="UTF-8"?>
614+
<ListVersionsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01">
615+
<Prefix>a%26</Prefix>
616+
<Delimiter>b%26</Delimiter>
617+
<KeyMarker>c%26</KeyMarker>
618+
<NextKeyMarker>d%26</NextKeyMarker>
619+
<Upload>
620+
<Key>e%26</Key>
621+
</Upload>
622+
<CommonPrefixes>
623+
<Prefix>f%26</Prefix>
624+
</CommonPrefixes>
625+
</ListVersionsResult>
618626
XML
619627
Seahorse::Client::Response.new(context: context)
620628
end
@@ -686,17 +694,17 @@ module S3
686694
status_code: 200,
687695
headers: {},
688696
body: <<-XML)
689-
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
690-
<Contents>
691-
<Key>prefix+suffix</Key>
692-
</Contents>
693-
<Contents>
694-
<Key>prefix%2Bsuffix</Key>
695-
</Contents>
696-
<Contents>
697-
<Key>prefix%20suffix</Key>
698-
</Contents>
699-
</ListBucketResult>
697+
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
698+
<Contents>
699+
<Key>prefix+suffix</Key>
700+
</Contents>
701+
<Contents>
702+
<Key>prefix%2Bsuffix</Key>
703+
</Contents>
704+
<Contents>
705+
<Key>prefix%20suffix</Key>
706+
</Contents>
707+
</ListBucketResult>
700708
XML
701709
Seahorse::Client::Response.new(context: context)
702710
end
@@ -781,13 +789,13 @@ module S3
781789
client.handle(step: :send) do |context|
782790
context.http_response.signal_headers(200, {})
783791
context.http_response.signal_data(<<-XML.strip)
784-
<?xml version="1.0" encoding="UTF-8"?>
785-
<Error>
786-
<Code>InternalError</Code>
787-
<Message>We encountered an internal error. Please try again.</Message>
788-
<RequestId>656c76696e6727732072657175657374</RequestId>
789-
<HostId>Uuag1LuByRx9e6j5Onimru9pO4ZVKnJ2Qz7/C1NPcfTWAtRPfTaOFg==</HostId>
790-
</Error>
792+
<?xml version="1.0" encoding="UTF-8"?>
793+
<Error>
794+
<Code>InternalError</Code>
795+
<Message>We encountered an internal error. Please try again.</Message>
796+
<RequestId>656c76696e6727732072657175657374</RequestId>
797+
<HostId>Uuag1LuByRx9e6j5Onimru9pO4ZVKnJ2Qz7/C1NPcfTWAtRPfTaOFg==</HostId>
798+
</Error>
791799
XML
792800
context.http_response.signal_done
793801
Seahorse::Client::Response.new(context: context)

0 commit comments

Comments
 (0)