diff options
-rw-r--r-- | lib/erl/rebar.config | 2 | ||||
-rw-r--r-- | 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 ec7798c0c..2728cb335 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 88af05a8d..d47429450 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. |