-
Notifications
You must be signed in to change notification settings - Fork 7
feat: Add raise_on_errors parameter for strict parsing validation
#73
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
Open
eduardiazf
wants to merge
1
commit into
frequenz-floss:v0.x.x
Choose a base branch
from
eduardiazf:fix/parsing-component-connection-errors
base: v0.x.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+122
−13
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,13 +89,20 @@ def stub(self) -> assets_pb2_grpc.PlatformAssetsAsyncStub: | |
| return self._stub # type: ignore | ||
|
|
||
| async def get_microgrid( # noqa: DOC502 (raises ApiClientError indirectly) | ||
| self, microgrid_id: MicrogridId | ||
| self, | ||
| microgrid_id: MicrogridId, | ||
| *, | ||
| raise_on_errors: bool = False, | ||
| ) -> Microgrid: | ||
| """ | ||
| Get the details of a microgrid. | ||
|
|
||
| Args: | ||
| microgrid_id: The ID of the microgrid to get the details of. | ||
| raise_on_errors: If True, raise a | ||
| [ParsingError][frequenz.client.assets.exceptions.ParsingError] | ||
| when major issues are found in the response instead of just | ||
| logging them. | ||
|
|
||
| Returns: | ||
| The details of the microgrid. | ||
|
|
@@ -113,16 +120,23 @@ async def get_microgrid( # noqa: DOC502 (raises ApiClientError indirectly) | |
| method_name="GetMicrogrid", | ||
| ) | ||
|
|
||
| return microgrid_from_proto(response.microgrid) | ||
| return microgrid_from_proto(response.microgrid, raise_on_errors=raise_on_errors) | ||
|
|
||
| async def list_microgrid_electrical_components( | ||
| self, microgrid_id: MicrogridId | ||
| self, | ||
| microgrid_id: MicrogridId, | ||
| *, | ||
| raise_on_errors: bool = False, | ||
| ) -> list[ElectricalComponent]: | ||
| """ | ||
| Get the electrical components of a microgrid. | ||
|
|
||
| Args: | ||
| microgrid_id: The ID of the microgrid to get the electrical components of. | ||
| raise_on_errors: If True, raise a | ||
| [ParsingError][frequenz.client.assets.exceptions.ParsingError] | ||
| when major issues are found in any component instead of just | ||
| logging them. | ||
|
|
||
| Returns: | ||
| The electrical components of the microgrid. | ||
|
|
@@ -139,14 +153,17 @@ async def list_microgrid_electrical_components( | |
| ) | ||
|
|
||
| return [ | ||
| electrical_component_proto(component) for component in response.components | ||
| electrical_component_proto(component, raise_on_errors=raise_on_errors) | ||
| for component in response.components | ||
| ] | ||
|
Comment on lines
155
to
158
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same, but we could use a components = []
exceptions = []
for component_pb in response.components:
major_issues = [] # minor_issues...
component = electrical_component_from_proto_with_issues(component_pb, major_issues, ...)
if major_issues:
exceptions.append(
InvalidElectricalComponentError(component, component_pb, major_issues, ...)
)
else:
components.append(component)
if exceptions:
raise InvalidElectricalComponentErrorGroup(valid_components=components, exceptions)
return components |
||
|
|
||
| async def list_microgrid_electrical_component_connections( | ||
| self, | ||
| microgrid_id: MicrogridId, | ||
| source_component_ids: Iterable[ElectricalComponentId] = (), | ||
| destination_component_ids: Iterable[ElectricalComponentId] = (), | ||
| *, | ||
| raise_on_errors: bool = False, | ||
| ) -> list[ComponentConnection | None]: | ||
| """ | ||
| Get the electrical component connections of a microgrid. | ||
|
|
@@ -158,6 +175,10 @@ async def list_microgrid_electrical_component_connections( | |
| these component IDs. If None or empty, no filtering is applied. | ||
| destination_component_ids: Only return connections that terminate at | ||
| these component IDs. If None or empty, no filtering is applied. | ||
| raise_on_errors: If True, raise a | ||
| [ParsingError][frequenz.client.assets.exceptions.ParsingError] | ||
| when major issues are found in any connection instead of just | ||
| logging them. | ||
|
|
||
| Returns: | ||
| The electrical component connections of the microgrid. | ||
|
|
@@ -177,9 +198,7 @@ async def list_microgrid_electrical_component_connections( | |
| method_name="ListMicrogridElectricalComponentConnections", | ||
| ) | ||
|
|
||
| return list( | ||
| map( | ||
| component_connection_from_proto, | ||
| filter(bool, response.connections), | ||
| ) | ||
| ) | ||
| return [ | ||
| component_connection_from_proto(conn, raise_on_errors=raise_on_errors) | ||
| for conn in filter(bool, response.connections) | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,15 +12,30 @@ | |
|
|
||
| from .._lifetime import Lifetime | ||
| from .._lifetime_proto import lifetime_from_proto | ||
| from ..exceptions import ParsingError | ||
| from ._connection import ComponentConnection | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def component_connection_from_proto( | ||
| message: electrical_components_pb2.ElectricalComponentConnection, | ||
| *, | ||
| raise_on_errors: bool = False, | ||
|
Comment on lines
+23
to
+24
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't add this here, I would add this at the client level. |
||
| ) -> ComponentConnection | None: | ||
| """Create a `ComponentConnection` from a protobuf message.""" | ||
| """Create a `ComponentConnection` from a protobuf message. | ||
|
|
||
| Args: | ||
| message: The protobuf message to parse. | ||
| raise_on_errors: If True, raise a ParsingError when major issues | ||
| are found instead of just logging them. | ||
|
|
||
| Returns: | ||
| The parsed ComponentConnection, or None if completely invalid. | ||
|
|
||
| Raises: | ||
| ParsingError: If `raise_on_errors` is True and major issues are found. | ||
| """ | ||
| major_issues: list[str] = [] | ||
| minor_issues: list[str] = [] | ||
|
|
||
|
|
@@ -29,6 +44,12 @@ def component_connection_from_proto( | |
| ) | ||
|
|
||
| if major_issues: | ||
| if raise_on_errors: | ||
| raise ParsingError( | ||
| major_issues=major_issues, | ||
| minor_issues=minor_issues, | ||
| raw_message=message, | ||
| ) | ||
| _logger.warning( | ||
| "Found issues in component connection: %s | Protobuf message:\n%s", | ||
| ", ".join(major_issues), | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do the error handling here instead:
EDIT: Updated example to only call
xxx_with_issues()if `raise_on_errors is true.I would use
InvalidMicrogridErrorrather thanParseErroras we are not really parsing anything here, protobuf messages are already parsed and have no problem at the protobuf-level, is just some validations we are doing that don't pass.In the future we might even change the argument for something like
on_validation_error=LOG|RAISE_ONCE|RAISE_ALL|IGNORE(for now I think we can keep it simple and leave theraise_on_errors).