From 82800faadf3debe9db5f26493b660b875624cc25 Mon Sep 17 00:00:00 2001 From: Kjell Winblad Date: Tue, 28 May 2024 18:07:14 +0200 Subject: [PATCH] fix: make protobuf schema decode error reported to user less cryptic Before this commit the user would see a cryptic warning log including function_clause when decoding a message failed. This commit improves this by removing function_caluse as well as some other cryptic words from the message and adding a description describing what went wrong. Fixes: https://emqx.atlassian.net/browse/EMQX-12453 --- .../src/emqx_schema_registry.app.src | 2 +- .../src/emqx_schema_registry_serde.erl | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/apps/emqx_schema_registry/src/emqx_schema_registry.app.src b/apps/emqx_schema_registry/src/emqx_schema_registry.app.src index f05295579..efd9f1162 100644 --- a/apps/emqx_schema_registry/src/emqx_schema_registry.app.src +++ b/apps/emqx_schema_registry/src/emqx_schema_registry.app.src @@ -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, [ diff --git a/apps/emqx_schema_registry/src/emqx_schema_registry_serde.erl b/apps/emqx_schema_registry/src/emqx_schema_registry_serde.erl index 638147fd1..96c5816ba 100644 --- a/apps/emqx_schema_registry/src/emqx_schema_registry_serde.erl +++ b/apps/emqx_schema_registry/src/emqx_schema_registry_serde.erl @@ -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, + msg => + <<"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]) ->