Skip to content

Commit a1a22b2

Browse files
committed
Log response body of failed AWS requests
1 parent 49a4758 commit a1a22b2

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

deps/rabbitmq_aws/src/rabbitmq_aws.erl

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -546,19 +546,24 @@ api_get_request(Service, Path) ->
546546
api_get_request_with_retries(Service, Path, ?MAX_RETRIES, ?LINEAR_BACK_OFF_MILLIS).
547547

548548

549-
-spec api_get_request_with_retries(string(), path(), integer(), integer()) -> result().
550-
%% @doc Invoke an API call to an AWS service with retries.
551-
%% @end
549+
-spec api_get_request_with_retries(string(), path(), integer(), integer()) -> result().
550+
%% @doc Invoke an API call to an AWS service with retries.
551+
%% @end
552552
api_get_request_with_retries(_, _, 0, _) ->
553553
rabbit_log:warning("Request to AWS service has failed after ~b retries", [?MAX_RETRIES]),
554554
{error, "AWS service is unavailable"};
555555
api_get_request_with_retries(Service, Path, Retries, WaitTimeBetweenRetries) ->
556556
ensure_credentials_valid(),
557557
case get(Service, Path) of
558-
{ok, {_Headers, Payload}} -> rabbit_log:debug("AWS request: ~s~nResponse: ~p", [Path, Payload]),
559-
{ok, Payload};
560-
{error, {credentials, _}} -> {error, credentials};
561-
{error, Message, _} -> rabbit_log:warning("Error occurred ~s~nWill retry AWS request, remaining retries: ~b", [Message, Retries]),
562-
timer:sleep(WaitTimeBetweenRetries),
563-
api_get_request_with_retries(Service, Path, Retries - 1, WaitTimeBetweenRetries)
558+
{ok, {_Headers, Payload}} -> rabbit_log:debug("AWS request: ~s~nResponse: ~p", [Path, Payload]),
559+
{ok, Payload};
560+
{error, {credentials, _}} -> {error, credentials};
561+
{error, Message, Response} -> rabbit_log:warning("Error occurred: ~s", [Message]),
562+
case Response of
563+
{_, Payload} -> rabbit_log:warning("Failed AWS request: ~s~nResponse: ~p", [Path, Payload]);
564+
_ -> ok
565+
end,
566+
rabbit_log:warning("Will retry AWS request, remaining retries: ~b", [Retries]),
567+
timer:sleep(WaitTimeBetweenRetries),
568+
api_get_request_with_retries(Service, Path, Retries - 1, WaitTimeBetweenRetries)
564569
end.

deps/rabbitmq_aws/test/src/rabbitmq_aws_tests.erl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ api_get_request_test_() ->
485485
secret_access_key = "ExpiredAccessKey",
486486
region = "us-east-1",
487487
expiration = {{3016, 4, 1}, {12, 0, 0}}},
488-
meck:expect(httpc, request, 4, {error, "invalid input"}),
488+
meck:expect(httpc, request, 4, {error, "network error"}),
489489
{ok, Pid} = rabbitmq_aws:start_link(),
490490
rabbitmq_aws:set_region("us-east-1"),
491491
rabbitmq_aws:set_credentials(State),
@@ -501,7 +501,11 @@ api_get_request_test_() ->
501501
secret_access_key = "ExpiredAccessKey",
502502
region = "us-east-1",
503503
expiration = {{3016, 4, 1}, {12, 0, 0}}},
504-
meck:expect(httpc, request, 4, meck:seq([{error, "invalid input"}, {ok, {{"HTTP/1.0", 200, "OK"}, [{"content-type", "application/json"}], "{\"data\": \"value\"}"}}])),
504+
meck:expect(httpc, request, 4, meck:seq([
505+
{error, "network error"},
506+
{ok, {{"HTTP/1.0", 500, "OK"}, [{"content-type", "application/json"}], "{\"error\": \"server error\"}"}},
507+
{ok, {{"HTTP/1.0", 200, "OK"}, [{"content-type", "application/json"}], "{\"data\": \"value\"}"}}
508+
])),
505509
{ok, Pid} = rabbitmq_aws:start_link(),
506510
rabbitmq_aws:set_region("us-east-1"),
507511
rabbitmq_aws:set_credentials(State),

0 commit comments

Comments
 (0)