pyTenable
pyTenable copied to clipboard
Unable to catch exception for scan export
Unable to catch exception for 404 when exporting scan
I have a for loop that is grabbing historical scan IDs and exporting. For one of my scans, a 404 is being returned by tenable.io API when trying to export. The scans.export
function dies when encountering this and I am unable to catch the exception.
To Reproduce I am not sure how i have a historical scan with a 404 so i can't provide clear steps on reproducing.
Some ways i've tried:
with open('example.nessus', 'wb') as reportobj:
try:
tio.scans.export(1, history_id=1, fobj=reportobj)
except:
print("some error message")
try:
with open('example.nessus', 'wb') as reportobj:
tio.scans.export(1, history_id=1, fobj=reportobj)
except:
print("some error message")
try:
reportobj = open('example.nessus', 'wb')
tio.scans.export(1, history_id=1, fobj=reportobj)
except:
print("some error message")
finally:
reportobj.close()
Expected behavior
I would expect pytenable to raise it's own exception that I can catch, but instead the script dies within restfly/sessions.py
with raise error_resp
and restfly.errors.NotFoundError
. I've written this several different ways and never does the exception get caught.
System Information
- OS: [MacOS]
- Architecture [64bit]
- Version pytenable version 1.4.6
- Python - Tested with 3.9 and 3.10
Additional context This very well could be my lack of knowledge, feel free to close if this is expected behavior. Although i'd appreciate some tips/documentation on how to catch this error so my script can proceed with the remaining scans.
As pytenable is relying on RESTfly for the low-level connection handling, this is correct. If you want to catch the 404 error you should be able to do the following:
from restfly.errors import NotFoundError
...
try:
with open('example.nessus', 'wb') as reportobj:
tio.scans.export(1, history_id=1, fobj=reportobj)
except NotFoundError as err:
print(f'I have god an error {err}')
Closing (as designed).