Skip to content

Commit

Permalink
fix: handle httpc errors
Browse files Browse the repository at this point in the history
  • Loading branch information
thalesmg committed May 9, 2024
1 parent 77d8b04 commit ef7f81a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/erlazure.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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) ->
Expand Down Expand Up @@ -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) ->
Expand Down
13 changes: 13 additions & 0 deletions test/erlazure_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit ef7f81a

Please sign in to comment.