Skip to content

Commit 3d05a16

Browse files
committed
PA-752: Catching the already used domain name exception and outputting something more readable than the stacktrace. by Nina, Sam
1 parent 6da0516 commit 3d05a16

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

cli/website.py

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

99
from pythonanywhere_core.website import Website
10+
from pythonanywhere_core.exceptions import SanityException
1011

1112

1213
app = typer.Typer(no_args_is_help=True)
@@ -32,7 +33,11 @@ def create(
3233
],
3334
):
3435
"""Create an ASGI website"""
35-
Website().create(domain_name=domain_name, command=command)
36+
try:
37+
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
3641

3742
typer.echo(
3843
snakesay(

tests/test_cli_website.py

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

77
from cli.website import app
8+
from pythonanywhere_core.exceptions import SanityException
89

910

1011
runner = CliRunner()
@@ -114,6 +115,27 @@ def test_create_with_domain_and_command_creates_it(mock_website):
114115
assert "All done!" in result.stdout
115116

116117

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+
121+
result = runner.invoke(
122+
app,
123+
[
124+
"create",
125+
"-d",
126+
"www.something.com",
127+
"-c",
128+
"some kind of server",
129+
],
130+
)
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. Use the --nuke option to replace it." in result.stdout
137+
138+
117139
def test_get_with_no_domain_lists_websites(mock_echo, mock_tabulate, mock_website, website_info):
118140
second_website_info = {"domain_name": "blah.com", "enabled": False}
119141
mock_website.return_value.list.return_value = [website_info, second_website_info]

0 commit comments

Comments
 (0)