From 63606922f28f77aad8656e1ac1f97347e1ae165d Mon Sep 17 00:00:00 2001 From: Stefan Strigler Date: Mon, 30 Oct 2023 16:19:11 +0100 Subject: [PATCH] refactor(emqx_bridge): only one place to generate same error --- apps/emqx_bridge/src/emqx_bridge_v2.erl | 27 ++++++++++++++----------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/apps/emqx_bridge/src/emqx_bridge_v2.erl b/apps/emqx_bridge/src/emqx_bridge_v2.erl index e7596765b..18f79a782 100644 --- a/apps/emqx_bridge/src/emqx_bridge_v2.erl +++ b/apps/emqx_bridge/src/emqx_bridge_v2.erl @@ -1389,30 +1389,33 @@ validate_referenced_connectors(BridgeType, ConnectorNameBin, BridgeName) -> %% N.B.: assumes that, for all bridgeV2 types, the name of the bridge type is %% identical to its matching connector type name. try - ConnectorType = bridge_v2_type_to_connector_type(to_existing_atom(BridgeType)), - ConnectorName = to_existing_atom(ConnectorNameBin), + {ConnectorName, ConnectorType} = to_connector(ConnectorNameBin, BridgeType), case emqx_config:get([connectors, ConnectorType, ConnectorName], undefined) of undefined -> - {error, #{ - reason => "connector_not_found_or_wrong_type", - connector_name => ConnectorName, - connectortype => ConnectorType, - bridge_name => BridgeName, - bridge_type => BridgeType - }}; + throw(not_found); _ -> ok end catch - throw:bad_atom -> + throw:not_found -> {error, #{ reason => "connector_not_found_or_wrong_type", - type => BridgeType, + connector_name => ConnectorNameBin, bridge_name => BridgeName, - connector_name => ConnectorNameBin + bridge_type => BridgeType }} end. +to_connector(ConnectorNameBin, BridgeType) -> + try + ConnectorType = ?MODULE:bridge_v2_type_to_connector_type(to_existing_atom(BridgeType)), + ConnectorName = to_existing_atom(ConnectorNameBin), + {ConnectorName, ConnectorType} + catch + _:_ -> + throw(not_found) + end. + multi_validate_referenced_connectors(Configs) -> Pipeline = lists:map(