From 7660c875630f0b6522c0b857e8dc6dd060219156 Mon Sep 17 00:00:00 2001 From: v0idpwn Date: Mon, 15 Apr 2024 17:17:48 -0300 Subject: [PATCH 1/3] Return ArgumentError when missing `:database` key in config Sample message printed by DbConnection: [error] Postgrex.Protocol (#PID<0.387.0>) failed to connect: ** (ArgumentError) missing the :database key in options for repo TestEctoIssue.Repo --- lib/postgrex/protocol.ex | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/postgrex/protocol.ex b/lib/postgrex/protocol.ex index 78162427..28dfbb48 100644 --- a/lib/postgrex/protocol.ex +++ b/lib/postgrex/protocol.ex @@ -170,15 +170,12 @@ defmodule Postgrex.Protocol do %{opts: opts, types_mod: types_mod} = status, previous_errors ) do - types_key = if types_mod, do: {host, port, Keyword.fetch!(opts, :database)} - opts = Config.Reader.merge(opts, extra_opts) - - status = %{status | types_key: types_key, opts: opts} - - case connect_and_handshake(host, port, sock_opts, timeout, s, status) do - {:ok, _} = ret -> - ret - + with {:ok, database} <- fetch_database(opts), + opts = Config.Reader.merge(opts, extra_opts), + status = %{status | types_key: if(types_mod, do: {host, port, database}), opts: opts}, + {:ok, ret} <- connect_and_handshake(host, port, sock_opts, timeout, s, status) do + {:ok, ret} + else {:error, err} -> connect_endpoints( remaining_endpoints, @@ -205,6 +202,19 @@ defmodule Postgrex.Protocol do {:error, %Postgrex.Error{message: message}} end + defp fetch_database(opts) do + case Keyword.fetch(opts, :database) do + {:ok, value} -> + value + + :error -> + {:error, + %ArgumentError{ + message: "missing the :database key in options for #{inspect(opts[:repo])}" + }} + end + end + defp connect_and_handshake(host, port, sock_opts, timeout, s, status) do case connect(host, port, sock_opts, timeout, s) do {:ok, s} -> From 1630fd40ee9196ee58a537bc001a7086a9599d28 Mon Sep 17 00:00:00 2001 From: v0idpwn Date: Mon, 15 Apr 2024 17:23:27 -0300 Subject: [PATCH 2/3] Better formatting --- lib/postgrex/protocol.ex | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/postgrex/protocol.ex b/lib/postgrex/protocol.ex index 28dfbb48..a9dfc12f 100644 --- a/lib/postgrex/protocol.ex +++ b/lib/postgrex/protocol.ex @@ -208,10 +208,8 @@ defmodule Postgrex.Protocol do value :error -> - {:error, - %ArgumentError{ - message: "missing the :database key in options for #{inspect(opts[:repo])}" - }} + message = "missing the :database key in options for #{inspect(opts[:repo])}" + {:error, %ArgumentError{message: message}} end end From 631fb0948ea14fbbbcb29c4448113e6b5f0e7fc2 Mon Sep 17 00:00:00 2001 From: v0idpwn Date: Mon, 15 Apr 2024 17:30:44 -0300 Subject: [PATCH 3/3] fix --- lib/postgrex/protocol.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/postgrex/protocol.ex b/lib/postgrex/protocol.ex index a9dfc12f..92bbeeb6 100644 --- a/lib/postgrex/protocol.ex +++ b/lib/postgrex/protocol.ex @@ -205,7 +205,7 @@ defmodule Postgrex.Protocol do defp fetch_database(opts) do case Keyword.fetch(opts, :database) do {:ok, value} -> - value + {:ok, value} :error -> message = "missing the :database key in options for #{inspect(opts[:repo])}"