Explorar o código

Merge pull request #9268 from sstrigler/EMQX-7817-fix-unexpected-400

Fix unexpected 400
Zaiming (Stone) Shi %!s(int64=3) %!d(string=hai) anos
pai
achega
e9d31b2f4a

+ 1 - 1
apps/emqx_gateway/src/emqx_gateway_api.erl

@@ -151,7 +151,7 @@ gateway_insta(get, #{bindings := #{name := Name0}}) ->
             end
     catch
         error:badarg ->
-            return_http_error(400, "Bad gateway name")
+            return_http_error(404, "Bad gateway name")
     end;
 gateway_insta(put, #{
     body := GwConf0,

+ 7 - 4
apps/emqx_gateway/test/emqx_gateway_api_SUITE.erl

@@ -66,8 +66,8 @@ t_gateway(_) ->
     lists:foreach(fun assert_gw_unloaded/1, UnloadedGateways),
     {200, NoRunningGateways} = request(get, "/gateways?status=running"),
     ?assertEqual([], NoRunningGateways),
-    {400, BadReqUnknownGw} = request(get, "/gateways/unknown_gateway"),
-    assert_bad_request(BadReqUnknownGw),
+    {404, GwNotFoundReq} = request(get, "/gateways/unknown_gateway"),
+    assert_not_found(GwNotFoundReq),
     {400, BadReqInvalidStatus} = request(get, "/gateways?status=invalid_status"),
     assert_bad_request(BadReqInvalidStatus),
     {400, BadReqUCStatus} = request(get, "/gateways?status=UNLOADED"),
@@ -86,8 +86,8 @@ t_gateway(_) ->
 t_deprecated_gateway(_) ->
     {200, Gateways} = request(get, "/gateway"),
     lists:foreach(fun assert_gw_unloaded/1, Gateways),
-    {400, BadReq} = request(get, "/gateway/uname_gateway"),
-    assert_bad_request(BadReq),
+    {404, NotFoundReq} = request(get, "/gateway/uname_gateway"),
+    assert_not_found(NotFoundReq),
     {201, _} = request(post, "/gateway", #{name => <<"stomp">>}),
     {200, StompGw1} = request(get, "/gateway/stomp"),
     assert_feilds_apperence(
@@ -571,3 +571,6 @@ assert_gw_unloaded(Gateway) ->
 
 assert_bad_request(BadReq) ->
     ?assertEqual(<<"BAD_REQUEST">>, maps:get(code, BadReq)).
+
+assert_not_found(NotFoundReq) ->
+    ?assertEqual(<<"RESOURCE_NOT_FOUND">>, maps:get(code, NotFoundReq)).

+ 2 - 0
changes/v5.0.10-en.md

@@ -30,3 +30,5 @@
   This bug was introduced in v5.0.9. For Rule-Engine's input events like `$events/message_delivered`
   and `$events/message_dropped`, if the message was delivered to a shared-subscription,
   the encoding (to JSON) of the event will fail.
+
+- Fix bad HTTP response status code for `/gateways` API, when Gateway name is unknown, it should return `404` instead of `400` [#9268](https://github.com/emqx/emqx/pull/9268).

+ 2 - 0
changes/v5.0.10-zh.md

@@ -28,3 +28,5 @@
 - 修复规则引擎的消息事件编码失败 [#9228](https://github.com/emqx/emqx/pull/9228)。
   该问题在 v5.0.9 中引入:带消息的规则引擎事件,例如 `$events/message_delivered` 和
   `$events/message_dropped`, 如果消息事件是共享订阅产生的,在编码(到 JSON 格式)过程中会失败。
+
+- 修复 HTTP API `/gateways` 的返回状态码,未知 Gateway 名字应返回 `404` 而不是 `400` [#9268](https://github.com/emqx/emqx/pull/9268)。