Skip to content

Commit 9734daa

Browse files
committed
Do not crash hard if a reload's results are indeterminate due to possible DNS config issues. by: Conrad, Giles
1 parent debbb50 commit 9734daa

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

pythonanywhere/api.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,18 @@ def reload(self):
130130
url = get_api_endpoint().format(username=getpass.getuser(), flavor="webapps") + self.domain + "/reload/"
131131
response = call_api(url, "post")
132132
if not response.ok:
133+
if response.status_code == 409 and response.json()["error"] == "cname_error":
134+
print(
135+
snakesay(
136+
dedent("""
137+
Could not find a CNAME for your website. If you're using an A record,
138+
CloudFlare, or some other way of pointing your domain at PythonAnywhere
139+
then that should not be a problem. If you're not, you should double-check
140+
your DNS setup.
141+
""")
142+
)
143+
)
144+
return
133145
raise Exception(
134146
"POST to reload webapp via API failed, got {response}:{response_text}".format(
135147
response=response, response_text=response.text

tests/test_api.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def test_does_post_to_reload_url(self, api_responses, api_token):
236236
assert post.request.body is None
237237
assert post.request.headers["Authorization"] == "Token {api_token}".format(api_token=api_token)
238238

239-
def test_raises_if_post_does_not_20x(self, api_responses, api_token):
239+
def test_raises_if_post_does_not_20x_that_is_not_a_cname_error(self, api_responses, api_token):
240240
expected_url = (
241241
get_api_endpoint().format(username=getpass.getuser(), flavor="webapps") + "mydomain.com/reload/"
242242
)
@@ -248,6 +248,15 @@ def test_raises_if_post_does_not_20x(self, api_responses, api_token):
248248
assert "POST to reload webapp via API failed" in str(e.value)
249249
assert "nope" in str(e.value)
250250

251+
def test_does_not_raise_if_post_responds_with_a_cname_error(self, api_responses, api_token):
252+
expected_url = (
253+
get_api_endpoint().format(username=getpass.getuser(), flavor="webapps") + "mydomain.com/reload/"
254+
)
255+
api_responses.add(responses.POST, expected_url, status=409, json={"status": "error", "error": "cname_error"})
256+
257+
## Should not raise
258+
Webapp("mydomain.com").reload()
259+
251260

252261
class TestSetWebappSSL:
253262
def test_does_post_to_ssl_url(self, api_responses, api_token):

0 commit comments

Comments
 (0)