LoginSignup
0
0

More than 5 years have passed since last update.

AWS SDK for Ruby (V2) でエラー発生時に生の HTTP リクエスト・レスポンスを確認する

Posted at

あまり必要ないかもだけど、やってたらできたのでメモしておく。

標準のログ出力だとできない

こんな感じで logger/log_level を指定しても、

require "aws-sdk"

ENV["AWS_ACCESS_KEY_ID"] = "test"
ENV["AWS_SECRET_ACCESS_KEY"] = "test"
ENV["AWS_REGION"] = "ap-northeast-1"

client = Aws::S3::Client.new(logger: Logger.new(STDOUT), log_level: :debug)
client.list_buckets

こんな出力になるだけ。

D, [2017-02-12T23:01:43.918174 #191] DEBUG -- : [Aws::S3::Client 403 0.027187 0 retries] list_buckets() Aws::S3::Errors::InvalidAccessKeyId The AWS Access Key Id you provided does not exist in our records.

エラーオブジェクトのコンテキストを参照するとよい

このようにエラーオブジェクトのコンテキストを p すると、

require "aws-sdk"

ENV["AWS_ACCESS_KEY_ID"] = "test"
ENV["AWS_SECRET_ACCESS_KEY"] = "test"
ENV["AWS_REGION"] = "ap-northeast-1"

begin
  Aws::S3::Client.new.list_buckets
rescue => e
  p e.context.http_request
  p e.context.http_request.body.read
  p e.context.http_response
  p e.context.http_response.body.read
end

このように生の HTTP リクエスト・レスポンスオブジェクトを調査することができる。

#<Seahorse::Client::Http::Request:0x00000001e67b10 @endpoint=#<URI::HTTPS https://s3-ap-northeast-1.amazonaws.com/>, @http_method="GET", @headers={"user-agent"=>"aws-sdk-ruby2/2.7.7 ruby/2.4.0 x86_64-linux", "x-amz-date"=>"20170212T230508Z", "host"=>"s3-ap-northeast-1.amazonaws.com", "x-amz-content-sha256"=>"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "authorization"=>"AWS4-HMAC-SHA256 Credential=test/20170212/ap-northeast-1/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=af202c352b983e16265c91eb435cf10044f8c0a7da511f61e2a4f7c3132a3a79", "content-length"=>"0"}, @body=#<StringIO:0x00000001cedc08>>
""
#<Seahorse::Client::Http::Response:0x00000001e58e80 @status_code=403, @headers={"x-amz-request-id"=>"73A1F598850BFE18", "x-amz-id-2"=>"FiIfC0BDsbYddCrVVMkrJV+9Toy9/cbq5XbD6xdmwJ9WnTnMaOxfjYkoaf/la6CAhyCeLoh34Vs=", "content-type"=>"application/xml", "transfer-encoding"=>"chunked", "date"=>"Sun, 12 Feb 2017 23:05:07 GMT", "server"=>"AmazonS3"}, @body=#<StringIO:0x00000001e58db8>, @listeners={:headers=>[], :data=>[], :done=>[]}, @complete=false, @done=true, @error=nil>
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>InvalidAccessKeyId</Code><Message>The AWS Access Key Id you provided does not exist in our records.</Message><AWSAccessKeyId>test</AWSAccessKeyId><RequestId>73A1F598850BFE18</RequestId><HostId>FiIfC0BDsbYddCrVVMkrJV+9Toy9/cbq5XbD6xdmwJ9WnTnMaOxfjYkoaf/la6CAhyCeLoh34Vs=</HostId></Error>"

参考資料

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0