Просмотр исходного кода

refactor(emqx_conf): raise exception at higher level for more context

zmstone 1 год назад
Родитель
Сommit
f9f14f9758
2 измененных файлов с 30 добавлено и 15 удалено
  1. 16 6
      apps/emqx_conf/src/emqx_conf.erl
  2. 14 9
      apps/emqx_conf/src/emqx_conf_schema_types.erl

+ 16 - 6
apps/emqx_conf/src/emqx_conf.erl

@@ -304,12 +304,22 @@ gen_flat_doc(RootNames, #{full_name := FullName, fields := Fields} = S, DescReso
         false ->
             ok
     end,
-    #{
-        text => short_name(FullName),
-        hash => format_hash(FullName),
-        doc => maps:get(desc, S, <<"">>),
-        fields => format_fields(Fields, DescResolver)
-    }.
+    try
+        #{
+            text => short_name(FullName),
+            hash => format_hash(FullName),
+            doc => maps:get(desc, S, <<"">>),
+            fields => format_fields(Fields, DescResolver)
+        }
+    catch
+        throw:Reason ->
+            io:format(
+                standard_error,
+                "failed_to_build_doc for ~s:~n~p~n",
+                [FullName, Reason]
+            ),
+            error(failed_to_build_doc)
+    end.
 
 format_fields(Fields, DescResolver) ->
     [format_field(F, DescResolver) || F <- Fields].

+ 14 - 9
apps/emqx_conf/src/emqx_conf_schema_types.erl

@@ -33,8 +33,19 @@ readable(Module, TypeStr) when is_list(TypeStr) ->
         %% Module is ignored so far as all types are distinguished by their names
         readable(TypeStr)
     catch
-        throw:unknown_type ->
-            fail(#{reason => unknown_type, type => TypeStr, module => Module})
+        throw:Reason ->
+            throw(#{
+                reason => Reason,
+                type => TypeStr,
+                module => Module
+            });
+        error:Reason:Stacktrace ->
+            throw(#{
+                reason => Reason,
+                stacktrace => Stacktrace,
+                type => TypeStr,
+                module => Module
+            })
     end.
 
 readable_swagger(Module, TypeStr) ->
@@ -49,16 +60,10 @@ readable_docgen(Module, TypeStr) ->
 get_readable(Module, TypeStr, Flavor) ->
     Map = readable(Module, TypeStr),
     case maps:get(Flavor, Map, undefined) of
-        undefined -> fail(#{reason => unknown_type, module => Module, type => TypeStr});
+        undefined -> throw(#{reason => unknown_type, module => Module, type => TypeStr});
         Value -> Value
     end.
 
-%% Fail the build or test. Production code should never get here.
--spec fail(_) -> no_return().
-fail(Reason) ->
-    io:format(standard_error, "ERROR: ~p~n", [Reason]),
-    error(Reason).
-
 readable("boolean()") ->
     #{
         swagger => #{type => boolean},