Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit 9e6e6f7

Browse files
author
Junchao Wu
committed
refactor the protocolerror class and usage
1 parent 41f12e3 commit 9e6e6f7

File tree

3 files changed

+37
-49
lines changed

3 files changed

+37
-49
lines changed

Diff for: tchannel/tornado/connection.py

+9-23
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
from ..messages.common import FlagsType
4343
from ..messages.error import ErrorMessage
4444
from ..messages.types import Types
45+
from .message_factory import build_protocol_error
4546
from .message_factory import MessageFactory
4647

4748
try:
@@ -218,7 +219,6 @@ def _loop(self):
218219
# keep continue message in the list
219220
# pop all other type messages including error message
220221

221-
result = {"type": context.message.message_type}
222222
if (context.message.message_type in self.CALL_RES_TYPES and
223223
context.message.flags == FlagsType.fragment):
224224
# still streaming, keep it for record
@@ -227,27 +227,22 @@ def _loop(self):
227227
future = self._outstanding.pop(context.message_id)
228228

229229
if context.message.message_type == Types.ERROR:
230-
protocol_error = (
231-
self.response_message_factory.build_protocol_error(
232-
context.message,
233-
context.message_id,
234-
)
230+
protocol_error = build_protocol_error(
231+
context.message,
232+
context.message_id,
235233
)
236-
result['value'] = protocol_error
237-
future.set_result(result)
234+
future.set_exception(protocol_error)
238235
continue
239236

240237
if response and future.running():
241-
result['value'] = response
242-
future.set_result(result)
238+
future.set_result(response)
243239
continue
244240

245241
log.warn('Unconsumed message %s', context)
246242

247243
# Basically, the only difference between send and write is that send
248244
# sets up a Future to get the response. That's ideal for peers making
249245
# calls. Peers responding to calls must use write.
250-
251246
def send(self, message, message_id=None):
252247
"""Send the given message up the wire.
253248
Use this for messages which have a response message.
@@ -614,16 +609,7 @@ def send_request(self, request):
614609

615610
def adapt_result(self, f, request, response_future):
616611
if f.exception():
617-
# TODO unexpected exception
618-
response_future.set_exception(
619-
f.exception()
620-
)
621-
return
622-
623-
result = f.result()
624-
625-
if result['type'] == Types.ERROR:
626-
protocol_error = result['value']
612+
protocol_error = f.exception()
627613
protocol_error.tracing = request.tracing
628614
response_future.set_exception(
629615
TChannelException(protocol_error.description)
@@ -633,8 +619,8 @@ def adapt_result(self, f, request, response_future):
633619
EventType.after_receive_protocol_error,
634620
protocol_error,
635621
)
636-
elif result['type'] == Types.CALL_RES:
637-
response = result['value']
622+
else:
623+
response = f.result()
638624
response.tracing = request.tracing
639625
response_future.set_result(response)
640626
# event: after_receive_response

Diff for: tchannel/tornado/data.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ def close_argstreams(self, force=False):
318318
stream.close()
319319

320320

321-
class ProtocolError(object):
321+
class ProtocolError(Exception):
322322
"""Object to represent protocol error message"""
323323

324324
def __init__(

Diff for: tchannel/tornado/message_factory.py

+27-25
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,33 @@
4343
from .stream import InMemStream
4444

4545

46+
def build_raw_error_message(protocol_error):
47+
"""build protocol level error message based on Error object"""
48+
message = ErrorMessage(
49+
code=protocol_error.code,
50+
tracing=Tracing(
51+
protocol_error.tracing.span_id,
52+
protocol_error.tracing.parent_span_id,
53+
protocol_error.tracing.trace_id,
54+
protocol_error.tracing.traceflags),
55+
description=protocol_error.description,
56+
)
57+
58+
return message
59+
60+
61+
def build_protocol_error(message, message_id=None):
62+
"""build protocol level error message based on Error object"""
63+
64+
error = ProtocolError(
65+
code=message.code,
66+
description=message.description,
67+
id=message_id,
68+
)
69+
70+
return error
71+
72+
4673
class MessageFactory(object):
4774
"""Provide the functionality to decompose and recompose
4875
streaming messages.
@@ -58,31 +85,6 @@ def __init__(self, remote_host=None, remote_host_port=None):
5885
self.in_checksum = {}
5986
self.out_checksum = {}
6087

61-
def build_raw_error_message(self, protocol_error):
62-
"""build protocol level error message based on Error object"""
63-
message = ErrorMessage(
64-
code=protocol_error.code,
65-
tracing=protocol_error.Tracing(
66-
protocol_error.tracing.span_id,
67-
protocol_error.tracing.parent_span_id,
68-
protocol_error.tracing.trace_id,
69-
protocol_error.tracing.traceflags),
70-
description=protocol_error.description,
71-
)
72-
73-
return message
74-
75-
def build_protocol_error(self, message, message_id=None):
76-
"""build protocol level error message based on Error object"""
77-
78-
error = ProtocolError(
79-
code=message.code,
80-
description=message.description,
81-
id=message_id,
82-
)
83-
84-
return error
85-
8688
def build_raw_request_message(self, request, args, is_completed=False):
8789
"""build protocol level message based on request and args.
8890

0 commit comments

Comments
 (0)