From cf1f19258e558d87d3f955b017f6ab1016164446 Mon Sep 17 00:00:00 2001 From: "Zaiming (Stone) Shi" Date: Thu, 27 Jan 2022 00:12:35 +0100 Subject: [PATCH] refactor(emqx_resource): catch only hocon throw exceptions --- apps/emqx_resource/src/emqx_resource.erl | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/apps/emqx_resource/src/emqx_resource.erl b/apps/emqx_resource/src/emqx_resource.erl index e42857303..fa19c0057 100644 --- a/apps/emqx_resource/src/emqx_resource.erl +++ b/apps/emqx_resource/src/emqx_resource.erl @@ -295,18 +295,20 @@ call_jsonify(Mod, Config) -> -spec check_config(resource_type(), raw_resource_config()) -> {ok, resource_config()} | {error, term()}. check_config(ResourceType, RawConfig) when is_binary(RawConfig) -> - case hocon:binary(RawConfig, #{format => richmap}) of + case hocon:binary(RawConfig, #{format => map}) of {ok, MapConfig} -> - case ?SAFE_CALL(hocon_tconf:check(ResourceType, MapConfig, ?HOCON_CHECK_OPTS)) of - {error, Reason} -> {error, Reason}; - Config -> {ok, hocon_maps:ensure_plain(Config)} - end; - Error -> Error + check_config(ResourceType, MapConfig); + {error, Reason} -> + {error, Reason} end; check_config(ResourceType, RawConfigTerm) -> - case ?SAFE_CALL(hocon_tconf:check_plain(ResourceType, RawConfigTerm, ?HOCON_CHECK_OPTS)) of - {error, Reason} -> {error, Reason}; - Config -> {ok, Config} + %% hocon_tconf map and check APIs throw exception on bad configs + try hocon_tconf:check_plain(ResourceType, RawConfigTerm, ?HOCON_CHECK_OPTS) of + Config -> + {ok, Config} + catch + throw : Reason -> + {error, Reason} end. -spec check_and_create(instance_id(), resource_type(), raw_resource_config()) ->