Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle ValueError on keyboard press #1666

Merged
merged 1 commit into from
May 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion backend/systembridgebackend/server/keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@ async def handler_keyboard(
)

if "key" in request.json:
keyboard_keypress(request.json["key"])
try:
keyboard_keypress(request.json["key"])
except ValueError as err:
return json(
{
"message": err.args[0],
},
status=400,
)

return json(
{
"message": "Keypress sent",
Expand Down
17 changes: 15 additions & 2 deletions backend/systembridgebackend/server/websocket.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""System Bridge: WebSocket handler"""
from json import JSONDecodeError, dumps, loads
from collections.abc import Callable
from json import JSONDecodeError, dumps, loads
from uuid import uuid4

from systembridgeshared.base import Base
Expand Down Expand Up @@ -176,7 +176,20 @@ async def handler(self) -> None:
)
continue

keyboard_keypress(data["key"])
try:
keyboard_keypress(data["key"])
except ValueError as err:
self._logger.warning(err.args[0])
await self._websocket.send(
dumps(
{
EVENT_TYPE: TYPE_ERROR,
EVENT_SUBTYPE: SUBTYPE_MISSING_KEY,
EVENT_MESSAGE: "Invalid key",
}
)
)
continue

await self._websocket.send(
dumps(
Expand Down
1 change: 1 addition & 0 deletions connector/systembridgeconnector/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# Event Types
SUBTYPE_BAD_API_KEY = "BAD_API_KEY"
SUBTYPE_BAD_JSON = "BAD_JSON"
SUBTYPE_BAD_KEY = "MISSING_KEY"
SUBTYPE_LISTENER_ALREADY_REGISTERED = "LISTENER_ALREADY_REGISTERED"
SUBTYPE_LISTENER_NOT_REGISTERED = "LISTENER_NOT_REGISTERED"
SUBTYPE_MISSING_API_KEY = "MISSING_API_KEY"
Expand Down
1 change: 1 addition & 0 deletions shared/systembridgeshared/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
# Event Types
SUBTYPE_BAD_API_KEY = "BAD_API_KEY"
SUBTYPE_BAD_JSON = "BAD_JSON"
SUBTYPE_BAD_KEY = "MISSING_KEY"
SUBTYPE_LISTENER_ALREADY_REGISTERED = "LISTENER_ALREADY_REGISTERED"
SUBTYPE_LISTENER_NOT_REGISTERED = "LISTENER_NOT_REGISTERED"
SUBTYPE_MISSING_API_KEY = "MISSING_API_KEY"
Expand Down