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

Commit e5cdb9e

Browse files
reivilibresquahtx
andauthored
Make get_device return None if the device doesn't exist rather than raising an exception. (#11565)
Co-authored-by: Sean Quah <8349537+squahtx@users.noreply.github.com>
1 parent aa8708e commit e5cdb9e

File tree

6 files changed

+20
-13
lines changed

6 files changed

+20
-13
lines changed

changelog.d/11565.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make `get_device` return `None` if the device doesn't exist rather than raising an exception.

synapse/handlers/auth.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -997,9 +997,7 @@ async def create_access_token_for_user_id(
997997
# really don't want is active access_tokens without a record of the
998998
# device, so we double-check it here.
999999
if device_id is not None:
1000-
try:
1001-
await self.store.get_device(user_id, device_id)
1002-
except StoreError:
1000+
if await self.store.get_device(user_id, device_id) is None:
10031001
await self.store.delete_access_token(access_token)
10041002
raise StoreError(400, "Login raced against device deletion")
10051003

synapse/handlers/device.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ async def get_device(self, user_id: str, device_id: str) -> JsonDict:
106106
Raises:
107107
errors.NotFoundError: if the device was not found
108108
"""
109-
try:
110-
device = await self.store.get_device(user_id, device_id)
111-
except errors.StoreError:
112-
raise errors.NotFoundError
109+
device = await self.store.get_device(user_id, device_id)
110+
if device is None:
111+
raise errors.NotFoundError()
112+
113113
ips = await self.store.get_last_client_ip_by_device(user_id, device_id)
114114
_update_device_from_client_ips(device, ips)
115115

@@ -602,6 +602,8 @@ async def rehydrate_device(
602602
access_token, device_id
603603
)
604604
old_device = await self.store.get_device(user_id, old_device_id)
605+
if old_device is None:
606+
raise errors.NotFoundError()
605607
await self.store.update_device(user_id, device_id, old_device["display_name"])
606608
# can't call self.delete_device because that will clobber the
607609
# access token so call the storage layer directly

synapse/rest/admin/devices.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ async def on_GET(
6363
device = await self.device_handler.get_device(
6464
target_user.to_string(), device_id
6565
)
66+
if device is None:
67+
raise NotFoundError("No device found")
6668
return HTTPStatus.OK, device
6769

6870
async def on_DELETE(

synapse/rest/client/devices.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717
from typing import TYPE_CHECKING, Tuple
1818

1919
from synapse.api import errors
20+
from synapse.api.errors import NotFoundError
2021
from synapse.http.server import HttpServer
2122
from synapse.http.servlet import (
2223
RestServlet,
2324
assert_params_in_dict,
2425
parse_json_object_from_request,
2526
)
2627
from synapse.http.site import SynapseRequest
28+
from synapse.rest.client._base import client_patterns, interactive_auth_handler
2729
from synapse.types import JsonDict
2830

29-
from ._base import client_patterns, interactive_auth_handler
30-
3131
if TYPE_CHECKING:
3232
from synapse.server import HomeServer
3333

@@ -116,6 +116,8 @@ async def on_GET(
116116
device = await self.device_handler.get_device(
117117
requester.user.to_string(), device_id
118118
)
119+
if device is None:
120+
raise NotFoundError("No device found")
119121
return 200, device
120122

121123
@interactive_auth_handler

synapse/storage/databases/main/devices.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,23 +101,25 @@ def count_devices_by_users_txn(txn, user_ids):
101101
"count_devices_by_users", count_devices_by_users_txn, user_ids
102102
)
103103

104-
async def get_device(self, user_id: str, device_id: str) -> Dict[str, Any]:
104+
async def get_device(
105+
self, user_id: str, device_id: str
106+
) -> Optional[Dict[str, Any]]:
105107
"""Retrieve a device. Only returns devices that are not marked as
106108
hidden.
107109
108110
Args:
109111
user_id: The ID of the user which owns the device
110112
device_id: The ID of the device to retrieve
111113
Returns:
112-
A dict containing the device information
113-
Raises:
114-
StoreError: if the device is not found
114+
A dict containing the device information, or `None` if the device does not
115+
exist.
115116
"""
116117
return await self.db_pool.simple_select_one(
117118
table="devices",
118119
keyvalues={"user_id": user_id, "device_id": device_id, "hidden": False},
119120
retcols=("user_id", "device_id", "display_name"),
120121
desc="get_device",
122+
allow_none=True,
121123
)
122124

123125
async def get_devices_by_user(self, user_id: str) -> Dict[str, Dict[str, str]]:

0 commit comments

Comments
 (0)