Skip to content

Commit

Permalink
Add optional callback ranch_transport:format_error/1
Browse files Browse the repository at this point in the history
  • Loading branch information
juhlig committed Nov 18, 2024
1 parent fd895dd commit 5de33d5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/ranch_acceptors_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ listen_error(Ref, Transport, TransOpts0, Reason, Logger) ->
TransOpts = TransOpts0#{socket_opts => SocketOpts},
ranch:log(error,
"Failed to start Ranch listener ~p in ~p:listen(~999999p) for reason ~p (~s)~n",
[Ref, Transport, TransOpts, Reason, format_error(Reason)], Logger),
[Ref, Transport, TransOpts, Reason, format_error(Transport, Reason)], Logger),
exit({listen_error, Ref, Reason}).

hide_socket_opts([]) ->
Expand All @@ -106,9 +106,14 @@ hide_socket_opts([{password, _}|SocketOpts]) ->
hide_socket_opts([SocketOpt|SocketOpts]) ->
[SocketOpt|hide_socket_opts(SocketOpts)].

format_error(no_cert) ->
format_error(_, no_cert) ->
"no certificate provided; see cert, certfile, sni_fun or sni_hosts options";
format_error(reuseport_local) ->
format_error(_, reuseport_local) ->
"num_listen_sockets must be set to 1 for local sockets";
format_error(Reason) ->
inet:format_error(Reason).
format_error(Transport, Reason) ->
case erlang:function_exported(Transport, format_error, 1) of
true ->
Transport:format_error(Reason);
false ->
io_lib:format("~999999p", [Reason])
end.
7 changes: 7 additions & 0 deletions src/ranch_ssl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
-export([shutdown/2]).
-export([close/1]).
-export([cleanup/1]).
-export([format_error/1]).

-type ssl_opt() :: {alpn_preferred_protocols, [binary()]}
| {anti_replay, '10k' | '100k' | {integer(), integer(), integer()}}
Expand Down Expand Up @@ -328,6 +329,12 @@ cleanup(#{socket_opts:=SocketOpts}) ->
cleanup(_) ->
ok.

-spec format_error({error, ssl:reason()} | ssl:reason()) -> string().
format_error(no_cert) ->
"no certificate provided; see cert, certfile, sni_fun or sni_hosts options";
format_error(Reason) ->
ssl:format_error(Reason).

get_tls_versions(SocketOpts) ->
%% Socket options need to be reversed for keyfind because later options
%% take precedence when contained multiple times, but keyfind will return
Expand Down
5 changes: 5 additions & 0 deletions src/ranch_tcp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
-export([shutdown/2]).
-export([close/1]).
-export([cleanup/1]).
-export([format_error/1]).

-type opt() :: {backlog, non_neg_integer()}
| {buffer, non_neg_integer()}
Expand Down Expand Up @@ -285,3 +286,7 @@ cleanup(#{socket_opts:=SocketOpts}) ->
end;
cleanup(_) ->
ok.

-spec format_error(inet:posix() | system_limit) -> string().
format_error(Reason) ->
inet:format_error(Reason).
3 changes: 3 additions & 0 deletions src/ranch_transport.erl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
-> ok | {error, atom()}.
-callback close(socket()) -> ok.
-callback cleanup(ranch:transport_opts(any())) -> ok.
-callback format_error(term()) -> string().

-optional_callbacks([format_error/1]).

%% A fallback for transports that don't have a native sendfile implementation.
%% Note that the ordering of arguments is different from file:sendfile/5 and
Expand Down

0 comments on commit 5de33d5

Please sign in to comment.