diff --git a/src/mod_legacy_auth.erl b/src/mod_legacy_auth.erl
index 722a05738..0ae3253c9 100644
--- a/src/mod_legacy_auth.erl
+++ b/src/mod_legacy_auth.erl
@@ -61,11 +61,16 @@ mod_opt_type(_) ->
c2s_state() | {stop, c2s_state()}.
c2s_unauthenticated_packet(State, #iq{type = T, sub_els = [_]} = IQ)
when T == get; T == set ->
- case xmpp:get_subtag(IQ, #legacy_auth{}) of
+ try xmpp:try_subtag(IQ, #legacy_auth{}) of
#legacy_auth{} = Auth ->
{stop, authenticate(State, xmpp:set_els(IQ, [Auth]))};
false ->
State
+ catch _:{xmpp_codec, Why} ->
+ Txt = xmpp:io_format_error(Why),
+ Lang = maps:get(lang, State),
+ Err = xmpp:make_error(IQ, xmpp:error_bad_request(Txt, Lang)),
+ {stop, ejabberd_c2s:send(State, Err)}
end;
c2s_unauthenticated_packet(State, _) ->
State.
diff --git a/src/mod_muc_room.erl b/src/mod_muc_room.erl
index 271e49b45..e8d61dacb 100644
--- a/src/mod_muc_room.erl
+++ b/src/mod_muc_room.erl
@@ -4006,7 +4006,7 @@ route_invitation(From, Invitation, Lang, StateData) ->
%% Otherwise, an error message is sent to the sender.
-spec handle_roommessage_from_nonparticipant(message(), state(), jid()) -> ok.
handle_roommessage_from_nonparticipant(Packet, StateData, From) ->
- case xmpp:get_subtag(Packet, #muc_user{}) of
+ try xmpp:try_subtag(Packet, #muc_user{}) of
#muc_user{decline = #muc_decline{to = #jid{} = To} = Decline} = XUser ->
NewDecline = Decline#muc_decline{to = undefined, from = From},
NewXUser = XUser#muc_user{decline = NewDecline},
@@ -4018,6 +4018,10 @@ handle_roommessage_from_nonparticipant(Packet, StateData, From) ->
"to the conference">>,
Err = xmpp:err_not_acceptable(ErrText, xmpp:get_lang(Packet)),
ejabberd_router:route_error(Packet, Err)
+ catch _:{xmpp_codec, Why} ->
+ Txt = xmpp:io_format_error(Why),
+ Err = xmpp:err_bad_request(Txt, xmpp:get_lang(Packet)),
+ ejabberd_router:route_error(Packet, Err)
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
diff --git a/src/mod_multicast.erl b/src/mod_multicast.erl
index 7b772521d..fa9e19aef 100644
--- a/src/mod_multicast.erl
+++ b/src/mod_multicast.erl
@@ -402,7 +402,7 @@ strip_addresses_element(Packet) ->
#addresses{list = Addrs} ->
PacketStripped = xmpp:remove_subtag(Packet, #addresses{}),
{ok, PacketStripped, Addrs};
- undefined ->
+ false ->
throw(eadsele)
end.
diff --git a/src/mod_privilege.erl b/src/mod_privilege.erl
index dab7a619b..def097739 100644
--- a/src/mod_privilege.erl
+++ b/src/mod_privilege.erl
@@ -274,7 +274,7 @@ get_permissions(ServerHost) ->
forward_message(#message{to = To} = Msg) ->
ServerHost = To#jid.lserver,
Lang = xmpp:get_lang(Msg),
- case xmpp:get_subtag(Msg, #privilege{}) of
+ try xmpp:try_subtag(Msg, #privilege{}) of
#privilege{forwarded = #forwarded{xml_els = [SubEl]}} ->
try xmpp:decode(SubEl, ?NS_CLIENT, [ignore_els]) of
#message{} = NewMsg ->
@@ -297,7 +297,11 @@ forward_message(#message{to = To} = Msg) ->
ejabberd_router:route_error(Msg, Err)
end;
_ ->
- Txt = <<"Invalid element">>,
+ Txt = <<"No element found">>,
+ Err = xmpp:err_bad_request(Txt, Lang),
+ ejabberd_router:route_error(Msg, Err)
+ catch _:{xmpp_codec, Why} ->
+ Txt = xmpp:io_format_error(Why),
Err = xmpp:err_bad_request(Txt, Lang),
ejabberd_router:route_error(Msg, Err)
end.
diff --git a/src/mod_register.erl b/src/mod_register.erl
index 77557ee0b..c74d99d38 100644
--- a/src/mod_register.erl
+++ b/src/mod_register.erl
@@ -95,7 +95,7 @@ stream_feature_register(Acc, Host) ->
c2s_unauthenticated_packet(#{ip := IP, server := Server} = State,
#iq{type = T, sub_els = [_]} = IQ)
when T == set; T == get ->
- case xmpp:get_subtag(IQ, #register{}) of
+ try xmpp:try_subtag(IQ, #register{}) of
#register{} = Register ->
{Address, _} = IP,
IQ1 = xmpp:set_els(IQ, [Register]),
@@ -105,6 +105,11 @@ c2s_unauthenticated_packet(#{ip := IP, server := Server} = State,
{stop, ejabberd_c2s:send(State, ResIQ1)};
false ->
State
+ catch _:{xmpp_codec, Why} ->
+ Txt = xmpp:io_format_error(Why),
+ Lang = maps:get(lang, State),
+ Err = xmpp:make_error(IQ, xmpp:error_bad_request(Txt, Lang)),
+ {stop, ejabberd_c2s:send(State, Err)}
end;
c2s_unauthenticated_packet(State, _) ->
State.