-
Notifications
You must be signed in to change notification settings - Fork 20
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
Add error handling when updating non-existing record #306
Conversation
Codecov ReportPatch coverage:
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
☔ View full report in Codecov by Sentry. |
Thanks a lot for the contribution. Without checking any details yet, could you add a unittest for that use-case as well, please? 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 |
d5f1885
to
ed69e07
Compare
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. |
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.
LGTM! I have added a unittest for the case of a non-matching RegEx.
tardis/plugins/auditor.py
Outdated
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": |
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.
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?
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.
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.
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.
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.
tardis/plugins/auditor.py
Outdated
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": |
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.
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...
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.
match and
check the code:
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 ...
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.
make a literal match against the error, since effectively this is what you are doing:
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.
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.
@maxfischer2781 thanks and 👍 for your remarks. Just a reminder that assignment expressions would not work until #304 is approved and merged.
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.
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(...)
.
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.
19ca892
to
e513aac
Compare
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.
LGTM!
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.
Thanks for the contribution and final touch-ups. Good to go.
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.