Merge pull request #13147 from kjellwinblad/kjell/fix_function_clause_when_decoding_protobuf/EMQX-12453

fix: make protobuf schema decode error reported to user less cryptic
This commit is contained in:
Kjell Winblad 2024-05-29 15:56:27 +02:00 committed by GitHub
commit 6810591911
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 36 additions and 3 deletions

View File

@ -1,6 +1,6 @@
{application, emqx_schema_registry, [
{description, "EMQX Schema Registry"},
{vsn, "0.3.0"},
{vsn, "0.3.1"},
{registered, [emqx_schema_registry_sup]},
{mod, {emqx_schema_registry_app, []}},
{included_applications, [

View File

@ -64,7 +64,21 @@ handle_rule_function(sparkplug_encode, [Term | MoreArgs]) ->
[?EMQX_SCHEMA_REGISTRY_SPARKPLUGB_SCHEMA_NAME, Term | MoreArgs]
);
handle_rule_function(schema_decode, [SchemaId, Data | MoreArgs]) ->
decode(SchemaId, Data, MoreArgs);
try
decode(SchemaId, Data, MoreArgs)
catch
error:{gpb_error, {decoding_failure, {_Data, _Schema, {error, function_clause, _Stack}}}} ->
throw(
{schema_decode_error, #{
error_type => decoding_failure,
schema_id => SchemaId,
data => Data,
more_args => MoreArgs,
explain =>
<<"The given data could not be decoded. Please check the input data and the schema.">>
}}
)
end;
handle_rule_function(schema_decode, Args) ->
error({args_count_error, {schema_decode, Args}});
handle_rule_function(schema_encode, [SchemaId, Term | MoreArgs]) ->

View File

@ -44,7 +44,8 @@ sparkplug_tests() ->
t_sparkplug_decode,
t_sparkplug_encode,
t_sparkplug_decode_encode_with_message_name,
t_sparkplug_encode_float_to_uint64_key
t_sparkplug_encode_float_to_uint64_key,
t_decode_fail
].
init_per_suite(Config) ->
@ -532,6 +533,23 @@ t_encode(Config) ->
end,
ok.
t_decode_fail(_Config) ->
SerdeName = my_serde,
SerdeType = protobuf,
ok = create_serde(SerdeType, SerdeName),
Payload = <<"ss">>,
?assertThrow(
{schema_decode_error, #{
data := <<"ss">>,
error_type := decoding_failure,
explain := _,
more_args := [<<"Person">>],
schema_id := <<"my_serde">>
}},
emqx_rule_funcs:schema_decode(<<"my_serde">>, Payload, <<"Person">>)
),
ok.
t_decode(Config) ->
SerdeType = ?config(serde_type, Config),
SerdeName = my_serde,

View File

@ -0,0 +1 @@
Error messages for decoding failures in the rule engine protobuf decode functions have been improved by adding a clear descriptions to indicate what went wrong when message decoding fails.