Skip to content
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

fix(api): properly handle non-labware jsons in sim #17198

Merged

Conversation

sfoster1
Copy link
Member

@sfoster1 sfoster1 commented Jan 7, 2025

opentrons_simulate tries to see if any json file it can reach is a labware and load it if so. We recently changed the exceptions we raise in verify_labware_definitions if that call fails, so now instead of ignoring the file on exception opentrons_simulate raises the error through and terminates. No good!

Change the exception to something a little saner and make sure we can actually ignore invalid labware files in sim.

Closes RQA-3820

opentrons_simulate tries to see if any json file it can reach is a
labware and load it if so. We recently changed the exceptions we raise
in verify_labware_definitions if that call fails, so now instead of
ignoring the file on exception opentrons_simulate raises the error
through and terminates. No good!

Change the exception to something a little saner and make sure we can
actually ignore invalid labware files in sim.

Closes RQA-3820
@sfoster1 sfoster1 requested a review from a team as a code owner January 7, 2025 15:41
Copy link
Contributor

@SyntaxColoring SyntaxColoring left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack! Thank you.

Only nitpicks:

try:
to_return = json.loads(contents)
except json.JSONDecodeError as e:
raise NotALabwareError("invalid-json", [e])
Copy link
Contributor

@SyntaxColoring SyntaxColoring Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: from e so the tracebacks reflect that we're deliberately converting exception A to exception B, instead of encountering an unexpected exception B while we're trying to handle exception A.

Suggested change
raise NotALabwareError("invalid-json", [e])
raise NotALabwareError("invalid-json", [e]) from e

from None instead of from e would also make sense here. Just a matter of taste I think.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh whoops yeah good call

try:
schema_version = to_return["schemaVersion"]
except KeyError as e:
raise NotALabwareError("no-schema-id", [e])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise NotALabwareError("no-schema-id", [e])
raise NotALabwareError("no-schema-id", [e]) from e

)
jsonschema.validate(to_return, schema)
except KeyError as e:
raise NotALabwareError("bad-schema-id", [e])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise NotALabwareError("bad-schema-id", [e])
raise NotALabwareError("bad-schema-id", [e]) from e

Comment on lines -1102 to +1103
wrapping: Optional[Sequence[EnumeratedError]] = None,
wrapping: Optional[Sequence[Union[EnumeratedError, BaseException]]] = None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for this PR, but I think we're losing the plot on wrapping. If wrapping includes BaseException then I think it is strictly a reinvention of the standard __cause__ and __context__ mechanisms, and we should transition to using those instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it has always included those actually, we've just been uneven about propagating the type signature down to things that inherit from the general exception. and while it does do basically the same thing as cause and context, it also represents a specific point at which we leave native exception land and transition into ours.

try:
jsonschema.validate(to_return, schema)
except jsonschema.ValidationError as e:
raise NotALabwareError("schema-mismatch", [e])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise NotALabwareError("schema-mismatch", [e])
raise NotALabwareError("schema-mismatch", [e]) from e

Comment on lines -82 to +79
except (ValidationError, JSONDecodeError):
except labware.NotALabwareError:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better, thank you.

@sfoster1 sfoster1 merged commit 8c2474d into chore_release-8.3.0 Jan 7, 2025
49 checks passed
@sfoster1 sfoster1 deleted the rqa-3820-fix-simulate-ignoring-bad-labware branch January 7, 2025 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants