Merge pull request #10964 from thalesmg/fix-hocon-compact-errors-v50

fix(schema): avoid `function_clause` error when compacting errors
This commit is contained in:
Thales Macedo Garitezi 2023-06-07 14:00:24 -03:00 committed by GitHub
commit 105c26afb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 6 deletions

View File

@ -100,7 +100,16 @@ no_stacktrace(Map) ->
%% it's maybe too much when reporting to the user
-spec compact_errors(any(), Stacktrace :: list()) -> {error, any()}.
compact_errors({SchemaModule, Errors}, Stacktrace) ->
compact_errors(SchemaModule, Errors, Stacktrace).
compact_errors(SchemaModule, Errors, Stacktrace);
compact_errors(ErrorContext0, _Stacktrace) when is_map(ErrorContext0) ->
case ErrorContext0 of
#{exception := #{schema_module := _Mod, message := _Msg} = Detail} ->
Error0 = maps:remove(exception, ErrorContext0),
Error = maps:merge(Error0, Detail),
{error, Error};
_ ->
{error, ErrorContext0}
end.
compact_errors(SchemaModule, [Error0 | More], _Stacktrace) when is_map(Error0) ->
Error1 =

View File

@ -2706,7 +2706,11 @@ do_to_timeout_duration(Str, Fn, Max, Unit) ->
Msg = lists:flatten(
io_lib:format("timeout value too large (max: ~b ~s)", [Max, Unit])
),
throw(Msg)
throw(#{
schema_module => ?MODULE,
message => Msg,
kind => validation_error
})
end;
Err ->
Err

View File

@ -886,15 +886,27 @@ timeout_types_test_() ->
typerefl:from_string(emqx_schema:timeout_duration_s(), <<"4294967000ms">>)
),
?_assertThrow(
"timeout value too large (max: 4294967295 ms)",
#{
kind := validation_error,
message := "timeout value too large (max: 4294967295 ms)",
schema_module := emqx_schema
},
typerefl:from_string(emqx_schema:timeout_duration(), <<"4294967296ms">>)
),
?_assertThrow(
"timeout value too large (max: 4294967295 ms)",
#{
kind := validation_error,
message := "timeout value too large (max: 4294967295 ms)",
schema_module := emqx_schema
},
typerefl:from_string(emqx_schema:timeout_duration_ms(), <<"4294967296ms">>)
),
?_assertThrow(
"timeout value too large (max: 4294967 s)",
#{
kind := validation_error,
message := "timeout value too large (max: 4294967 s)",
schema_module := emqx_schema
},
typerefl:from_string(emqx_schema:timeout_duration_s(), <<"4294967001ms">>)
)
].

View File

@ -67,7 +67,7 @@ health_check_interval_validator_test_() ->
parse_and_check_webhook_bridge(webhook_bridge_health_check_hocon(<<"3_600_000ms">>))
)},
?_assertThrow(
#{exception := "timeout value too large" ++ _},
#{exception := #{message := "timeout value too large" ++ _}},
parse_and_check_webhook_bridge(
webhook_bridge_health_check_hocon(<<"150000000000000s">>)
)