From 2a27d2e7817d7b5861ffdb128465396393a190f1 Mon Sep 17 00:00:00 2001 From: Stefan Strigler Date: Wed, 7 Dec 2022 14:07:03 +0100 Subject: [PATCH 1/3] fix: return 409 in case of duplicate --- apps/emqx_management/src/emqx_mgmt_api_trace.erl | 15 ++++++++++----- .../test/emqx_mgmt_api_trace_SUITE.erl | 12 +++++++++++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/apps/emqx_management/src/emqx_mgmt_api_trace.erl b/apps/emqx_management/src/emqx_mgmt_api_trace.erl index 4bdf225fd..17895786b 100644 --- a/apps/emqx_management/src/emqx_mgmt_api_trace.erl +++ b/apps/emqx_management/src/emqx_mgmt_api_trace.erl @@ -84,11 +84,16 @@ schema("/trace") -> 200 => hoconsc:ref(trace), 400 => emqx_dashboard_swagger:error_codes( [ - 'ALREADY_EXISTS', - 'DUPLICATE_CONDITION', 'INVALID_PARAMS' ], - <<"trace name already exists">> + <<"invalid trace params">> + ), + 409 => emqx_dashboard_swagger:error_codes( + [ + 'ALREADY_EXISTS', + 'DUPLICATE_CONDITION' + ], + <<"trace already exists">> ) } }, @@ -392,12 +397,12 @@ trace(post, #{body := Param}) -> {ok, Trace0} -> {200, format_trace(Trace0)}; {error, {already_existed, Name}} -> - {400, #{ + {409, #{ code => 'ALREADY_EXISTS', message => ?TO_BIN([Name, " Already Exists"]) }}; {error, {duplicate_condition, Name}} -> - {400, #{ + {409, #{ code => 'DUPLICATE_CONDITION', message => ?TO_BIN([Name, " Duplication Condition"]) }}; diff --git a/apps/emqx_management/test/emqx_mgmt_api_trace_SUITE.erl b/apps/emqx_management/test/emqx_mgmt_api_trace_SUITE.erl index 68c998c47..d73cb79fd 100644 --- a/apps/emqx_management/test/emqx_mgmt_api_trace_SUITE.erl +++ b/apps/emqx_management/test/emqx_mgmt_api_trace_SUITE.erl @@ -149,7 +149,7 @@ t_create_failed(_Config) -> {ok, Create} = request_api(post, api_path("trace"), Header, [GoodName | Trace]), ?assertMatch(#{<<"name">> := <<"test-name-0">>}, json(Create)), ?assertMatch( - {error, {"HTTP/1.1", 400, _}, _}, + {error, {"HTTP/1.1", 409, _}, _}, request_api(post, api_path("trace"), Header, [GoodName | Trace]) ), @@ -171,6 +171,16 @@ t_create_failed(_Config) -> {error, {"HTTP/1.1", 400, _}, _}, request_api(post, api_path("trace"), Header, [GoodName1 | Trace]) ), + %% clear + ?assertMatch({ok, _}, request_api(delete, api_path("trace"), Header, [])), + {ok, Create} = request_api(post, api_path("trace"), Header, [GoodName | Trace]), + %% new name but same trace + GoodName2 = {<<"name">>, <<"test-name-1">>}, + ?assertMatch( + {error, {"HTTP/1.1", 409, _}, _}, + request_api(post, api_path("trace"), Header, [GoodName2 | Trace]) + ), + unload(), emqx_trace:clear(), ok. From a5337e0cdfeb32a77bfffbd90b278ebbabd8413b Mon Sep 17 00:00:00 2001 From: Stefan Strigler Date: Wed, 7 Dec 2022 14:13:44 +0100 Subject: [PATCH 2/3] chore: add changelog --- changes/v5.0.12-en.md | 4 +++- changes/v5.0.12-zh.md | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/changes/v5.0.12-en.md b/changes/v5.0.12-en.md index d3a0e7a27..27e56741e 100644 --- a/changes/v5.0.12-en.md +++ b/changes/v5.0.12-en.md @@ -39,4 +39,6 @@ - Fix shadowing `'client.authenticate'` callbacks by `emqx_authenticator`. Now `emqx_authenticator` passes execution to the further callbacks if none of the authenticators matches [#9496](https://github.com/emqx/emqx/pull/9496). -- Return `400` if query param `node` is not a known in `/trace/:id/download?node={node}` [#9478](https://github.com/emqx/emqx/pull/9478). +- Return `400` if query param `node` is not a known node in `/trace/:id/download?node={node}` [#9478](https://github.com/emqx/emqx/pull/9478). + +- `POST /traces` to return `409` in case of duplicate [#9494](https://github.com/emqx/emqx/pull/9494). diff --git a/changes/v5.0.12-zh.md b/changes/v5.0.12-zh.md index 26208a7cc..2a0b567c5 100644 --- a/changes/v5.0.12-zh.md +++ b/changes/v5.0.12-zh.md @@ -39,3 +39,5 @@ - 通过 `emqx_authenticator` 修复隐藏 `'client.authenticate'` 回调。 现在 `emqx_authenticator` 如果没有任何验证器匹配,则将执行传递给进一步的回调 [#9496](https://github.com/emqx/emqx/pull/9496)。 - 如果在调用 `/trace/:id/download?node={node}` 时,`node` 不存在,则会返回 `400` [#9478](https://github.com/emqx/emqx/pull/9478). + +- 现在,当重复调用 `POST /traces` 时,将会返回 `409` ,而不再是 `400` [#9494](https://github.com/emqx/emqx/pull/9494)。 From b4af0961523fd6f16b9f91bb06fa580ab7d36e28 Mon Sep 17 00:00:00 2001 From: Stefan Strigler Date: Fri, 9 Dec 2022 14:26:31 +0100 Subject: [PATCH 3/3] style: fix translations Co-authored-by: Zaiming (Stone) Shi --- changes/v5.0.12-zh.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/changes/v5.0.12-zh.md b/changes/v5.0.12-zh.md index 2a0b567c5..d1f10c656 100644 --- a/changes/v5.0.12-zh.md +++ b/changes/v5.0.12-zh.md @@ -38,6 +38,6 @@ - 通过 `emqx_authenticator` 修复隐藏 `'client.authenticate'` 回调。 现在 `emqx_authenticator` 如果没有任何验证器匹配,则将执行传递给进一步的回调 [#9496](https://github.com/emqx/emqx/pull/9496)。 -- 如果在调用 `/trace/:id/download?node={node}` 时,`node` 不存在,则会返回 `400` [#9478](https://github.com/emqx/emqx/pull/9478). +- 如果在调用 `/trace/:id/download?node={node}` 时,`node` 不存在,则会返回 `400` [#9478](https://github.com/emqx/emqx/pull/9478)。 -- 现在,当重复调用 `POST /traces` 时,将会返回 `409` ,而不再是 `400` [#9494](https://github.com/emqx/emqx/pull/9494)。 +- 当重复调用 `POST /traces` 时,将会返回 `409` ,而不再是 `400` [#9494](https://github.com/emqx/emqx/pull/9494)。