THRIFT-1593 Pass on errors like "connection closed" to the handler module
Patch: Björn Bylander
+ bump jsx

git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1340073 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/erl/src/thrift_processor.erl b/lib/erl/src/thrift_processor.erl
index 88af05a..d474294 100644
--- a/lib/erl/src/thrift_processor.erl
+++ b/lib/erl/src/thrift_processor.erl
@@ -32,25 +32,42 @@
                            service = Service,
                            handler = Handler}).
 
-loop(State0 = #thrift_processor{protocol  = Proto0}) ->
+loop(State0 = #thrift_processor{protocol  = Proto0,
+                                handler = Handler}) ->
     {Proto1, MessageBegin} = thrift_protocol:read(Proto0, message_begin),
     State1 = State0#thrift_processor{protocol = Proto1},
     case MessageBegin of
         #protocol_message_begin{name = Function,
                                 type = ?tMessageType_CALL,
                                 seqid = Seqid} ->
-            {State2, ok} = handle_function(State1, list_to_atom(Function), Seqid),
-            loop(State2);
+            case handle_function(State1, list_to_atom(Function), Seqid) of
+                {State2, ok} -> loop(State2);
+                {_State2, {error, Reason}} ->
+                    Handler:handle_error(list_to_atom(Function), Reason),
+                    thrift_protocol:close_transport(Proto1),
+                    ok
+            end;
         #protocol_message_begin{name = Function,
                                 type = ?tMessageType_ONEWAY, 
                                 seqid = Seqid} ->
-            {State2, ok} = handle_function(State1, list_to_atom(Function), Seqid),
-            loop(State2);
-        {error, timeout} ->
+            case handle_function(State1, list_to_atom(Function), Seqid) of
+                {State2, ok} -> loop(State2);
+                {_State2, {error, Reason}} ->
+                    Handler:handle_error(list_to_atom(Function), Reason),
+                    thrift_protocol:close_transport(Proto1),
+                    ok
+            end;
+        {error, timeout = Reason} ->
+            Handler:handle_error(undefined, Reason),
             thrift_protocol:close_transport(Proto1),
             ok;
-        {error, closed} ->
+        {error, closed = Reason} ->
             %% error_logger:info_msg("Client disconnected~n"),
+            Handler:handle_error(undefined, Reason),
+            thrift_protocol:close_transport(Proto1),
+            exit(shutdown);
+        {error, Reason} ->
+            Handler:handle_error(undefined, Reason),
             thrift_protocol:close_transport(Proto1),
             exit(shutdown)
     end.
@@ -175,11 +192,16 @@
     send_reply(State, Function, ?tMessageType_EXCEPTION, Reply, Seqid).
 
 send_reply(State = #thrift_processor{protocol = Proto0}, Function, ReplyMessageType, Reply, Seqid) ->
-    {Proto1, ok} = thrift_protocol:write(Proto0, #protocol_message_begin{
-                                           name = atom_to_list(Function),
-                                           type = ReplyMessageType,
-                                           seqid = Seqid}),
-    {Proto2, ok} = thrift_protocol:write(Proto1, Reply),
-    {Proto3, ok} = thrift_protocol:write(Proto2, message_end),
-    {Proto4, ok} = thrift_protocol:flush_transport(Proto3),
-    {State#thrift_processor{protocol = Proto4}, ok}.
+    try
+        {Proto1, ok} = thrift_protocol:write(Proto0, #protocol_message_begin{
+                                               name = atom_to_list(Function),
+                                               type = ReplyMessageType,
+                                               seqid = Seqid}),
+        {Proto2, ok} = thrift_protocol:write(Proto1, Reply),
+        {Proto3, ok} = thrift_protocol:write(Proto2, message_end),
+        {Proto4, ok} = thrift_protocol:flush_transport(Proto3),
+        {State#thrift_processor{protocol = Proto4}, ok}
+    catch
+        error:{badmatch, {_, {error, _} = Error}} ->
+            {State, Error}
+    end.