From ef7f81afb3e778dd3820747cc1dfcfad0de3158f Mon Sep 17 00:00:00 2001 From: Thales Macedo Garitezi Date: Thu, 9 May 2024 15:56:31 -0300 Subject: [PATCH] fix: handle httpc errors --- src/erlazure.erl | 15 +++++++++++---- test/erlazure_SUITE.erl | 13 +++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/erlazure.erl b/src/erlazure.erl index ae76ccf..1ad6d47 100644 --- a/src/erlazure.erl +++ b/src/erlazure.erl @@ -497,9 +497,13 @@ handle_call({list_containers, Options}, _From, State) -> ReqOptions = [{params, [{comp, list}] ++ Options}], ReqContext = new_req_context(?blob_service, ReqOptions, State), - {?http_ok, Body} = execute_request(ServiceContext, ReqContext), - {ok, Containers} = erlazure_blob:parse_container_list(Body), - {reply, Containers, State}; + case execute_request(ServiceContext, ReqContext) of + {?http_ok, Body} -> + {ok, Containers} = erlazure_blob:parse_container_list(Body), + {reply, Containers, State}; + {error, _} = Error -> + {reply, Error, State} + end; % Create a container handle_call({create_container, Name, Options}, _From, State) -> @@ -805,7 +809,10 @@ execute_request(ServiceContext = #service_context{}, ReqContext = #req_context{} {Code, Body}; {ok, {{_, _, _}, _, Body}} -> - get_error_code(Body) + get_error_code(Body); + + {error, Error} -> + {error, Error} end. get_error_code(Body) -> diff --git a/test/erlazure_SUITE.erl b/test/erlazure_SUITE.erl index 829dc1c..5cb4b2c 100644 --- a/test/erlazure_SUITE.erl +++ b/test/erlazure_SUITE.erl @@ -149,3 +149,16 @@ t_append_blob_smoke_test(Config) -> %% Delete container ?assertMatch({ok, deleted}, erlazure:delete_container(Pid, Container)), ok. + +%% Test error handling when endpoint is unavailable +t_blob_failure_to_connect(_Config) -> + BadEndpoint = "http://127.0.0.2:65535/", + {ok, Pid} = erlazure:start(#{account => ?ACCOUNT, key => ?KEY, endpoint => BadEndpoint}), + ?assertMatch({error, {failed_connect, _}}, erlazure:list_containers(Pid)), + ?assertMatch({error, {failed_connect, _}}, erlazure:create_container(Pid, "c")), + ?assertMatch({error, {failed_connect, _}}, erlazure:delete_container(Pid, "c")), + ?assertMatch({error, {failed_connect, _}}, erlazure:put_append_blob(Pid, "c", "b1")), + ?assertMatch({error, {failed_connect, _}}, erlazure:put_block_blob(Pid, "c", "b1", <<"a">>)), + ?assertMatch({error, {failed_connect, _}}, erlazure:append_block(Pid, "c", "b1", <<"a">>)), + ?assertMatch({error, {failed_connect, _}}, erlazure:get_blob(Pid, "c", "b1")), + ok.