소스 검색

fix: rate limiter schema check crash and return 500

Zhongwen Deng 3 년 전
부모
커밋
38215f73fc

+ 35 - 34
apps/emqx/src/emqx_limiter/src/emqx_limiter_schema.erl

@@ -202,34 +202,30 @@ to_rate(Str, CanInfinity, CanZero) ->
             {ok, infinity};
         %% if time unit is 1s, it can be omitted
         [QuotaStr] ->
-            {ok, Val} = to_capacity(QuotaStr),
-            check_capacity(
-                Str,
-                Val,
-                CanZero,
-                fun(Quota) ->
-                    {ok, Quota * minimum_period() / ?UNIT_TIME_IN_MS}
-                end
-            );
+            Fun = fun(Quota) ->
+                {ok, Quota * minimum_period() / ?UNIT_TIME_IN_MS}
+            end,
+            to_capacity(QuotaStr, Str, CanZero, Fun);
         [QuotaStr, Interval] ->
-            {ok, Val} = to_capacity(QuotaStr),
-            check_capacity(
-                Str,
-                Val,
-                CanZero,
-                fun(Quota) ->
-                    case emqx_schema:to_duration_ms(Interval) of
-                        {ok, Ms} when Ms > 0 ->
-                            {ok, Quota * minimum_period() / Ms};
-                        _ ->
-                            {error, Str}
-                    end
+            Fun = fun(Quota) ->
+                case emqx_schema:to_duration_ms(Interval) of
+                    {ok, Ms} when Ms > 0 ->
+                        {ok, Quota * minimum_period() / Ms};
+                    _ ->
+                        {error, Str}
                 end
-            );
+            end,
+            to_capacity(QuotaStr, Str, CanZero, Fun);
         _ ->
             {error, Str}
     end.
 
+to_capacity(QuotaStr, Str, CanZero, Fun) ->
+    case to_capacity(QuotaStr) of
+        {ok, Val} -> check_capacity(Str, Val, CanZero, Fun);
+        {error, _Error} -> {error, Str}
+    end.
+
 check_capacity(_Str, 0, true, _Cont) ->
     {ok, 0};
 check_capacity(Str, 0, false, _Cont) ->
@@ -247,18 +243,23 @@ to_initial(Str) ->
 
 to_quota(Str, Regex) ->
     {ok, MP} = re:compile(Regex),
-    Result = re:run(Str, MP, [{capture, all_but_first, list}]),
-    case Result of
-        {match, [Quota, Unit]} ->
-            Val = erlang:list_to_integer(Quota),
-            Unit2 = string:to_lower(Unit),
-            {ok, apply_unit(Unit2, Val)};
-        {match, [Quota, ""]} ->
-            {ok, erlang:list_to_integer(Quota)};
-        {match, ""} ->
-            {ok, infinity};
-        _ ->
-            {error, Str}
+    try
+        Result = re:run(Str, MP, [{capture, all_but_first, list}]),
+        case Result of
+            {match, [Quota, Unit]} ->
+                Val = erlang:list_to_integer(Quota),
+                Unit2 = string:to_lower(Unit),
+                {ok, apply_unit(Unit2, Val)};
+            {match, [Quota, ""]} ->
+                {ok, erlang:list_to_integer(Quota)};
+            {match, ""} ->
+                {ok, infinity};
+            _ ->
+                {error, Str}
+        end
+    catch
+        _:Error ->
+            {error, Error}
     end.
 
 apply_unit("", Val) -> Val;

+ 5 - 4
apps/emqx_dashboard/src/emqx_dashboard_config.erl

@@ -76,8 +76,9 @@ pre_config_update(_Path, UpdateConf0, RawConf) ->
 post_config_update(_, _Req, NewConf, OldConf, _AppEnvs) ->
     #{listeners := NewListeners} = NewConf,
     #{listeners := OldListeners} = OldConf,
-    case NewListeners =:= OldListeners of
-        true -> ok;
-        false -> erlang:send_after(500, ?MODULE, {update_listeners, OldListeners, NewListeners})
-    end,
+    _ =
+        case NewListeners =:= OldListeners of
+            true -> ok;
+            false -> erlang:send_after(500, ?MODULE, {update_listeners, OldListeners, NewListeners})
+        end,
     ok.

+ 3 - 1
apps/emqx_dashboard/src/emqx_dashboard_swagger.erl

@@ -813,7 +813,9 @@ schema_converter(Options) ->
 
 serialize_hocon_error_msg({_Schema, Errors}) ->
     Msg = lists:map(fun hocon_error/1, Errors),
-    iolist_to_binary(string:join(Msg, ",")).
+    iolist_to_binary(string:join(Msg, ","));
+serialize_hocon_error_msg(Error) ->
+    iolist_to_binary(io_lib:format("~p", [Error])).
 
 hocon_error({validation_error, #{reason := #{exception := Exception}, path := Path}}) ->
     io_lib:format("~ts: ~p", [sub_path(Path), Exception]);

+ 1 - 1
apps/emqx_management/test/emqx_mgmt_api_trace_SUITE.erl

@@ -55,7 +55,7 @@ t_http_test(_Config) ->
     ErrorTrace = #{},
     {error, {"HTTP/1.1", 400, "Bad Request"}, Body} =
         request_api(post, api_path("trace"), Header, ErrorTrace),
-    ?assertMatch(#{<<"code">> := <<"BAD_REQUEST">>} = json(Body)),
+    ?assertMatch(#{<<"code">> := <<"BAD_REQUEST">>}, json(Body)),
 
     Name = <<"test-name">>,
     Trace = [