Fix some decoding crashes with .eterm
This commit is contained in:
parent
29ebb5dfc9
commit
d7f3812f99
@ -3,8 +3,7 @@
|
|||||||
{plugins, [rebar3_hex]}.
|
{plugins, [rebar3_hex]}.
|
||||||
|
|
||||||
{deps, [
|
{deps, [
|
||||||
{setup, {git, "https://github.com/uwiger/setup.git", {ref, "9675f9a"}}}
|
{setup, "2.2.1"}
|
||||||
%% , {setup, "2.1.2"}
|
|
||||||
]}.
|
]}.
|
||||||
|
|
||||||
{profiles, [
|
{profiles, [
|
||||||
|
@ -119,7 +119,8 @@ mock_system_defaults(Config) ->
|
|||||||
-endif.
|
-endif.
|
||||||
|
|
||||||
-spec set_gmconfig_env(gmconfig()) -> ok.
|
-spec set_gmconfig_env(gmconfig()) -> ok.
|
||||||
set_gmconfig_env(Env) when is_map(Env) ->
|
set_gmconfig_env(Env0) when is_map(Env0) ->
|
||||||
|
Env = maps:merge(default_gmconfig_env(), Env0),
|
||||||
persistent_term:put({?MODULE, gmconfig_env}, Env).
|
persistent_term:put({?MODULE, gmconfig_env}, Env).
|
||||||
|
|
||||||
-spec gmconfig_env() -> gmconfig().
|
-spec gmconfig_env() -> gmconfig().
|
||||||
@ -941,7 +942,7 @@ read_file(F, Type, Decoder, Mode) ->
|
|||||||
try_decode_bin(Bin, Decoder, Type, Mode),
|
try_decode_bin(Bin, Decoder, Type, Mode),
|
||||||
F, Mode);
|
F, Mode);
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?LOG_ERROR("Read error ~s - ~p", [F, Reason]),
|
error_msg(Mode, "Read error ~s - ~p", [F, Reason]),
|
||||||
error({read_error, F, Reason})
|
error({read_error, F, Reason})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
@ -951,13 +952,29 @@ json_decode(Bin) when is_binary(Bin) ->
|
|||||||
json:decode(Bin).
|
json:decode(Bin).
|
||||||
|
|
||||||
eterm_consult(Bin) ->
|
eterm_consult(Bin) ->
|
||||||
setup_file:consult_binary(Bin).
|
case setup_file:consult_binary(Bin) of
|
||||||
|
{ok, [Map]} when is_map(Map) ->
|
||||||
|
{ok, normalize_config(Map)};
|
||||||
|
{ok, Other} ->
|
||||||
|
error({unknown_data, Other});
|
||||||
|
{error, _} = Error ->
|
||||||
|
Error
|
||||||
|
end.
|
||||||
|
|
||||||
|
normalize_config(Map) when is_map(Map) ->
|
||||||
|
try
|
||||||
|
json:decode(iolist_to_binary(json:encode(Map)))
|
||||||
|
catch
|
||||||
|
error:E ->
|
||||||
|
error({cannot_normalize, E, Map})
|
||||||
|
end.
|
||||||
|
|
||||||
try_decode_bin(Bin, Decoder, Fmt, Mode) ->
|
try_decode_bin(Bin, Decoder, Fmt, Mode) ->
|
||||||
try decode_bin(Bin, Decoder)
|
try decode_bin(Bin, Decoder)
|
||||||
catch
|
catch
|
||||||
error:E ->
|
error:E:T ->
|
||||||
decode_fail(E, Fmt, Mode)
|
error_msg(Mode, "CAUGHT for ~p: ~p / ~p", [Fmt, E, T]),
|
||||||
|
error(E)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
decode_bin(Str, Decoder) when is_list(Str) ->
|
decode_bin(Str, Decoder) when is_list(Str) ->
|
||||||
@ -969,13 +986,11 @@ decode_bin(Bin, Decoder) when is_binary(Bin), is_function(Decoder, 1) ->
|
|||||||
Map when is_map(Map) ->
|
Map when is_map(Map) ->
|
||||||
Map;
|
Map;
|
||||||
{error, E} ->
|
{error, E} ->
|
||||||
error(E)
|
error(E);
|
||||||
|
Other ->
|
||||||
|
error({bad_decoder_return, Other})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
decode_fail(E, Fmt, Mode) ->
|
|
||||||
error_msg(Mode, "Parse error (~p)", [Fmt]),
|
|
||||||
erlang:error(E).
|
|
||||||
|
|
||||||
validate(JSON, F, Mode) when is_map(JSON) ->
|
validate(JSON, F, Mode) when is_map(JSON) ->
|
||||||
check_validation([validate_(JSON)], JSON, F, Mode).
|
check_validation([validate_(JSON)], JSON, F, Mode).
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user