From 034d1373d70f9c2bee20fbaffe3786b4f87273ae Mon Sep 17 00:00:00 2001 From: Adhoniran Gomes Date: Sun, 15 Oct 2023 12:14:19 -0300 Subject: [PATCH] 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. --- netbox/extras/dashboard/widgets.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/netbox/extras/dashboard/widgets.py b/netbox/extras/dashboard/widgets.py index e762e5857..d1d2b83f2 100644 --- a/netbox/extras/dashboard/widgets.py +++ b/netbox/extras/dashboard/widgets.py @@ -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, }