Skip to content

Commit 9a9b3b4

Browse files
author
Nina Kahr
committed
PA-752: Handle errors in website creation. by Filip, Nina
1 parent 7593ba6 commit 9a9b3b4

File tree

5 files changed

+39
-15
lines changed

5 files changed

+39
-15
lines changed

cli/website.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from tabulate import tabulate
88

99
from pythonanywhere_core.website import Website
10-
from pythonanywhere_core.exceptions import SanityException
10+
from pythonanywhere_core.exceptions import PythonAnywhereApiException, DomainAlreadyExistsException
1111

1212

1313
app = typer.Typer(no_args_is_help=True)
@@ -35,9 +35,12 @@ def create(
3535
"""Create an ASGI website"""
3636
try:
3737
Website().create(domain_name=domain_name, command=command)
38-
except SanityException as e:
39-
typer.echo(f"You already have a website for {domain_name}. Use the --nuke option to replace it.")
40-
return
38+
except DomainAlreadyExistsException:
39+
typer.echo(f"You already have a website for {domain_name}.")
40+
raise typer.Exit(code=1)
41+
except PythonAnywhereApiException as e:
42+
typer.echo(str(e))
43+
raise typer.Exit(code=1)
4144

4245
typer.echo(
4346
snakesay(

pythonanywhere/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.15.3"
1+
__version__ = "0.15.4"

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pytest==8.3.3
88
pytest-cov==5.0.0
99
pytest-mock==3.14.0
1010
pytest-mypy==0.10.3
11-
pythonanywhere_core==0.2.2
11+
pythonanywhere_core==0.2.3
1212
requests==2.32.3
1313
responses==0.25.3
1414
schema==0.7.2

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setup(
1111
name="pythonanywhere",
12-
version="0.15.3",
12+
version="0.15.4",
1313
description="PythonAnywhere helper tools for users",
1414
long_description=long_description,
1515
long_description_content_type="text/markdown",
@@ -36,7 +36,7 @@
3636
"docopt",
3737
"packaging",
3838
"python-dateutil",
39-
"pythonanywhere_core==0.2.0",
39+
"pythonanywhere_core==0.2.3",
4040
"requests",
4141
"schema",
4242
"snakesay",

tests/test_cli_website.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typer.testing import CliRunner
66

77
from cli.website import app
8-
from pythonanywhere_core.exceptions import SanityException
8+
from pythonanywhere_core.exceptions import PythonAnywhereApiException, DomainAlreadyExistsException
99

1010

1111
runner = CliRunner()
@@ -115,25 +115,46 @@ def test_create_with_domain_and_command_creates_it(mock_website):
115115
assert "All done!" in result.stdout
116116

117117

118-
def test_create_with_failing_sanity_check(mock_website):
119-
mock_website.return_value.create.side_effect = SanityException("You already have a website for this domain. Use the --nuke option to replace it.")
120-
118+
def test_create_with_existing_domain(mock_website):
119+
mock_website.return_value.create.side_effect = DomainAlreadyExistsException
120+
domain_name = "www.something.com"
121121
result = runner.invoke(
122122
app,
123123
[
124124
"create",
125125
"-d",
126-
"www.something.com",
126+
domain_name,
127127
"-c",
128128
"some kind of server",
129129
],
130130
)
131-
assert result.exit_code == 0
131+
assert result.exit_code != 0
132+
mock_website.return_value.create.assert_called_once_with(
133+
domain_name="www.something.com",
134+
command="some kind of server"
135+
)
136+
assert "You already have a website for www.something.com." in result.stdout
137+
138+
139+
def test_create_with_existing_domain(mock_website):
140+
mock_website.return_value.create.side_effect = PythonAnywhereApiException("Something terrible has happened.")
141+
domain_name = "www.something.com"
142+
result = runner.invoke(
143+
app,
144+
[
145+
"create",
146+
"-d",
147+
domain_name,
148+
"-c",
149+
"some kind of server",
150+
],
151+
)
152+
assert result.exit_code != 0
132153
mock_website.return_value.create.assert_called_once_with(
133154
domain_name="www.something.com",
134155
command="some kind of server"
135156
)
136-
assert "You already have a website for www.something.com. Use the --nuke option to replace it." in result.stdout
157+
assert "Something terrible has happened." in result.stdout
137158

138159

139160
def test_get_with_no_domain_lists_websites(mock_echo, mock_tabulate, mock_website, website_info):

0 commit comments

Comments
 (0)