From 6cd9a1261e50f060839454a1b606690fd382cf83 Mon Sep 17 00:00:00 2001 From: Marc Barry <4965634+marc-barry@users.noreply.github.com> Date: Fri, 22 Nov 2024 12:39:04 -0500 Subject: [PATCH] Fix code scanning alert no. 5: Information exposure through an exception Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> --- apps/python-geoweather/app.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/apps/python-geoweather/app.py b/apps/python-geoweather/app.py index 68ff862..33d1ca3 100644 --- a/apps/python-geoweather/app.py +++ b/apps/python-geoweather/app.py @@ -14,10 +14,10 @@ def get_public_ip(): return response.text.strip() except requests.HTTPError as http_err: logging.error(f'HTTP error occurred: {http_err}') # HTTP error - return f"Unable to get public IP: HTTP error occurred: {http_err}" + return "Unable to get public IP due to an HTTP error." except Exception as err: logging.error(f'Other error occurred: {err}') # Other errors - return f"Unable to get public IP: Other error occurred: {err}" + return "Unable to get public IP due to an unexpected error." def get_location(ip): url = f"https://ipwho.is/{ip}?fields=country,city,latitude,longitude" @@ -27,10 +27,10 @@ def get_location(ip): return response.json() except requests.HTTPError as http_err: logging.error(f'HTTP error occurred while fetching location: {http_err}') - return {'error': f"HTTP error occurred: {http_err}"} + return {'error': "Unable to fetch location due to an HTTP error."} except Exception as err: logging.error(f'Error fetching location: {err}') - return {'error': f"Other error occurred: {err}"} + return {'error': "Unable to fetch location due to an unexpected error."} def get_weather(lat, lon): url = f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}¤t=temperature_2m&forecast_days=1" @@ -40,10 +40,10 @@ def get_weather(lat, lon): return response.json() except requests.HTTPError as http_err: logging.error(f'HTTP error occurred while fetching weather: {http_err}') - return {'error': f"HTTP error occurred: {http_err}"} + return {'error': "Unable to fetch weather due to an HTTP error."} except Exception as err: logging.error(f'Error fetching weather: {err}') - return {'error': f"Other error occurred: {err}"} + return {'error': "Unable to fetch weather due to an unexpected error."} @app.route('/') def index():