Skip to content

Commit

Permalink
raise a nice error message of params is not a list
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-rychlewski committed Nov 6, 2024
1 parent fb9992e commit c57a22e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
6 changes: 5 additions & 1 deletion lib/postgrex/type_module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,14 @@ defmodule Postgrex.TypeModule do
unquote(encodes)

@doc false
def encode_params(params, types) do
def encode_params(params, types) when is_list(params) do
encode_params(params, types, [])
end

def encode_params(params, _types) do
raise ArgumentError, "expected params to be a list, got: #{inspect(params)}"
end

defp encode_params([param | params], [type | types], encoded) do
encode_params(params, types, [encode_value(param, type) | encoded])
end
Expand Down
12 changes: 8 additions & 4 deletions test/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,6 @@ defmodule QueryTest do
assert [[[{1, "2"}]]] = query("SELECT ARRAY[(1, '2')::composite1]", [])
end

test "decode enum", context do
assert [["elixir"]] = query("SELECT 'elixir'::enum1", [])
end

@tag min_pg_version: "9.2"
test "decode range", context do
# These do not appear to match what is selected, but that's because
Expand Down Expand Up @@ -1927,4 +1923,12 @@ 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
msg = ~r"expected params to be a list"

assert_raise ArgumentError, msg, fn ->
query("SELECT 'hi ' <> $1", "postgrex")
end
end
end

0 comments on commit c57a22e

Please sign in to comment.