kaisero / fireREST

Python library for interacting with Cisco Firepower Management Center REST API
GNU General Public License v3.0
70 stars 49 forks source link

Crash if applicabledevices return 404 #49

Closed gsin0080 closed 3 years ago

gsin0080 commented 3 years ago

requests.exceptions.HTTPError: 404 Client Error: 404 for url: https://fmc.lab.local/api/fmc_platform/v1/updates/upgradepackages/82db7c60-850c-11eb-995a-bdf00d291159/applicabledevices?limit=1000&expanded=True

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "fmc_patching.py", line 69, in response = fmc.conn.get(applicabledevicesURL) File "\Python39\site-packages\fireREST\fmc__init.py", line 159, in get response = self._request('get', url, params=params) File "", line 2, in wrapper File "\Python39\site-packages\retry\api.py", line 73, in retry_decorator return retry_internal(partial(f, *args, **kwargs), exceptions, tries, delay, max_delay, backoff, jitter, File "\Python39\site-packages\retry\api.py", line 33, in __retry_internal return f() File "\Python39\site-packages\fireREST\utils.py", line 197, in wrapper raise_for_status(response) File "\Python39\site-packages\fireREST\utils.py", line 245, in raise_for_status raise exceptions.get(status_code, exc.GenericApiError)( fireREST.exceptions.ResourceNotFoundError: No resource found

kaisero commented 3 years ago

The ResourceNotFoundError is to be expected when a GET request returns a 404, this is not an error with FireREST but by design

gsin0080 commented 3 years ago

Yes, it will return 404 when no applicable device (annoying). But the error I receive seems to be about another exception during the raise an exception process. Is it by default? I thought the code will keep going even the response is 404.

gsin0080 commented 3 years ago

I wrote something like this...

response = fmc.update.upgradepackage.applicabledevice.get(container_name=pkgName)

It doesn't complain about 404, but the process inside the requests.exceptions.HTTPError: 404 Client Error: 404 for url: During handling of the above exception, another exception occurred: ... \Python39\site-packages\fireREST\utils.py", line 245, in raise_for_status raise exceptions.get(status_code, exc.GenericApiError)(

kaisero commented 3 years ago

An exception will be thrown if a resource could not be found, the ResourceNotFound exception occurs when a 404 HTTPError is raised, which is why you see both exception in your stacktrace. If you do not care about 404 errors just write a try/except block

from fireREST.exceptions import ResourceNotFoundError
try:
    response = fmc.update.upgradepackage.applicabledevice.get(container_name=pkgName)
except ResourceNotFoundError:
    pass