From 6a0b3efb08ab9e367aa2b416555f3c230505bdc2 Mon Sep 17 00:00:00 2001 From: Ali Sabil Date: Thu, 28 Mar 2013 14:46:18 +0100 Subject: Catch badmatch errors when using file:sendfile --- src/ranch_tcp.erl | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/ranch_tcp.erl b/src/ranch_tcp.erl index bb7ad55..b54ae70 100644 --- a/src/ranch_tcp.erl +++ b/src/ranch_tcp.erl @@ -114,7 +114,15 @@ send(Socket, Packet) -> -spec sendfile(inet:socket(), file:name()) -> {ok, non_neg_integer()} | {error, atom()}. sendfile(Socket, Filename) -> - file:sendfile(Filename, Socket). + try file:sendfile(Filename, Socket) of + Result -> Result + catch + error:{badmatch, {error, enotconn}} -> + %% file:sendfile/2 might fail by throwing a {badmatch, {error, enotconn}} + %% this is because its internal implementation fails with a badmatch in + %% prim_file:sendfile/10 if the socket is not connected. + {error, closed} + end. %% @doc Set options on the given socket. %% @see inet:setopts/2 -- cgit v1.2.3