Skip to content

Commit

Permalink
Parse request_id in XML handler (#2716)
Browse files Browse the repository at this point in the history
  • Loading branch information
mullermp authored Jun 20, 2022
1 parent d686636 commit 2b09dc7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
2 changes: 2 additions & 0 deletions gems/aws-sdk-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Unreleased Changes
------------------

* Issue - Populate context :request_id for XML error responses.

3.131.1 (2022-05-20)
------------------

Expand Down
2 changes: 1 addition & 1 deletion gems/aws-sdk-core/lib/aws-sdk-core/rest/handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def call(context)

def apply_request_id(context)
h = context.http_response.headers
context[:request_id] = h['x-amz-request-id'] || h['x-amzn-requestid']
context[:request_id] ||= h['x-amz-request-id'] || h['x-amzn-requestid']
end

end
Expand Down
7 changes: 7 additions & 0 deletions gems/aws-sdk-core/lib/aws-sdk-core/xml/error_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def error(context)
else
code, message, data = extract_error(body, context)
end
context[:request_id] = request_id(body)
errors_module = context.client.class.errors_module
error_class = errors_module.error_class(code).new(context, message, data)
error_class
Expand Down Expand Up @@ -94,6 +95,12 @@ def error_message(body)
end
end

def request_id(body)
if matches = body.match(/<RequestId>(.+?)<\/RequestId>/m)
matches[1]
end
end

def unescape(str)
CGI.unescapeHTML(str)
end
Expand Down
8 changes: 6 additions & 2 deletions gems/aws-sdk-core/spec/aws/xml/error_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module Xml
<Message>Required parameter 'AvailabilityZone' is not provided.</Message>
</Error>
</Errors>
<RequestID>549b7a4f-088e-41f1-8db0-905f3c3e0e03</RequestID>
<RequestId>549b7a4f-088e-41f1-8db0-905f3c3e0e03</RequestId>
</Response>
XML

Expand All @@ -73,7 +73,7 @@ module Xml

let(:unmodeled_error) { <<-XML.strip }
<?xml version="1.0"?>
<ErrorResponse xmlns="http://cloudfront.amazonaws.com/doc/2016-09-07/">
<ErrorResponse xmlns="http://cloudfront.amazonaws.com/doc/2018-11-05/">
<Error>
<Type>Sender</Type>
<Code>MalformedXML</Code>
Expand Down Expand Up @@ -114,6 +114,7 @@ module Xml
}.to raise_error { |error|
expect(error.code).to eq('AccessDenied')
expect(error.message).to eq('foo')
expect(error.context[:request_id]).to eq('b25f48e8-84fd-11e6-80d9-574e0c4664cb')
}
end

Expand All @@ -133,6 +134,7 @@ module Xml
}.to raise_error { |error|
expect(error.code).to eq('MalformedXML')
expect(error.message).to eq(msg)
expect(error.context[:request_id]).to eq('b25f48e8-84fd-11e6-80d9-574e0c4664cb')
}
end

Expand All @@ -157,6 +159,7 @@ module Xml
}.to raise_error { |error|
expect(error.code).to eq('InvalidRequest')
expect(error.message).to eq('Required parameter \'AvailabilityZone\' is not provided.')
expect(error.context[:request_id]).to eq('549b7a4f-088e-41f1-8db0-905f3c3e0e03')
}
end

Expand All @@ -168,6 +171,7 @@ module Xml
}.to raise_error { |error|
expect(error.code).to eq('InvalidParameter')
expect(error.message).to eq('Invalid parameter: TopicArn Reason: no value for required parameter')
expect(error.context[:request_id]).to eq('e9e9cfe5-9023-5f6f-a9fc-9f817a196bcd')
}
end
end
Expand Down

0 comments on commit 2b09dc7

Please sign in to comment.