From: Roger Meier Date: Fri, 18 May 2012 11:35:51 +0000 (+0000) Subject: THRIFT-1593 Pass on errors like "connection closed" to the handler module X-Git-Tag: 0.9.1~357 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=3f972b1cf146463a672a47d025502fd5b6f7c4bf;p=common%2Fthrift.git 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/rebar.config b/lib/erl/rebar.config index ec7798c0..2728cb33 100644 --- a/lib/erl/rebar.config +++ b/lib/erl/rebar.config @@ -1,5 +1,5 @@ {erl_opts, [debug_info]}. {lib_dirs, ["deps"]}. {deps, [ - { jsx, "0.9.0", {git, "git://github.com/talentdeficit/jsx.git", {tag, "v0.9.0"}}} + { jsx, "1.2.1", {git, "git://github.com/talentdeficit/jsx.git", {tag, "v1.2.1"}}} ]}. diff --git a/lib/erl/src/thrift_processor.erl b/lib/erl/src/thrift_processor.erl index 88af05a8..d4742945 100644 --- a/lib/erl/src/thrift_processor.erl +++ b/lib/erl/src/thrift_processor.erl @@ -32,25 +32,42 @@ init({_Server, ProtoGen, Service, Handler}) when is_function(ProtoGen, 0) -> 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 @@ handle_error(State, Function, Error, Seqid) -> 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.