Skip to content

Commit 59dd823

Browse files
committed
Avoid repetition on error handling
We group the repeated code in one place to make it more clear that the code for all error paths is the same, except for the logging. We also update the set in-place instead of creating a new one on every iteration. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent fbf2f47 commit 59dd823

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/frequenz/sdk/actor/power_distributing/_component_managers/_battery_manager.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -652,40 +652,38 @@ def _parse_result(
652652

653653
for inverter_id, aws in tasks.items():
654654
battery_ids = self._inv_bats_map[inverter_id]
655+
failed = True
655656
try:
656657
aws.result()
658+
failed = False
657659
except OperationOutOfRange as err:
658-
failed_power += distribution[inverter_id]
659-
failed_batteries = failed_batteries.union(battery_ids)
660660
_logger.debug(
661661
"Set power for battery %s failed due to out of range error: %s",
662662
battery_ids,
663663
err,
664664
)
665665
except ClientError as err:
666-
failed_power += distribution[inverter_id]
667-
failed_batteries = failed_batteries.union(battery_ids)
668666
_logger.warning(
669667
"Set power for battery %s failed, mark it as broken. Error: %s",
670668
battery_ids,
671669
err,
672670
)
673671
except asyncio.exceptions.CancelledError:
674-
failed_power += distribution[inverter_id]
675-
failed_batteries = failed_batteries.union(battery_ids)
676672
_logger.warning(
677673
"Battery %s didn't respond in %f sec. Mark it as broken.",
678674
battery_ids,
679675
request_timeout.total_seconds(),
680676
)
681677
except Exception: # pylint: disable=broad-except
682-
failed_power += distribution[inverter_id]
683-
failed_batteries = failed_batteries.union(battery_ids)
684678
_logger.exception(
685679
"Unknown error while setting power to batteries: %s",
686680
battery_ids,
687681
)
688682

683+
if failed:
684+
failed_power += distribution[inverter_id]
685+
failed_batteries.update(battery_ids)
686+
689687
return failed_power, failed_batteries
690688

691689
async def _cancel_tasks(

0 commit comments

Comments
 (0)