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

Add error handling when updating non-existing record #306

Conversation

QuantumDancer
Copy link
Contributor

@QuantumDancer QuantumDancer commented Aug 2, 2023

This is my proposed solution to fix #305

I tried to only catch this specific exception. All other exceptions should still be passed along.

Please let me know if we should do things differently.

Also, I was not sure how I can create a unit test for this specific case, because the auditor server is not directly mocked if I understood the test setup correctly.

I tested this in Freiburg in production and found no issues with this fix so far.

@codecov-commenter
Copy link

codecov-commenter commented Aug 2, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (2bf9dcb) 98.84% compared to head (19ca892) 98.85%.

❗ Current head 19ca892 differs from pull request most recent head e513aac. Consider uploading reports for the commit e513aac to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #306   +/-   ##
=======================================
  Coverage   98.84%   98.85%           
=======================================
  Files          56       56           
  Lines        2345     2356   +11     
=======================================
+ Hits         2318     2329   +11     
  Misses         27       27           
Files Changed Coverage Δ
tardis/plugins/auditor.py 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@giffels
Copy link
Member

giffels commented Aug 2, 2023

Thanks a lot for the contribution. Without checking any details yet, could you add a unittest for that use-case as well, please?
You could potentially use cls.mock_auditorclientbuilder_patcher to mock the behaviour. I think you can basically use the side_effect method of the mock self.mock_auditorclientbuilder to throw the exception, when updating the record.

self.client.update.side_effect = RuntimeError(...)

run_async(
            self.plugin.notify,
            state=DownState(),
            resource_attributes=self.test_param,
        )

"plus check the result and add further tests here to test re-raising etc."

self.client.update.side_effect = None

@giffels giffels requested review from a team, giffels and maxfischer2781 and removed request for a team August 2, 2023 14:50
@giffels giffels added bug Something isn't working Improvement Code Improvements labels Aug 2, 2023
@QuantumDancer QuantumDancer force-pushed the add-auditor-update-record-error-handling branch 2 times, most recently from d5f1885 to ed69e07 Compare August 3, 2023 14:35
@QuantumDancer
Copy link
Contributor Author

Thanks for the instructions regarding the unit testing. I've now added tests that should cover all three cases. Please let me know, I should change something else.

giffels
giffels previously approved these changes Aug 4, 2023
Copy link
Member

@giffels giffels left a comment

Choose a reason for hiding this comment

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

LGTM! I have added a unittest for the case of a non-matching RegEx.

reqwest_error_match = REQWEST_ERROR.match(str(e))
if reqwest_error_match:
http_error_code = reqwest_error_match.group(1)
if http_error_code == "400":
Copy link
Member

Choose a reason for hiding this comment

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

This not related o the plugin code, but to Auditor instead. Should the returned HTTP error not be 404 -Not Found instead, when trying to update a non-existent resource?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I think you are right. This might need a little bit of time to get fixed in Auditor due to the holiday season. I've opened a new issue ALU-Schumacher/AUDITOR#378. I plan to include this change in the v0.2.0 release of Auditor. Once released, we will create a PR for tardis that includes the version update and updates to all breaking changes.

Copy link
Member

@maxfischer2781 maxfischer2781 left a comment

Choose a reason for hiding this comment

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

I've added some suggestions for smoothing the error detection. Since I'm guessing most people looking at the code won't actually know the exact error message, it would be good to have that as straightforward as possible.
FWIW, I don't know the error message myself so if you have an even better approach, go for it.

Comment on lines 95 to 98
reqwest_error_match = REQWEST_ERROR.match(str(e))
if reqwest_error_match:
http_error_code = reqwest_error_match.group(1)
if http_error_code == "400":
Copy link
Member

Choose a reason for hiding this comment

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

It looks like these double-if could be folded into one, making it more obvious that you are really just checking one thing and unconditionally raise otherwise.

Either...

Copy link
Member

Choose a reason for hiding this comment

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

match and check the code:

Suggested change
reqwest_error_match = REQWEST_ERROR.match(str(e))
if reqwest_error_match:
http_error_code = reqwest_error_match.group(1)
if http_error_code == "400":
error_match = REQWEST_ERROR.match(str(e))
if error_match and error_match.group(1) == "400:
...

(this could also do with an assignment expression, but is rather dense then)
or ...

Copy link
Member

Choose a reason for hiding this comment

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

make a literal match against the error, since effectively this is what you are doing:

Suggested change
reqwest_error_match = REQWEST_ERROR.match(str(e))
if reqwest_error_match:
http_error_code = reqwest_error_match.group(1)
if http_error_code == "400":
if str(e).startswith("Reqwest Error: HTTP status client error (400 Bad Request)"):

In this case I would recommend to create a new constant for the error message instead of the pattern. Note that I've guessed the necessity for .startswith from the pattern's trailing .*, it may be unnecessary or not.

Copy link
Member

Choose a reason for hiding this comment

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

@maxfischer2781 thanks and 👍 for your remarks. Just a reminder that assignment expressions would not work until #304 is approved and merged.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion. I agree that maybe the error message matching was slightly overengineered for this case 😉 I've replaced the regex logic with a simple str(e).startswith(...).

@giffels giffels mentioned this pull request Aug 8, 2023
10 tasks
QuantumDancer and others added 2 commits August 8, 2023 10:38
It can happen that a drone does not reach the AvailableState but only
the DownState. In this case, updating the record in Auditor does not
work, because no record has been created. In this case, Auditor returns
a 400 BAD REQUEST. This commit adds error handling for this specific
case. Other exceptions will not be handled.
Copy link
Member

@giffels giffels left a comment

Choose a reason for hiding this comment

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

LGTM!

giffels added a commit to giffels/tardis that referenced this pull request Aug 8, 2023
Copy link
Member

@maxfischer2781 maxfischer2781 left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution and final touch-ups. Good to go.

@giffels giffels added this pull request to the merge queue Aug 9, 2023
Merged via the queue into MatterMiners:master with commit 48daacb Aug 9, 2023
19 checks passed
giffels added a commit to giffels/tardis that referenced this pull request Aug 10, 2023
giffels added a commit to giffels/tardis that referenced this pull request Aug 15, 2023
giffels added a commit to giffels/tardis that referenced this pull request Oct 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Improvement Code Improvements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Auditor plugin crashes when drone does not reach AvailableState
4 participants