Skip to content

Commit

Permalink
Raise a nice error message of params is not a list (#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-rychlewski authored Nov 6, 2024
1 parent fb9992e commit 66325e4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/postgrex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ defmodule Postgrex do
"""
@spec query(conn, iodata, list, [execute_option]) ::
{:ok, Postgrex.Result.t()} | {:error, Exception.t()}
def query(conn, statement, params, opts \\ []) do
def query(conn, statement, params, opts \\ []) when is_list(params) do
name = Keyword.get(opts, :cache_statement)

if comment_not_present!(opts) && name do
Expand Down Expand Up @@ -349,7 +349,7 @@ defmodule Postgrex do
there was an error. See `query/3`.
"""
@spec query!(conn, iodata, list, [execute_option]) :: Postgrex.Result.t()
def query!(conn, statement, params, opts \\ []) do
def query!(conn, statement, params, opts \\ []) when is_list(params) do
case query(conn, statement, params, opts) do
{:ok, result} -> result
{:error, err} -> raise err
Expand Down Expand Up @@ -434,7 +434,7 @@ defmodule Postgrex do
"""
@spec prepare_execute(conn, iodata, iodata, list, [execute_option]) ::
{:ok, Postgrex.Query.t(), Postgrex.Result.t()} | {:error, Postgrex.Error.t()}
def prepare_execute(conn, name, statement, params, opts \\ []) do
def prepare_execute(conn, name, statement, params, opts \\ []) when is_list(params) do
query = %Query{name: name, statement: statement}
opts = Keyword.put(opts, :postgrex_prepare, comment_not_present!(opts))
DBConnection.prepare_execute(conn, query, params, opts)
Expand All @@ -446,7 +446,7 @@ defmodule Postgrex do
"""
@spec prepare_execute!(conn, iodata, iodata, list, [execute_option]) ::
{Postgrex.Query.t(), Postgrex.Result.t()}
def prepare_execute!(conn, name, statement, params, opts \\ []) do
def prepare_execute!(conn, name, statement, params, opts \\ []) when is_list(params) do
query = %Query{name: name, statement: statement}
opts = Keyword.put(opts, :postgrex_prepare, comment_not_present!(opts))
DBConnection.prepare_execute!(conn, query, params, opts)
Expand Down Expand Up @@ -482,7 +482,7 @@ defmodule Postgrex do
"""
@spec execute(conn, Postgrex.Query.t(), list, [execute_option]) ::
{:ok, Postgrex.Query.t(), Postgrex.Result.t()} | {:error, Postgrex.Error.t()}
def execute(conn, query, params, opts \\ []) do
def execute(conn, query, params, opts \\ []) when is_list(params) do
DBConnection.execute(conn, query, params, opts)
end

Expand All @@ -492,7 +492,7 @@ defmodule Postgrex do
"""
@spec execute!(conn, Postgrex.Query.t(), list, [execute_option]) ::
Postgrex.Result.t()
def execute!(conn, query, params, opts \\ []) do
def execute!(conn, query, params, opts \\ []) when is_list(params) do
DBConnection.execute!(conn, query, params, opts)
end

Expand Down
26 changes: 26 additions & 0 deletions test/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1927,4 +1927,30 @@ defmodule QueryTest do
{:ok, pid} = P.start_link(database: "postgrex_test", search_path: ["public", "test_schema"])
%{rows: [[1, "foo"]]} = P.query!(pid, "SELECT * from test_table", [])
end

test "raise a nice message if params is not a list", context do
assert_raise FunctionClauseError, fn ->
query("SELECT 'hi ' <> $1", "postgrex")
end

assert_raise FunctionClauseError, fn ->
Postgrex.query!(context[:pid], "SELECT 'hi ' <> $1", "postgrex")
end

assert_raise FunctionClauseError, fn ->
prepare_execute("name", "SELECT 'hi ' <> $1", "postgrex")
end

assert_raise FunctionClauseError, fn ->
Postgrex.prepare_execute!(context[:pid], "name", "SELECT 'hi ' <> $1", "postgrex")
end

assert_raise FunctionClauseError, fn ->
execute("name", "postgrex")
end

assert_raise FunctionClauseError, fn ->
Postgrex.execute!(context[:pid], "name", "postgrex")
end
end
end

0 comments on commit 66325e4

Please sign in to comment.