Przeglądaj źródła

refactor(emqx_bridge): only one place to generate same error

Stefan Strigler 2 lat temu
rodzic
commit
63606922f2
1 zmienionych plików z 15 dodań i 12 usunięć
  1. 15 12
      apps/emqx_bridge/src/emqx_bridge_v2.erl

+ 15 - 12
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(