Skip to content

Commit 89964c9

Browse files
committed
Raise the new exceptions in the client
Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 999dbae commit 89964c9

File tree

2 files changed

+52
-46
lines changed

2 files changed

+52
-46
lines changed

src/frequenz/client/microgrid/_client.py

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,21 @@ async def components(self) -> Iterable[Component]:
9090
Iterator whose elements are all the components in the microgrid.
9191
9292
Raises:
93-
ClientError: If the connection to the Microgrid API cannot be established or
94-
when the api call exceeded the timeout.
93+
ClientError: If the are any errors communicating with the Microgrid API,
94+
most likely a subclass of
95+
[GrpcStatusError][frequenz.client.microgrid.GrpcStatusError].
9596
"""
9697
try:
9798
component_list = await self.api.list_components(
9899
pb_microgrid.ComponentFilter(),
99100
timeout=int(DEFAULT_GRPC_CALL_TIMEOUT),
100101
)
101-
102-
except grpclib.GRPCError as err:
103-
raise ClientError(
104-
f"Failed to list components. Microgrid API: {self._server_url}. Err: {err}"
105-
) from err
102+
except grpclib.GRPCError as grpc_error:
103+
raise ClientError.from_grpc_error(
104+
server_url=self._server_url,
105+
operation="list_components",
106+
grpc_error=grpc_error,
107+
) from grpc_error
106108

107109
components_only = filter(
108110
lambda c: c.category
@@ -168,8 +170,9 @@ async def connections(
168170
Microgrid connections matching the provided start and end filters.
169171
170172
Raises:
171-
ClientError: If the connection to the Microgrid API cannot be established or
172-
when the api call exceeded the timeout.
173+
ClientError: If the are any errors communicating with the Microgrid API,
174+
most likely a subclass of
175+
[GrpcStatusError][frequenz.client.microgrid.GrpcStatusError].
173176
"""
174177
connection_filter = pb_microgrid.ConnectionFilter(
175178
starts=list(starts), ends=list(ends)
@@ -182,10 +185,12 @@ async def connections(
182185
timeout=int(DEFAULT_GRPC_CALL_TIMEOUT),
183186
),
184187
)
185-
except grpclib.GRPCError as err:
186-
raise ClientError(
187-
f"Failed to list connections. Microgrid API: {self._server_url}. Err: {err}"
188-
) from err
188+
except grpclib.GRPCError as grpc_error:
189+
raise ClientError.from_grpc_error(
190+
server_url=self._server_url,
191+
operation="list_connections",
192+
grpc_error=grpc_error,
193+
) from grpc_error
189194
# Filter out the components filtered in `components` method.
190195
# id=0 is an exception indicating grid component.
191196
valid_ids = {c.component_id for c in valid_components}
@@ -384,8 +389,9 @@ async def set_power(self, component_id: int, power_w: float) -> None:
384389
power_w: power to set for the component.
385390
386391
Raises:
387-
ClientError: If the connection to the Microgrid API cannot be established or
388-
when the api call exceeded the timeout.
392+
ClientError: If the are any errors communicating with the Microgrid API,
393+
most likely a subclass of
394+
[GrpcStatusError][frequenz.client.microgrid.GrpcStatusError].
389395
"""
390396
try:
391397
await self.api.set_power_active(
@@ -394,10 +400,12 @@ async def set_power(self, component_id: int, power_w: float) -> None:
394400
),
395401
timeout=int(DEFAULT_GRPC_CALL_TIMEOUT),
396402
)
397-
except grpclib.GRPCError as err:
398-
raise ClientError(
399-
f"Failed to set power. Microgrid API: {self._server_url}. Err: {err}"
400-
) from err
403+
except grpclib.GRPCError as grpc_error:
404+
raise ClientError.from_grpc_error(
405+
server_url=self._server_url,
406+
operation="set_power_active",
407+
grpc_error=grpc_error,
408+
) from grpc_error
401409

402410
async def set_bounds(
403411
self,
@@ -415,10 +423,10 @@ async def set_bounds(
415423
Raises:
416424
ValueError: when upper bound is less than 0, or when lower bound is
417425
greater than 0.
418-
ClientError: If the connection to the Microgrid API cannot be established or
419-
when the api call exceeded the timeout.
426+
ClientError: If the are any errors communicating with the Microgrid API,
427+
most likely a subclass of
428+
[GrpcStatusError][frequenz.client.microgrid.GrpcStatusError].
420429
"""
421-
api_details = f"Microgrid API: {self._server_url}."
422430
if upper < 0:
423431
raise ValueError(f"Upper bound {upper} must be greater than or equal to 0.")
424432
if lower > 0:
@@ -436,15 +444,9 @@ async def set_bounds(
436444
),
437445
timeout=int(DEFAULT_GRPC_CALL_TIMEOUT),
438446
)
439-
except grpclib.GRPCError as err:
440-
_logger.error(
441-
"set_bounds write failed: %s, for message: %s, api: %s. Err: %s",
442-
err,
443-
next,
444-
api_details,
445-
err,
446-
)
447-
raise ClientError(
448-
f"Failed to set inclusion bounds. Microgrid API: {self._server_url}. "
449-
f"Err: {err}"
450-
) from err
447+
except grpclib.GRPCError as grpc_error:
448+
raise ClientError.from_grpc_error(
449+
server_url=self._server_url,
450+
operation="add_inclusion_bounds",
451+
grpc_error=grpc_error,
452+
) from grpc_error

tests/test_client.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,13 @@ async def test_components_grpc_error() -> None:
187187
"""Test the components() method when the gRPC call fails."""
188188
client = _TestClient()
189189
client.mock_stub.list_components.side_effect = grpclib.GRPCError(
190-
mock.MagicMock(name="mock status"), "fake grpc error"
190+
mock.MagicMock(name="mock_status"), "fake grpc error", "fake details"
191191
)
192192
with pytest.raises(
193193
ClientError,
194-
match="Failed to list components. Microgrid API: grpc://mock_host:1234. "
195-
"Err: .*fake grpc error",
194+
match=r"Failed calling 'list_components' on 'grpc://mock_host:1234': .* "
195+
r"<status=<MagicMock name='mock_status\.name' id='.*'>>: fake grpc error "
196+
r"\(fake details\)",
196197
):
197198
await client.components()
198199

@@ -327,12 +328,13 @@ async def test_connections_grpc_error() -> None:
327328
"""Test the components() method when the gRPC call fails."""
328329
client = _TestClient()
329330
client.mock_stub.list_connections.side_effect = grpclib.GRPCError(
330-
mock.MagicMock(name="mock status"), "fake grpc error"
331+
mock.MagicMock(name="mock_status"), "fake grpc error", "fake details"
331332
)
332333
with pytest.raises(
333334
ClientError,
334-
match="Failed to list connections. Microgrid API: grpc://mock_host:1234. "
335-
"Err: .*fake grpc error",
335+
match=r"Failed calling 'list_connections' on 'grpc://mock_host:1234': .* "
336+
r"<status=<MagicMock name='mock_status\.name' id='.*'>>: fake grpc error "
337+
r"\(fake details\)",
336338
):
337339
await client.connections()
338340

@@ -543,12 +545,13 @@ async def test_set_power_grpc_error() -> None:
543545
"""Test set_power() raises ClientError when the gRPC call fails."""
544546
client = _TestClient()
545547
client.mock_stub.set_power_active.side_effect = grpclib.GRPCError(
546-
mock.MagicMock(name="mock status"), "fake grpc error"
548+
mock.MagicMock(name="mock_status"), "fake grpc error", "fake details"
547549
)
548550
with pytest.raises(
549551
ClientError,
550-
match="Failed to set power. Microgrid API: grpc://mock_host:1234. "
551-
"Err: .*fake grpc error",
552+
match=r"Failed calling 'set_power_active' on 'grpc://mock_host:1234': .* "
553+
r"<status=<MagicMock name='mock_status\.name' id='.*'>>: fake grpc error "
554+
r"\(fake details\)",
552555
):
553556
await client.set_power(component_id=83, power_w=100.0)
554557

@@ -609,11 +612,12 @@ async def test_set_bounds_grpc_error() -> None:
609612
"""Test the components() method when the gRPC call fails."""
610613
client = _TestClient()
611614
client.mock_stub.add_inclusion_bounds.side_effect = grpclib.GRPCError(
612-
mock.MagicMock(name="mock status"), "fake grpc error"
615+
mock.MagicMock(name="mock_status"), "fake grpc error", "fake details"
613616
)
614617
with pytest.raises(
615618
ClientError,
616-
match="Failed to set inclusion bounds. Microgrid API: grpc://mock_host:1234. "
617-
"Err: .*fake grpc error",
619+
match=r"Failed calling 'add_inclusion_bounds' on 'grpc://mock_host:1234': .* "
620+
r"<status=<MagicMock name='mock_status\.name' id='.*'>>: fake grpc error "
621+
r"\(fake details\)",
618622
):
619623
await client.set_bounds(99, 0.0, 100.0)

0 commit comments

Comments
 (0)