From ac31c858669c33555aace5647c875caea4990ff9 Mon Sep 17 00:00:00 2001 From: Evgeniy Khramtsov Date: Mon, 25 Jun 2018 09:45:45 +0300 Subject: [PATCH] Use error formatting functions from xmpp library --- rebar.config | 2 +- src/mod_s2s_dialback.erl | 17 +---------------- src/xmpp_stream_in.erl | 22 ++++------------------ src/xmpp_stream_out.erl | 35 +++++------------------------------ 4 files changed, 11 insertions(+), 65 deletions(-) diff --git a/rebar.config b/rebar.config index 30420d5f1..63d1b395d 100644 --- a/rebar.config +++ b/rebar.config @@ -25,7 +25,7 @@ {fast_tls, ".*", {git, "https://github.com/processone/fast_tls", {tag, "1.0.22"}}}, {stringprep, ".*", {git, "https://github.com/processone/stringprep", {tag, "1.0.11"}}}, {fast_xml, ".*", {git, "https://github.com/processone/fast_xml", {tag, "1.1.30"}}}, - {xmpp, ".*", {git, "https://github.com/processone/xmpp", "5745b7e"}}, + {xmpp, ".*", {git, "https://github.com/processone/xmpp", "cb39f8b"}}, {fast_yaml, ".*", {git, "https://github.com/processone/fast_yaml", {tag, "1.0.14"}}}, {jiffy, ".*", {git, "https://github.com/davisp/jiffy", {tag, "0.14.8"}}}, {p1_oauth2, ".*", {git, "https://github.com/processone/p1_oauth2", {tag, "0.6.3"}}}, diff --git a/src/mod_s2s_dialback.erl b/src/mod_s2s_dialback.erl index fa77e7d36..ff4174ebf 100644 --- a/src/mod_s2s_dialback.erl +++ b/src/mod_s2s_dialback.erl @@ -335,24 +335,9 @@ format_error(#db_result{type = invalid}) -> format_error(#db_result{type = error} = Result) -> case xmpp:get_error(Result) of #stanza_error{} = Err -> - format_stanza_error(Err); + xmpp:format_stanza_error(Err); undefined -> <<"unrecognized error">> end; format_error(_) -> <<"unexpected dialback result">>. - --spec format_stanza_error(stanza_error()) -> binary(). -format_stanza_error(#stanza_error{reason = Reason, text = Txt}) -> - Slogan = case Reason of - undefined -> <<"no reason">>; - #gone{} -> <<"gone">>; - #redirect{} -> <<"redirect">>; - _ -> erlang:atom_to_binary(Reason, latin1) - end, - case xmpp:get_text(Txt) of - <<"">> -> - Slogan; - Data -> - <> - end. diff --git a/src/xmpp_stream_in.erl b/src/xmpp_stream_in.erl index 675425bd0..29e0d1016 100644 --- a/src/xmpp_stream_in.erl +++ b/src/xmpp_stream_in.erl @@ -196,10 +196,10 @@ format_error({socket, Reason}) -> format("Connection failed: ~s", [format_inet_error(Reason)]); format_error({stream, reset}) -> <<"Stream reset by peer">>; -format_error({stream, {in, #stream_error{reason = Reason, text = Txt}}}) -> - format("Stream closed by peer: ~s", [format_stream_error(Reason, Txt)]); -format_error({stream, {out, #stream_error{reason = Reason, text = Txt}}}) -> - format("Stream closed by us: ~s", [format_stream_error(Reason, Txt)]); +format_error({stream, {in, #stream_error{} = Err}}) -> + format("Stream closed by peer: ~s", [xmpp:format_stream_error(Err)]); +format_error({stream, {out, #stream_error{} = Err}}) -> + format("Stream closed by us: ~s", [xmpp:format_stream_error(Err)]); format_error({tls, Reason}) -> format("TLS failed: ~s", [format_tls_error(Reason)]); format_error(internal_failure) -> @@ -1156,20 +1156,6 @@ format_inet_error(Reason) -> Txt -> Txt end. --spec format_stream_error(atom() | 'see-other-host'(), [text()]) -> string(). -format_stream_error(Reason, Txt) -> - Slogan = case Reason of - undefined -> "no reason"; - #'see-other-host'{} -> "see-other-host"; - _ -> atom_to_list(Reason) - end, - case xmpp:get_text(Txt) of - <<"">> -> - Slogan; - Data -> - binary_to_list(Data) ++ " (" ++ Slogan ++ ")" - end. - -spec format_sasl_error(cyrsasl:mechanism(), atom()) -> {atom(), binary()}. format_sasl_error(<<"EXTERNAL">>, Err) -> xmpp_stream_pkix:format_error(Err); diff --git a/src/xmpp_stream_out.erl b/src/xmpp_stream_out.erl index 8552ec749..5401d3073 100644 --- a/src/xmpp_stream_out.erl +++ b/src/xmpp_stream_out.erl @@ -217,10 +217,10 @@ format_error({pkix, Reason}) -> format("Peer certificate rejected: ~s", [ErrTxt]); format_error({stream, reset}) -> <<"Stream reset by peer">>; -format_error({stream, {in, #stream_error{reason = Reason, text = Txt}}}) -> - format("Stream closed by peer: ~s", [format_stream_error(Reason, Txt)]); -format_error({stream, {out, #stream_error{reason = Reason, text = Txt}}}) -> - format("Stream closed by us: ~s", [format_stream_error(Reason, Txt)]); +format_error({stream, {in, #stream_error{} = Err}}) -> + format("Stream closed by peer: ~s", [xmpp:format_stream_error(Err)]); +format_error({stream, {out, #stream_error{} = Err}}) -> + format("Stream closed by us: ~s", [xmpp:format_stream_error(Err)]); format_error({tls, Reason}) -> format("TLS failed: ~s", [format_tls_error(Reason)]); format_error({auth, Reason}) -> @@ -672,7 +672,7 @@ process_sasl_success(#{socket := Socket} = State) -> -spec process_sasl_failure(sasl_failure() | binary(), state()) -> state(). process_sasl_failure(#sasl_failure{} = Failure, State) -> Reason = format("Peer responded with error: ~s", - [format_sasl_failure(Failure)]), + [xmpp:format_sasl_error(Failure)]), process_sasl_failure(Reason, State); process_sasl_failure(Reason, State) -> try callback(handle_auth_failure, <<"EXTERNAL">>, {auth, Reason}, State) @@ -817,37 +817,12 @@ format_inet_error(Reason) -> Txt -> Txt end. --spec format_stream_error(atom() | 'see-other-host'(), [text()]) -> string(). -format_stream_error(Reason, Txt) -> - Slogan = case Reason of - undefined -> "no reason"; - #'see-other-host'{} -> "see-other-host"; - _ -> atom_to_list(Reason) - end, - case xmpp:get_text(Txt) of - <<"">> -> - Slogan; - Data -> - binary_to_list(Data) ++ " (" ++ Slogan ++ ")" - end. - -spec format_tls_error(atom() | binary()) -> list(). format_tls_error(Reason) when is_atom(Reason) -> format_inet_error(Reason); format_tls_error(Reason) -> binary_to_list(Reason). -format_sasl_failure(#sasl_failure{reason = Reason, text = Txt}) -> - Slogan = case Reason of - undefined -> "no reason"; - _ -> atom_to_list(Reason) - end, - case xmpp:get_text(Txt) of - <<"">> -> Slogan; - Data -> - binary_to_list(Data) ++ " (" ++ Slogan ++ ")" - end. - -spec format(io:format(), list()) -> binary(). format(Fmt, Args) -> iolist_to_binary(io_lib:format(Fmt, Args)).