From c4e4940598522ff27fc0b657956dd6890e6f98c0 Mon Sep 17 00:00:00 2001 From: zmstone Date: Fri, 27 Sep 2024 22:16:00 +0200 Subject: [PATCH 1/2] chore: improve error context instead of crashing lists:zip with a function_clause --- src/commands/epgsql_cmd_prepared_query2.erl | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/commands/epgsql_cmd_prepared_query2.erl b/src/commands/epgsql_cmd_prepared_query2.erl index 198ac106..87670437 100644 --- a/src/commands/epgsql_cmd_prepared_query2.erl +++ b/src/commands/epgsql_cmd_prepared_query2.erl @@ -44,7 +44,7 @@ execute(Sock, #pquery2{name = Name, params = Params} = State) -> }, {finish, {error, Error}, Sock}; #statement{types = Types} = Stmt -> - TypedParams = lists:zip(Types, Params), + TypedParams = zip(Name, Types, Params), #statement{name = StatementName, columns = Columns} = Stmt, Codec = epgsql_sock:get_codec(Sock), Bin1 = epgsql_wire:encode_parameters(TypedParams, Codec), @@ -58,6 +58,19 @@ execute(Sock, #pquery2{name = Name, params = Params} = State) -> {send_multi, Commands, Sock, State#pquery2{stmt = Stmt}} end. +zip(Name, Types, Params) -> + case length(Types) =:= length(Params) of + true -> + lists:zip(Types, Params); + false -> + error(#{cause => "prepared_data_types_and_column_count_mismatch", + name => Name, + types => Types, + types_count => length(Types), + values_count => length(Params) + }) + end. + %% prepared query handle_message(?BIND_COMPLETE, <<>>, Sock, #pquery2{stmt = Stmt} = State) -> #statement{columns = Columns} = Stmt, From d4191c278f415fbe8f3696ff6d5d14e6000140cb Mon Sep 17 00:00:00 2001 From: zmstone Date: Mon, 30 Sep 2024 16:31:16 +0200 Subject: [PATCH 2/2] chore: refine error context Co-authored-by: Thales Macedo Garitezi --- src/commands/epgsql_cmd_prepared_query2.erl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/commands/epgsql_cmd_prepared_query2.erl b/src/commands/epgsql_cmd_prepared_query2.erl index 87670437..74f27a1a 100644 --- a/src/commands/epgsql_cmd_prepared_query2.erl +++ b/src/commands/epgsql_cmd_prepared_query2.erl @@ -66,8 +66,8 @@ zip(Name, Types, Params) -> error(#{cause => "prepared_data_types_and_column_count_mismatch", name => Name, types => Types, - types_count => length(Types), - values_count => length(Params) + type_count => length(Types), + column_count => length(Params) }) end.