Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #150 from lendingblock/master
Browse files Browse the repository at this point in the history
1.1.2
  • Loading branch information
lsbardel authored Nov 22, 2018
2 parents 34ce728 + 91169f5 commit c6a7194
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
2 changes: 1 addition & 1 deletion openapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Minimal OpenAPI asynchronous server application
"""

__version__ = '1.1.1'
__version__ = '1.1.2'
26 changes: 14 additions & 12 deletions openapi/ws/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ async def get(self):
if sockets:
sockets.add(self)
#
async for msg in response:
if msg.type == web.WSMsgType.TEXT:
await self.on_message(msg)
try:
async for msg in response:
if msg.type == web.WSMsgType.TEXT:
await self.on_message(msg)
except (asyncio.CancelledError, asyncio.TimeoutError, RuntimeError):
pass

return response

Expand All @@ -83,7 +86,7 @@ def encode_message(self, msg):
"""
try:
return json.dumps(msg)
except json.JSONDecodeError:
except TypeError:
raise ProtocolError('JSON object expected') from None

async def on_message(self, msg):
Expand All @@ -109,7 +112,11 @@ async def on_message(self, msg):
))
except ProtocolError as exc:
logger.error('Protocol error: %s', exc)
await self.error_message(str(exc))
await self.error_message(
str(exc),
id=id_,
method=rpc.method if rpc else None
)
except ValidationErrors as exc:
await self.error_message(
'Invalid RPC parameters',
Expand All @@ -125,13 +132,8 @@ async def error_message(self, message, *, errors=None, **kw):
await self.write(compact(error=error, **kw))

async def write(self, msg: Dict) -> None:
try:
text = self.encode_message(msg)
await self.response.send_str(text)
except RuntimeError:
# TODO: is this the best way to avoid spamming exception
# when the websocket is closed by the client?
pass
text = self.encode_message(msg)
await self.response.send_str(text)


class Sockets:
Expand Down
12 changes: 12 additions & 0 deletions tests/example/ws.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import asyncio

from aiohttp import web

from openapi import ws
Expand All @@ -19,3 +21,13 @@ async def ws_rpc_echo(self, payload):
"""Echo parameters
"""
return payload

async def ws_rpc_cancel(self, payload):
"""Echo parameters
"""
raise asyncio.CancelledError

async def ws_rpc_badjson(self, payload):
"""Echo parameters
"""
return ApiPath
22 changes: 22 additions & 0 deletions tests/test_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,25 @@ async def test_rpc_pubsub(cli):
data = msg.json()
assert data['channel'] == 'test'
assert data['data'] == 'hello again'


async def test_cancelled(cli):
async with cli.ws_connect('/stream') as ws:
await ws.send_json(
dict(id='abc', method='cancel')
)
msg = await ws.receive()
assert msg.type == aiohttp.WSMsgType.CLOSE


async def test_badjson(cli):
async with cli.ws_connect('/stream') as ws:
await ws.send_json(
dict(id='abc', method='badjson')
)
msg = await ws.receive()
assert msg.type == aiohttp.WSMsgType.TEXT
data = msg.json()
assert data['id'] == 'abc'
assert data['error']
assert data['error']['message'] == 'JSON object expected'

0 comments on commit c6a7194

Please sign in to comment.