@@ -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
0 commit comments