diff --git a/apps/emqx/src/emqx_config_handler.erl b/apps/emqx/src/emqx_config_handler.erl index 5576f579d..1ca2e8b24 100644 --- a/apps/emqx/src/emqx_config_handler.erl +++ b/apps/emqx/src/emqx_config_handler.erl @@ -457,6 +457,17 @@ bin_path(ConfKeyPath) -> [bin(Key) || Key <- ConfKeyPath]. bin(A) when is_atom(A) -> atom_to_binary(A, utf8); bin(B) when is_binary(B) -> B. +atom(Bin) when is_binary(Bin), size(Bin) > 255 -> + erlang:throw( + iolist_to_binary( + io_lib:format( + "Name is is too long." + " Please provide a shorter name (<= 255 bytes)." + " The name that is too long: \"~s\"", + [Bin] + ) + ) + ); atom(Bin) when is_binary(Bin) -> binary_to_atom(Bin, utf8); atom(Str) when is_list(Str) -> diff --git a/apps/emqx_bridge/src/emqx_bridge_api.erl b/apps/emqx_bridge/src/emqx_bridge_api.erl index c0b36dd76..443e2d93e 100644 --- a/apps/emqx_bridge/src/emqx_bridge_api.erl +++ b/apps/emqx_bridge/src/emqx_bridge_api.erl @@ -602,7 +602,7 @@ create_or_update_bridge(BridgeType, BridgeName, Conf, HttpStatusCode) -> case emqx_bridge:create(BridgeType, BridgeName, Conf) of {ok, _} -> lookup_from_all_nodes(BridgeType, BridgeName, HttpStatusCode); - {error, #{kind := validation_error} = Reason} -> + {error, Reason} when is_map(Reason) -> ?BAD_REQUEST(map_to_json(Reason)) end. diff --git a/apps/emqx_bridge/test/emqx_bridge_api_SUITE.erl b/apps/emqx_bridge/test/emqx_bridge_api_SUITE.erl index 2f5d65259..6f5c669a0 100644 --- a/apps/emqx_bridge/test/emqx_bridge_api_SUITE.erl +++ b/apps/emqx_bridge/test/emqx_bridge_api_SUITE.erl @@ -421,6 +421,26 @@ t_http_crud_apis(Config) -> ), %% Test bad updates + %% ================ + + %% Add bridge with a name that is too long + %% We only support bridge names up to 255 characters + LongName = list_to_binary(lists:duplicate(256, $a)), + NameTooLongRequestResult = request_json( + post, + uri(["bridges"]), + ?HTTP_BRIDGE(URL1, LongName), + Config + ), + ?assertMatch( + {ok, 400, _}, + NameTooLongRequestResult + ), + {ok, 400, #{<<"message">> := NameTooLongMessage}} = NameTooLongRequestResult, + %% Use regex to check that the message contains the name + Match = re:run(NameTooLongMessage, LongName), + ?assertMatch({match, _}, Match), + %% Add bridge without the URL field {ok, 400, PutFail1} = request_json( put, uri(["bridges", BridgeID]), diff --git a/changes/ce/fix-10911.en.md b/changes/ce/fix-10911.en.md new file mode 100644 index 000000000..8fafb7ce4 --- /dev/null +++ b/changes/ce/fix-10911.en.md @@ -0,0 +1 @@ +The error message and log entry that appear when one tries to create a bridge with a name the exceeds 255 bytes is now easier to understand.