Skip to content

Commit

Permalink
Added error handling for 401,404 requests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidNicolas-cecs committed Jan 23, 2024
1 parent 06d746e commit 77e586a
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions flaskr/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def index():
except:
flash("Could not find the location, Please try again.")
return redirect(url_for("weather.index"))
if data is not None:
if data != False:
#0 - name, 1 - temp, 2 - feels like, 3 - pressure, 4 - humidity
#if true redirect to weather_displayer function
#submit data into the database
Expand All @@ -78,8 +78,6 @@ def index():

return redirect(url_for('weather.weather_displayer', name=data[0], temp=data[1], feels_like=data[2], pressure=data[3], humidity=data[4]))
else:
error = "The search location could not be found"
flash(error)
return redirect(url_for('weather.index'))
return render_template('weather/index.html')

Expand Down Expand Up @@ -120,6 +118,7 @@ def geocoding(city,state_code,country_code,want_humidity,want_pressure,want_feel
error = ""
data = []
if response.status_code == 200:
print('Request successful')
data_list = response.json() #get data from response
# Access data
first_item = data_list[0]
Expand All @@ -129,8 +128,13 @@ def geocoding(city,state_code,country_code,want_humidity,want_pressure,want_feel
data.append(name)
current_weather(lat,lon,API_key,data,want_humidity,want_pressure,want_feels_like)
else:
error = f'Could not find the location{response.status_code}'
flash(error)
if response.status_code == 401:
print(f'could not make request {response.status_code}')
error = 'Could not fetch location at this time. Please try again later.'
elif response.status_code == 404:
print(f'Specified the wrong city name, ZIP-code or city ID')
error = 'Specified the wrong city name, ZIP-code or city ID'
flash(error)
return False
return data

Expand Down

0 comments on commit 77e586a

Please sign in to comment.