Repository: DjangoGirls/djangogirls

Issue:
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Sentry Issue: [DJANGO-GIRLS-WEBSITE-6K](https://django-girls.sentry.io/issues/5093987191/?referrer=github_integration)

```
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
  File "requests/models.py", line 971, in json
    return complexjson.loads(self.text, **kwargs)
  File "__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None

JSONDecodeError: Expecting value: line 1 column 1 (char 0)
(10 additional frame(s) were not displayed)
...
  File "organize/views.py", line 52, in done
    application = EventApplication.object.create(**data_dict)
  File "organize/models.py", line 83, in create
    return super().create(**data_dict)
  File "organize/models.py", line 155, in save
    self.latlng = get_coordinates_for_city(self.city, self.get_country_display())
  File "core/utils.py", line 17, in get_coordinates_for_city
    data = req.json()[0]
```


Discussion:
This looks the same as we're seeing in some test failures for the pre-commit and black updates

https://github.com/DjangoGirls/djangogirls/pull/965
Would it be possible to make the Sentry issue public, as it could be helpful? It's tough to determine what caused this issue solely from the stack trace.
@vanessa Here's a public link for that issue;

https://django-girls.sentry.io/share/issue/b4f5fe7b27e34ee8948a3371e8fb203a/
@marksweb do we know what's the city that caused this error?
@vanessa Ah yes, the public pages miss such useful info;

city	"Prague"

country	"Czechia"

q	"Prague, Czechia"

req	<Response [403]>

Resolving patch:
diff --git a/core/utils.py b/core/utils.py
--- a/core/utils.py
+++ b/core/utils.py
@@ -1,4 +1,5 @@
 from datetime import date, datetime, timedelta
+from json import JSONDecodeError
 
 import requests
 from django.utils import timezone
@@ -18,7 +19,7 @@ def get_coordinates_for_city(city, country):
         formatted_lat = "{:.7f}".format(float(data["lat"]))
         formatted_lon = "{:.7f}".format(float(data["lon"]))
         return f"{formatted_lat}, {formatted_lon}"
-    except (IndexError, KeyError):
+    except (IndexError, KeyError, JSONDecodeError):
         return None
 
 


Test patch:
diff --git a/tests/core/test_utils.py b/tests/core/test_utils.py
--- a/tests/core/test_utils.py
+++ b/tests/core/test_utils.py
@@ -1,3 +1,4 @@
+import json
 from datetime import date
 from unittest import mock
 
@@ -63,6 +64,14 @@ def test_returns_none_when_invalid_results(mock_get):
     assert result is None
 
 
+@mock.patch("requests.get")
+def test_get_coordinates_for_city_invalid_response(mock_get):
+    # Raise JSONDecodeError when trying to parse the response
+    mock_get.return_value.json.side_effect = json.JSONDecodeError("error", "doc", 0)
+    result = get_coordinates_for_city("Prague", "Czechia")
+    assert result is None
+
+
 @freeze_time("2016-10-10")
 def test_a_week_before_deadline():
     result = next_deadline()
diff --git a/tests/organize/test_models.py b/tests/organize/test_models.py
--- a/tests/organize/test_models.py
+++ b/tests/organize/test_models.py
@@ -43,7 +43,10 @@ def test_deploy_event_from_previous_event(get_or_create_gmail, base_application,
 
 @mock.patch("organize.models.gmail_accounts.get_or_create_gmail")
 def test_send_deployed_email(get_or_create_gmail, base_application, mailoutbox, stock_pictures):
-    get_or_create_gmail.return_value = (f"{base_application.city}@djangogirls.org", "asd123ASD")
+    get_or_create_gmail.return_value = (
+        f"{base_application.city}@djangogirls.org",
+        "asd123ASD",
+    )
 
     base_application.create_event()
     event = base_application.deploy()
@@ -123,7 +126,10 @@ def test_previous_application_with_approximate_date(data_dict, previous_applicat
 
 def test_coorganizer_str(base_application):
     org = Coorganizer.objects.create(
-        event_application=base_application, email="anna@example.com", first_name="Anna", last_name="Smith"
+        event_application=base_application,
+        email="anna@example.com",
+        first_name="Anna",
+        last_name="Smith",
     )
     assert str(org) == f"{org.first_name} {org.last_name} <{org.email}>"