From 3f972b1cf146463a672a47d025502fd5b6f7c4bf Mon Sep 17 00:00:00 2001 From: Roger Meier Date: Fri, 18 May 2012 11:35:51 +0000 Subject: [PATCH] =?utf8?q?THRIFT-1593=20Pass=20on=20errors=20like=20"conne?= =?utf8?q?ction=20closed"=20to=20the=20handler=20module=20Patch:=20Bj?= =?utf8?q?=C3=B6rn=20Bylander=20+=20bump=20jsx?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1340073 13f79535-47bb-0310-9956-ffa450edef68 --- lib/erl/rebar.config | 2 +- lib/erl/src/thrift_processor.erl | 52 +++++++++++++++++++++++--------- 2 files changed, 38 insertions(+), 16 deletions(-) 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. -- 2.17.1