Skip to content

Commit 35b19a2

Browse files
authored
Always return ok or error tuple on connect (#178)
1 parent 4a3418c commit 35b19a2

1 file changed

Lines changed: 16 additions & 16 deletions

File tree

lib/tds/protocol.ex

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -364,19 +364,22 @@ defmodule Tds.Protocol do
364364
s = %{s | opts: opts}
365365

366366
# Initalize TCP connection with the SQL Server
367-
with {:ok, sock} <- :gen_tcp.connect(host, port, sock_opts, timeout),
368-
{:ok, buffers} <- :inet.getopts(sock, [:sndbuf, :recbuf, :buffer]),
369-
:ok <- :inet.setopts(sock, buffer: max_buf_size(buffers)) do
370-
# Send Prelogin message to SQL Server
371-
case send_prelogin(%{s | sock: {:gen_tcp, sock}}) do
372-
{:error, error, _state} ->
373-
:gen_tcp.close(sock)
374-
{:error, error}
375-
376-
other ->
377-
other
378-
end
379-
else
367+
case :gen_tcp.connect(host, port, sock_opts, timeout) do
368+
{:ok, sock} ->
369+
with {:ok, buffers} <- :inet.getopts(sock, [:sndbuf, :recbuf, :buffer]),
370+
:ok <- :inet.setopts(sock, buffer: max_buf_size(buffers)),
371+
{:ok, s} <- send_prelogin(%{s | sock: {:gen_tcp, sock}}) do
372+
{:ok, s}
373+
else
374+
{_error_or_disconnect, exception, _state} ->
375+
:gen_tcp.close(sock)
376+
{:error, exception}
377+
378+
{:error, error} ->
379+
:gen_tcp.close(sock)
380+
{:error, %Tds.Error{message: "tcp connect: #{error}"}}
381+
end
382+
380383
{:error, error} ->
381384
{:error, %Tds.Error{message: "tcp connect: #{error}"}}
382385
end
@@ -851,9 +854,6 @@ defmodule Tds.Protocol do
851854
buffer
852855
|> IO.iodata_to_binary()
853856
|> decode(s)
854-
else
855-
{:disconnect, _ex, _s} = res -> {0, res}
856-
other -> other
857857
end
858858
end
859859

0 commit comments

Comments
 (0)