From 1cc3b32b8ef6c187b8be3601319e21c1ba04fa27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20S=C3=B6derqvist?= Date: Mon, 24 Oct 2022 14:09:59 +0200 Subject: Handle send errors --- src/gun.erl | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'src/gun.erl') diff --git a/src/gun.erl b/src/gun.erl index bfb1bc4..4108b93 100644 --- a/src/gun.erl +++ b/src/gun.erl @@ -1237,17 +1237,25 @@ connected_ws_only(Type, Event, State) -> connected(internal, {connected, Socket, NewProtocol}, State0=#state{owner=Owner, opts=Opts, transport=Transport}) -> {Protocol, ProtoOpts} = gun_protocols:handler_and_opts(NewProtocol, Opts), - %% @todo Handle error result from Protocol:init/4 - {ok, StateName, ProtoState} = Protocol:init(Owner, Socket, Transport, ProtoOpts), - Owner ! {gun_up, self(), Protocol:name()}, - case active(State0#state{socket=Socket, protocol=Protocol, protocol_state=ProtoState}) of - {ok, State} -> - case Protocol:has_keepalive() of - true -> {next_state, StateName, keepalive_timeout(State)}; - false -> {next_state, StateName, State} - end; - Disconnect -> - Disconnect + case Protocol:init(Owner, Socket, Transport, ProtoOpts) of + Error={error, _} -> + %% @todo Don't send gun_up and gun_down if Protocol:init/4 failes here. + Owner ! {gun_up, self(), Protocol:name()}, + disconnect(State0, Error); + {ok, StateName, ProtoState} -> + %% @todo Don't send gun_up and gun_down if active/1 failes here. + Owner ! {gun_up, self(), Protocol:name()}, + State1 = State0#state{socket=Socket, protocol=Protocol, protocol_state=ProtoState}, + case active(State1) of + {ok, State2} -> + State = case Protocol:has_keepalive() of + true -> keepalive_timeout(State2); + false -> State2 + end, + {next_state, StateName, State}; + Disconnect -> + Disconnect + end end; %% Public HTTP interface. %% -- cgit v1.2.3