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

Commit 257dcf5

Browse files
author
Junchao Wu
committed
change ProtocolError to ProtocolException
1 parent 9e6e6f7 commit 257dcf5

File tree

6 files changed

+65
-64
lines changed

6 files changed

+65
-64
lines changed

tchannel/event.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ class EventType(IntEnum):
4646
before_receive_response = 0x30
4747
after_receive_response = 0x31
4848

49-
after_receive_protocol_error = 0x40
50-
after_send_protocol_error = 0x41
49+
after_receive_error = 0x40
50+
after_send_error = 0x41
5151

5252

5353
class EventHook(object):
@@ -95,11 +95,11 @@ def after_receive_response(self, response):
9595
"""Called after a ``CALL_RESP`` message is read."""
9696
pass
9797

98-
def after_receive_protocol_error(self, error):
98+
def after_receive_error(self, err):
9999
"""Called after a ``error`` message is read."""
100100
pass
101101

102-
def after_send_protocol_error(self, error):
102+
def after_send_error(self, err):
103103
"""Called after a ``error`` message is sent."""
104104
pass
105105

tchannel/exceptions.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,27 @@ class TChannelException(Exception):
2828

2929
class ProtocolException(TChannelException):
3030
"""Represent a protocol-level exception"""
31-
pass
31+
__slots__ = (
32+
'code',
33+
'description',
34+
'id',
35+
'tracing',
36+
)
37+
38+
def __init__(
39+
self,
40+
code,
41+
description,
42+
id=None,
43+
tracing=None,
44+
):
45+
super(TChannelException, self).__init__(
46+
'TChannel protocol error (%s)' % description
47+
)
48+
self.code = code
49+
self.tracing = tracing
50+
self.id = id
51+
self.description = description
3252

3353

3454
class InvalidMessageException(ProtocolException):

tchannel/tornado/connection.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@
3636
from ..event import EventType
3737
from ..exceptions import ConnectionClosedException
3838
from ..exceptions import InvalidErrorCodeException
39-
from ..exceptions import TChannelException
4039
from ..io import BytesIO
4140
from ..messages.common import PROTOCOL_VERSION
4241
from ..messages.common import FlagsType
4342
from ..messages.error import ErrorMessage
4443
from ..messages.types import Types
45-
from .message_factory import build_protocol_error
44+
from .message_factory import build_protocol_exception
4645
from .message_factory import MessageFactory
4746

4847
try:
@@ -227,11 +226,11 @@ def _loop(self):
227226
future = self._outstanding.pop(context.message_id)
228227

229228
if context.message.message_type == Types.ERROR:
230-
protocol_error = build_protocol_error(
229+
protocol_exception = build_protocol_exception(
231230
context.message,
232231
context.message_id,
233232
)
234-
future.set_exception(protocol_error)
233+
future.set_exception(protocol_exception)
235234
continue
236235

237236
if response and future.running():
@@ -609,15 +608,13 @@ def send_request(self, request):
609608

610609
def adapt_result(self, f, request, response_future):
611610
if f.exception():
612-
protocol_error = f.exception()
613-
protocol_error.tracing = request.tracing
614-
response_future.set_exception(
615-
TChannelException(protocol_error.description)
616-
)
611+
protocol_exception = f.exception()
612+
protocol_exception.tracing = request.tracing
613+
response_future.set_exception(protocol_exception)
617614
# event: after_receive_protocol_error
618615
self.tchannel.event_emitter.fire(
619-
EventType.after_receive_protocol_error,
620-
protocol_error,
616+
EventType.after_receive_error,
617+
protocol_exception,
621618
)
622619
else:
623620
response = f.result()

tchannel/tornado/data.py

+20-36
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ class Request(object):
4242

4343
# TODO decide which elements inside "message" object to expose to user.
4444
def __init__(
45-
self,
46-
id=None,
47-
flags=FlagsType.none,
48-
ttl=10,
49-
tracing=None,
50-
service=None,
51-
headers=None,
52-
checksum=None,
53-
argstreams=None,
54-
scheme=None,
45+
self,
46+
id=None,
47+
flags=FlagsType.none,
48+
ttl=10,
49+
tracing=None,
50+
service=None,
51+
headers=None,
52+
checksum=None,
53+
argstreams=None,
54+
scheme=None,
5555
):
5656
self.flags = flags
5757
self.ttl = ttl
@@ -133,16 +133,16 @@ class Response(object):
133133

134134
# TODO decide which elements inside "message" object to expose to user.
135135
def __init__(
136-
self,
137-
connection=None,
138-
id=None,
139-
flags=FlagsType.none,
140-
code=0,
141-
tracing=None,
142-
headers=None,
143-
checksum=None,
144-
argstreams=None,
145-
scheme=None,
136+
self,
137+
connection=None,
138+
id=None,
139+
flags=FlagsType.none,
140+
code=0,
141+
tracing=None,
142+
headers=None,
143+
checksum=None,
144+
argstreams=None,
145+
scheme=None,
146146
):
147147

148148
self.flags = flags or StatusCode.ok
@@ -316,19 +316,3 @@ def close_argstreams(self, force=False):
316316
for stream in self.argstreams:
317317
if stream.auto_close or force:
318318
stream.close()
319-
320-
321-
class ProtocolError(Exception):
322-
"""Object to represent protocol error message"""
323-
324-
def __init__(
325-
self,
326-
code,
327-
description,
328-
id=None,
329-
tracing=None,
330-
):
331-
self.code = code
332-
self.tracing = tracing
333-
self.id = id
334-
self.description = description

tchannel/tornado/message_factory.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,31 @@
3737
from ..messages.error import ErrorMessage
3838
from ..zipkin.annotation import Endpoint
3939
from ..zipkin.trace import Trace
40-
from .data import ProtocolError
40+
from ..exceptions import ProtocolException
4141
from .data import Request
4242
from .data import Response
4343
from .stream import InMemStream
4444

4545

46-
def build_raw_error_message(protocol_error):
46+
def build_raw_error_message(protocol_exception):
4747
"""build protocol level error message based on Error object"""
4848
message = ErrorMessage(
49-
code=protocol_error.code,
49+
code=protocol_exception.code,
5050
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,
51+
protocol_exception.tracing.span_id,
52+
protocol_exception.tracing.parent_span_id,
53+
protocol_exception.tracing.trace_id,
54+
protocol_exception.tracing.traceflags),
55+
description=protocol_exception.description,
5656
)
5757

5858
return message
5959

6060

61-
def build_protocol_error(message, message_id=None):
61+
def build_protocol_exception(message, message_id=None):
6262
"""build protocol level error message based on Error object"""
6363

64-
error = ProtocolError(
64+
error = ProtocolException(
6565
code=message.code,
6666
description=message.description,
6767
id=message_id,

tchannel/zipkin/zipkin_trace.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ def after_receive_response(self, response):
7171
response.tracing.annotations.append(ann)
7272
self.tracer.record([(response.tracing, response.tracing.annotations)])
7373

74-
def after_receive_protocol_error(self, error):
74+
def after_receive_error(self, error):
7575
if not error.tracing.traceflags:
7676
return
7777

7878
ann = annotation.client_recv()
7979
error.tracing.annotations.append(ann)
8080
self.tracer.record([(error.tracing, error.tracing.annotations)])
8181

82-
def after_send_protocol_error(self, error):
82+
def after_send_error(self, error):
8383
if not error.tracing.traceflags:
8484
return
8585

0 commit comments

Comments
 (0)