Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 41 additions & 17 deletions src/hb_http.erl
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,34 @@ request(Method, Peer, Path, RawMessage, Opts) ->
),
StartTime = os:system_time(millisecond),
% Perform the HTTP request.
{_ErlStatus, Status, Headers, Body} = hb_http_client:request(Req, Opts),
Response = hb_http_client:request(Req, Opts),
% Process the response.
EndTime = os:system_time(millisecond),
?event(http_outbound,
{
http_response,
{req, Req},
{response,
#{
status => Status,
headers => Headers,
body => Body
}
}
},
Opts
),
Duration = EndTime - StartTime,
case Response of
{_ErlStatus, Status, Headers, Body} ->
?event(http_outbound,
{
http_response,
{req, Req},
{response,
#{
status => Status,
headers => Headers,
body => Body
}
}
},
Opts
),
request_response(Method, Peer, Path, Response, Duration, Opts);
Error ->
Error
end.


request_response(Method, Peer, Path, Response, Duration, Opts) ->
{_ErlStatus, Status, Headers, Body} = Response,
% Convert the set-cookie headers into a cookie message, if they are present.
% We do this by extracting the set-cookie headers and converting them into a
% cookie message if they are present.
Expand Down Expand Up @@ -161,7 +172,7 @@ request(Method, Peer, Path, RawMessage, Opts) ->
?event(http_short,
{received,
{status, Status},
{duration, EndTime - StartTime},
{duration, Duration},
{method, Method},
{peer, Peer},
{path, {string, Path}},
Expand Down Expand Up @@ -1308,4 +1319,17 @@ parallel_request_test() ->
#{<<"path">> => <<"/BOogk_XAI3bvNWnxNxwxmvOfglZt17o4MOVAdPNZ_ew">>},
Opts
)
).
).

request_error_handling_test() ->
Opts = #{},
NonExistentDomain = <<"http://nonexistent.invalid:80">>,
Result = hb_http:request(
<<"GET">>,
NonExistentDomain,
<<"/">>,
#{},
Opts
),
% The result should be an error tuple, not crash with badmatch
?assertMatch({error, _}, Result).