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

Raise and handle exceptions in SNMP module #76

Merged
merged 21 commits into from
Oct 6, 2023
Merged

Conversation

stveit
Copy link
Contributor

@stveit stveit commented Oct 4, 2023

Fixes #73

This PR makes snmp functions raise exceptions if something goes wrong.
A hierarchy of exceptions for snmp related errors is added for this purpose.

This will cause tasks to fail unless its natural for the task to handle certain exceptions.
For example, it will be natural for ReachableTask to handle TimeoutError, since detecting
a device not answering is the purpose of the task. VendorTask on the other hand can not function if
a timeout occurs, so it will be natural for this task to let the exception raise further up the chain.

Currently this will cause zino to crash, so a new issue will be needed for handling tasks failing.

@github-actions
Copy link

github-actions bot commented Oct 4, 2023

Test results

    3 files      3 suites   49s ⏱️
  98 tests   98 ✔️ 0 💤 0
294 runs  294 ✔️ 0 💤 0

Results for commit 82ce61b.

♻️ This comment has been updated with latest results.

@codecov
Copy link

codecov bot commented Oct 4, 2023

Codecov Report

Merging #76 (82ce61b) into master (0ae693c) will increase coverage by 0.23%.
The diff coverage is 95.00%.

@@            Coverage Diff             @@
##           master      #76      +/-   ##
==========================================
+ Coverage   94.06%   94.28%   +0.23%     
==========================================
  Files          22       22              
  Lines        1144     1137       -7     
==========================================
- Hits         1076     1072       -4     
+ Misses         68       65       -3     
Files Coverage Δ
src/zino/tasks/reachabletask.py 98.39% <100.00%> (+0.08%) ⬆️
src/zino/tasks/vendor.py 100.00% <100.00%> (ø)
src/zino/snmp.py 96.35% <94.23%> (+1.91%) ⬆️

... and 1 file with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@stveit stveit marked this pull request as ready for review October 4, 2023 13:58
Copy link
Member

@lunkwill42 lunkwill42 left a comment

Choose a reason for hiding this comment

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

This looks like a very nice improvement on the SNMP adapter code!

On some level, I kind of like the way you've grouped the various Exception-related tests in classes, but I also find it makes it somewhat hard to reason about which tests that exercise a function I'm looking at. Maybe not a big problem with modern IDEs (also, since I've taken to look at mutmut, I've had to switch on dynamic contexts for coverage measurements, so that mutmut can trace which tests cover which code lines)

src/zino/snmp.py Outdated Show resolved Hide resolved
src/zino/snmp.py Outdated Show resolved Hide resolved
src/zino/snmp.py Outdated Show resolved Hide resolved
src/zino/snmp.py Outdated Show resolved Hide resolved
src/zino/snmp.py Outdated Show resolved Hide resolved
Comment on lines +24 to +26
try:
await self._get_sysuptime()
except TimeoutError:
Copy link
Member

Choose a reason for hiding this comment

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

A note for future reference (not to be fixed by this PR): Depending on the original Zino implementation, this function should probably record the device boot time to its state. I've added a utility function to DeviceState for this in one of the PRs I'm currently working on, as the original link state checker did this.

for "unreachable_task", a mock raises timeouterror instantly
so setting low timeout value has no purpose.

for "snmp_client", its not supposed to timeout so
setting a low timeout to speed things up does not make sense
As demonstrated in the code, error_indication can
apparantly be a errind.RequestTimedOut object, which is a subclass of
errind.ErrorIndication. So im guessing any type of errind.ErrorIndication
could be used there.

Im leaving string as union, because the docstring for getCmd does state
it is supposed to be a str value, so im leaving it open for now
@sonarcloud
Copy link

sonarcloud bot commented Oct 6, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@stveit
Copy link
Contributor Author

stveit commented Oct 6, 2023

Fixed the stuff mentioned in the review, and also updated typing for _raise_errors. Would be weird for me to check if an input is an instance of something and not allow that something in the typehints.

I have a pretty strong suspicion all the return values from the various SNMP commands (getCmd, bulkCmd) etc, all have misleading docstrings. I am pretty sure error_status is returned as an int and not a str. I also think error_indication is None if there are no errors.

I am leaving any more fixes for this to a separate issue #82

@lunkwill42
Copy link
Member

I have a pretty strong suspicion all the return values from the various SNMP commands (getCmd, bulkCmd) etc, all have misleading docstrings. I am pretty sure error_status is returned as an int and not a str. I also think error_indication is None if there are no errors.

Yeah, the library is confusing enough at certain points. It could do with some proper type annotations, at least. I did read somewhere in those docs just yesterday that error_indication is a string, when it is in fact an Exception instance.

I am leaving any more fixes for this to a separate issue #82

Great :)

Copy link
Member

@lunkwill42 lunkwill42 left a comment

Choose a reason for hiding this comment

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

👍

@stveit stveit merged commit 04c37f0 into master Oct 6, 2023
9 checks passed
@lunkwill42 lunkwill42 deleted the snmp-exception-handling branch October 6, 2023 12:17
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.

zino.snmp.SNMP needs improved error handling and logging
2 participants