refactor: inline fn clauses, improve error logging

This commit is contained in:
Thales Macedo Garitezi 2023-09-06 13:54:40 -03:00
parent c9100c7591
commit bb55b04d46
1 changed files with 36 additions and 20 deletions

View File

@ -273,37 +273,46 @@ format_mqtt_properties(MQTTPropertiesTemplate, Selected, Env) ->
), ),
coerce_properties_values(MQTTProperties0, Env). coerce_properties_values(MQTTProperties0, Env).
ensure_int(B) when is_binary(B) -> binary_to_integer(B); ensure_int(B) when is_binary(B) ->
ensure_int(I) when is_integer(I) -> I. try
binary_to_integer(B)
catch
error:badarg ->
throw(bad_integer)
end;
ensure_int(I) when is_integer(I) ->
I.
coerce_properties_values(MQTTProperties, #{metadata := #{rule_id := RuleId}}) -> coerce_properties_values(MQTTProperties, #{metadata := #{rule_id := RuleId}}) ->
PublishProperties =
maps:from_list([
{K, T}
|| {_Id, {K, T, Tags}} <- maps:to_list(emqtt_props:all()),
is_list(Tags) andalso lists:member(?PUBLISH, Tags)
]),
maps:fold( maps:fold(
fun(K, V, Acc) -> fun(K, V0, Acc) ->
try try
case maps:get(K, PublishProperties) of V = encode_mqtt_property(K, V0),
'Byte' -> Acc#{K => ensure_int(V)}; Acc#{K => V}
'Two-Byte-Integer' -> Acc#{K => ensure_int(V)};
'Four-Byte-Integer' -> Acc#{K => ensure_int(V)};
'Variable-Byte-Integer' -> Acc#{K => ensure_int(V)};
_ -> Acc#{K => V}
end
catch catch
Kind:Error -> throw:bad_integer ->
?SLOG( ?SLOG(
error, error,
#{ #{
msg => "bad_mqtt_property_value_ignored", msg => "bad_mqtt_property_value_ignored",
rule_id => RuleId, rule_id => RuleId,
reason => Error, reason => bad_integer,
exception => Kind,
property => K, property => K,
value => V value => V0
}
),
Acc;
Kind:Reason:Stacktrace ->
?SLOG(
error,
#{
msg => "bad_mqtt_property_value_ignored",
rule_id => RuleId,
exception => Kind,
reason => Reason,
property => K,
value => V0,
stacktrace => Stacktrace
} }
), ),
Acc Acc
@ -312,3 +321,10 @@ coerce_properties_values(MQTTProperties, #{metadata := #{rule_id := RuleId}}) ->
#{}, #{},
MQTTProperties MQTTProperties
). ).
%% Note: currently we do not support `Topic-Alias', which would need to be encoded as an
%% int.
encode_mqtt_property('Payload-Format-Indicator', V) -> ensure_int(V);
encode_mqtt_property('Message-Expiry-Interval', V) -> ensure_int(V);
encode_mqtt_property('Subscription-Identifier', V) -> ensure_int(V);
encode_mqtt_property(_Prop, V) -> V.