|
24 | 24 | import logging
|
25 | 25 | from fastapi import WebSocket, WebSocketDisconnect
|
26 | 26 | from fastapi.encoders import jsonable_encoder
|
27 |
| -from typing import TYPE_CHECKING |
| 27 | +from typing import TYPE_CHECKING, Literal |
| 28 | +from .exceptions import PropertyNotObservableError |
28 | 29 |
|
29 | 30 | if TYPE_CHECKING:
|
30 | 31 | from .thing import Thing
|
31 | 32 |
|
32 | 33 |
|
| 34 | +WEBTHING_ERROR_URL = "https://w3c.github.io/web-thing-protocol/errors" |
| 35 | + |
| 36 | + |
| 37 | +def observation_error_response( |
| 38 | + name: str, affordance_type: Literal["action", "property"], exception: Exception |
| 39 | +) -> dict[str, str | dict]: |
| 40 | + r"""Generate a websocket error response for observing an action or property. |
| 41 | +
|
| 42 | + When a websocket client asks to observe a property or action that either |
| 43 | + doesn't exist or isn't observable, this function makes a dictionary that |
| 44 | + can be returned to the client indicating an error. |
| 45 | +
|
| 46 | + :param name: The name of the affordance being observed. |
| 47 | + :param affordance_type: The type of the affordance. |
| 48 | + :param exception: The error that was raised. |
| 49 | + :returns: A dictionary that may be returned to the websocket. |
| 50 | +
|
| 51 | + :raises TypeError: if the exception is not a `KeyError` |
| 52 | + or `.PropertyNotObservableError`\ . |
| 53 | + """ |
| 54 | + if isinstance(exception, KeyError): |
| 55 | + error = { |
| 56 | + "status": "404", |
| 57 | + "type": f"{WEBTHING_ERROR_URL}#not-found", |
| 58 | + "title": "Not Found", |
| 59 | + "detail": f"No {affordance_type} found with the name '{name}'.", |
| 60 | + } |
| 61 | + elif isinstance(exception, PropertyNotObservableError): |
| 62 | + error = { |
| 63 | + "status": "403", |
| 64 | + "type": f"{WEBTHING_ERROR_URL}#not-observable", |
| 65 | + "title": "Not Observable", |
| 66 | + "detail": f"Property '{name}' is not observable.", |
| 67 | + } |
| 68 | + else: |
| 69 | + raise TypeError(f"Can't generate an error response for {exception}.") |
| 70 | + return { |
| 71 | + "messageType": "response", |
| 72 | + "operation": f"observe{affordance_type}", |
| 73 | + "name": name, |
| 74 | + "error": error, |
| 75 | + } |
| 76 | + |
| 77 | + |
33 | 78 | async def relay_notifications_to_websocket(
|
34 | 79 | websocket: WebSocket, receive_stream: ObjectReceiveStream
|
35 | 80 | ) -> None:
|
@@ -66,17 +111,23 @@ async def process_messages_from_websocket(
|
66 | 111 | while True:
|
67 | 112 | try:
|
68 | 113 | data = await websocket.receive_json()
|
69 |
| - if data["messageType"] == "addPropertyObservation": |
| 114 | + except WebSocketDisconnect: |
| 115 | + await send_stream.aclose() |
| 116 | + return |
| 117 | + if data["messageType"] == "addPropertyObservation": |
| 118 | + try: |
70 | 119 | for k in data["data"].keys():
|
71 | 120 | thing.observe_property(k, send_stream)
|
72 |
| - if data["messageType"] == "addActionObservation": |
| 121 | + except (KeyError, PropertyNotObservableError) as e: |
| 122 | + logging.error(f"Got a bad websocket message: {data}, caused {e!r}.") |
| 123 | + await send_stream.send(observation_error_response(k, "property", e)) |
| 124 | + if data["messageType"] == "addActionObservation": |
| 125 | + try: |
73 | 126 | for k in data["data"].keys():
|
74 | 127 | thing.observe_action(k, send_stream)
|
75 |
| - except KeyError as e: |
76 |
| - logging.error(f"Got a bad websocket message: {data}, caused KeyError({e})") |
77 |
| - except WebSocketDisconnect: |
78 |
| - await send_stream.aclose() |
79 |
| - return |
| 128 | + except KeyError as e: |
| 129 | + logging.error(f"Got a bad websocket message: {data}, caused {e!r}.") |
| 130 | + await send_stream.send(observation_error_response(k, "action", e)) |
80 | 131 |
|
81 | 132 |
|
82 | 133 | async def websocket_endpoint(thing: Thing, websocket: WebSocket) -> None:
|
|
0 commit comments