Enhanced error handling for HTTP requests

Improved the error handling logic in HTTP request function. Now, the error message returned upon exceptions is more specific, providing information about the type of the error occurred (Timeout, URL Required, Too Many Redirects, etc.). If the error is associated with the response from the server, the reason for the HTTP error is now included in the error message.
This commit is contained in:
Adhoniran Gomes 2023-10-15 12:14:19 -03:00
parent 845c43185c
commit 034d1373d7

View File

@ -318,8 +318,23 @@ class RSSFeedWidget(DashboardWidget):
timeout=3
)
response.raise_for_status()
except requests.exceptions.RequestException:
feed_error = 'NetBox is unable to connect to feed server...'
except requests.exceptions.RequestException as e:
if 'response' in locals():
feed_error = f'HTTP {response.reason}'
else:
if isinstance(e, requests.exceptions.Timeout):
feed_error = 'Timeout'
elif isinstance(e, requests.exceptions.URLRequired):
feed_error = 'URL Required'
elif isinstance(e, requests.exceptions.TooManyRedirects):
feed_error = 'Too Many Redirects'
else:
if 'NameResolutionError' in str(e):
feed_error = 'Failed to Resolve'
else:
feed_error = 'Connection Error'
return {
'error': feed_error,
}