Skip to content

Commit e4a2252

Browse files
committed
In postgres, add null fallback if integer encoding fails
1 parent e77384c commit e4a2252

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

src/sql_bridge_epgsql_codec_integer.erl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
-module(sql_bridge_epgsql_codec_integer).
22
-behaviour(epgsql_codec).
3+
-include_lib("epgsql/include/protocol.hrl").
34

45
-export([init/2, names/0, encode/3, decode/3, decode_text/3]).
56

@@ -12,8 +13,13 @@ names() ->
1213
?MAIN_MOD:names().
1314

1415
encode(Data, Type, State) ->
15-
N = normalize_int(Data),
16-
?MAIN_MOD:encode(N, Type, State).
16+
try normalize_int(Data) of
17+
N -> ?MAIN_MOD:encode(N, Type, State)
18+
catch _:_ ->
19+
%% Hardcode the NULL term here (not sure the better way)
20+
logger:warning("~p: Failed to convert term (~p) to integer. Convering to 'null'", [?MODULE, Data]),
21+
<< -1:?int32>>
22+
end.
1723

1824
decode(Data, Type, State) ->
1925
?MAIN_MOD:decode(Data, Type, State).

0 commit comments

Comments
 (0)