Skip to content

Commit 6d81aaa

Browse files
authored
Improve domain handler to catch duplicate domain error (baserow#4400)
* Add error handling when handler tries to create a duplicate domain * Add changelog * Simplify error handling. * Rephrase changelog message
1 parent a2f9ee7 commit 6d81aaa

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

backend/src/baserow/contrib/builder/domains/handler.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from baserow.core.db import specific_iterator
1818
from baserow.core.exceptions import IdDoesNotExist
1919
from baserow.core.models import Workspace
20+
from baserow.core.psycopg import is_unique_violation_error
2021
from baserow.core.registries import ImportExportConfig, application_type_registry
2122
from baserow.core.storage import get_default_storage
2223
from baserow.core.trash.handler import TrashHandler
@@ -131,7 +132,13 @@ def create_domain(
131132
prepared_values["domain_name"] = prepared_values["domain_name"].lower()
132133

133134
domain = model_class(builder=builder, order=last_order, **prepared_values)
134-
domain.save()
135+
136+
try:
137+
domain.save()
138+
except IntegrityError as error:
139+
if is_unique_violation_error(error):
140+
raise DomainNameNotUniqueError(prepared_values["domain_name"])
141+
raise error
135142

136143
return domain
137144

@@ -171,7 +178,7 @@ def update_domain(self, domain: Domain, **kwargs) -> Domain:
171178
try:
172179
domain.save()
173180
except IntegrityError as error:
174-
if "unique" in str(error) and "domain_name" in prepared_values:
181+
if is_unique_violation_error(error):
175182
raise DomainNameNotUniqueError(prepared_values["domain_name"])
176183
raise error
177184

backend/tests/baserow/contrib/builder/domains/test_domain_handler.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from baserow.contrib.builder.domains.domain_types import CustomDomainType
44
from baserow.contrib.builder.domains.exceptions import (
55
DomainDoesNotExist,
6+
DomainNameNotUniqueError,
67
DomainNotInBuilder,
78
)
89
from baserow.contrib.builder.domains.handler import DomainHandler
@@ -68,6 +69,21 @@ def test_create_domain(data_fixture):
6869
assert domain.domain_name == "test.com"
6970

7071

72+
@pytest.mark.django_db
73+
def test_create_domain_with_duplicate_name(data_fixture):
74+
builder = data_fixture.create_builder_application()
75+
domain_name = "test.com"
76+
77+
DomainHandler().create_domain(CustomDomainType(), builder, domain_name=domain_name)
78+
79+
with pytest.raises(DomainNameNotUniqueError) as exc_info:
80+
DomainHandler().create_domain(
81+
CustomDomainType(), builder, domain_name=domain_name
82+
)
83+
84+
assert exc_info.value.domain_name == domain_name
85+
86+
7187
@pytest.mark.django_db
7288
def test_delete_domain(data_fixture):
7389
domain = data_fixture.create_builder_custom_domain()
@@ -88,6 +104,24 @@ def test_update_domain(data_fixture):
88104
assert domain.domain_name == "new.com"
89105

90106

107+
@pytest.mark.django_db
108+
def test_update_domain_with_duplicate_name(data_fixture):
109+
builder = data_fixture.create_builder_application()
110+
domain = data_fixture.create_builder_custom_domain(
111+
domain_name="test.com", builder=builder
112+
)
113+
114+
existing_domain = "other.com"
115+
DomainHandler().create_domain(
116+
CustomDomainType(), builder, domain_name=existing_domain
117+
)
118+
119+
with pytest.raises(DomainNameNotUniqueError) as exc_info:
120+
DomainHandler().update_domain(domain, domain_name=existing_domain)
121+
122+
assert exc_info.value.domain_name == existing_domain
123+
124+
91125
@pytest.mark.django_db
92126
def test_order_domains(data_fixture):
93127
builder = data_fixture.create_builder_application()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"type": "bug",
3+
"message": "Fixed a bug where domain names were not correctly validated during domain creation.",
4+
"issue_origin": "github",
5+
"issue_number": 4399,
6+
"domain": "builder",
7+
"bullet_points": [],
8+
"created_at": "2025-12-05"
9+
}

0 commit comments

Comments
 (0)