Skip to content

Commit 56f46a1

Browse files
committed
Make the python 2 async observer send graphql error for exceptions explicitly returned
1 parent 4554636 commit 56f46a1

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

graphql_ws/base_sync.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ def __init__(
6565
self.send_message = send_message
6666

6767
def on_next(self, value):
68-
self.send_execution_result(self.connection_context, self.op_id, value)
68+
if isinstance(value, Exception):
69+
send_method = self.send_error
70+
else:
71+
send_method = self.send_execution_result
72+
send_method(self.connection_context, self.op_id, value)
6973

7074
def on_completed(self):
7175
self.send_message(self.connection_context, self.op_id, GQL_COMPLETE)

tests/test_base.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pytest
99

1010
from graphql_ws import base
11+
from graphql_ws.base_sync import SubscriptionObserver
1112

1213

1314
def test_not_implemented():
@@ -77,3 +78,35 @@ def test_context_operations():
7778
assert not context.has_operation(1)
7879
# Removing a non-existant operation fails silently.
7980
context.remove_operation(999)
81+
82+
83+
def test_observer_data():
84+
ws = mock.Mock()
85+
context = base.BaseConnectionContext(ws)
86+
send_result, send_error, send_message = mock.Mock(), mock.Mock(), mock.Mock()
87+
observer = SubscriptionObserver(
88+
connection_context=context,
89+
op_id=1,
90+
send_execution_result=send_result,
91+
send_error=send_error,
92+
send_message=send_message,
93+
)
94+
observer.on_next('data')
95+
assert send_result.called
96+
assert not send_error.called
97+
98+
99+
def test_observer_exception():
100+
ws = mock.Mock()
101+
context = base.BaseConnectionContext(ws)
102+
send_result, send_error, send_message = mock.Mock(), mock.Mock(), mock.Mock()
103+
observer = SubscriptionObserver(
104+
connection_context=context,
105+
op_id=1,
106+
send_execution_result=send_result,
107+
send_error=send_error,
108+
send_message=send_message,
109+
)
110+
observer.on_next(TypeError('some bad message'))
111+
assert send_error.called
112+
assert not send_result.called

0 commit comments

Comments
 (0)