Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Add semantic error for reserved Space slugs

Manav Rathi55b6d2eeb887
diff --git a/rust/bindings/wasm/space/src/space.rs b/rust/bindings/wasm/space/src/space.rs
index e88dc4e6b4..cfb3e5b103 100644
--- a/rust/bindings/wasm/space/src/space.rs
+++ b/rust/bindings/wasm/space/src/space.rs
@@ -27,6 +27,7 @@ impl Error {
Self::Space(ente_space::Error::SpaceSlugAlreadyExists) => {
Some("space_slug_already_exists")
}
+ Self::Space(ente_space::Error::SpaceSlugReserved) => Some("space_slug_reserved"),
Self::Space(ente_space::Error::InvalidSpaceSlug) => Some("invalid_space_slug"),
Self::Space(ente_space::Error::PostLimitReached) => Some("post_limit_reached"),
Self::Space(ente_space::Error::SessionUnauthorized) => Some("session_unauthorized"),
diff --git a/rust/crates/space/src/client/mod.rs b/rust/crates/space/src/client/mod.rs
index 4da545f011..72cef2b47e 100644
--- a/rust/crates/space/src/client/mod.rs
+++ b/rust/crates/space/src/client/mod.rs
@@ -773,6 +773,7 @@ fn map_create_space_error(error: http::Error) -> Error {
fn map_space_slug_error(error: http::Error) -> Error {
match &error {
http::Error::Api { code, .. } if code == "ALREADY_EXISTS" => Error::SpaceSlugAlreadyExists,
+ http::Error::Api { code, .. } if code == "SPACE_SLUG_RESERVED" => Error::SpaceSlugReserved,
http::Error::Api { code, .. } if code == "BAD_REQUEST" => Error::InvalidSpaceSlug,
_ => map_account_error(error),
}
diff --git a/rust/crates/space/src/client/tests.rs b/rust/crates/space/src/client/tests.rs
index 27a699a419..bde5b67263 100644
--- a/rust/crates/space/src/client/tests.rs
+++ b/rust/crates/space/src/client/tests.rs
@@ -679,6 +679,26 @@ async fn update_space_slug_maps_duplicate_error() {
update_slug.assert_async().await;
}
+#[tokio::test]
+async fn update_space_slug_maps_reserved_error() {
+ let mut server = Server::new_async().await;
+ let ctx = test_account_ctx(&server.url());
+ let update_slug = server
+ .mock("PUT", "/spaces/space_owner_main/slug")
+ .with_status(400)
+ .with_body(json!({ "code": "SPACE_SLUG_RESERVED" }).to_string())
+ .create_async()
+ .await;
+
+ let error = ctx
+ .update_space_slug("space_owner_main", "support")
+ .await
+ .expect_err("slug update should fail");
+
+ assert!(matches!(error, Error::SpaceSlugReserved));
+ update_slug.assert_async().await;
+}
+
#[tokio::test]
async fn create_space_updates_loaded_owned_space_cache() {
let mut server = Server::new_async().await;
diff --git a/rust/crates/space/src/error.rs b/rust/crates/space/src/error.rs
index 8b19ff39f9..a2ccc75acc 100644
--- a/rust/crates/space/src/error.rs
+++ b/rust/crates/space/src/error.rs
@@ -30,6 +30,9 @@ pub enum Error {
#[error("space slug already exists")]
SpaceSlugAlreadyExists,
+ #[error("space slug is reserved")]
+ SpaceSlugReserved,
+
#[error("invalid space slug")]
InvalidSpaceSlug,
diff --git a/server/space/repo/slug_validation.go b/server/space/repo/slug_validation.go
index 61e5299be2..cbd90b06fe 100644
--- a/server/space/repo/slug_validation.go
+++ b/server/space/repo/slug_validation.go
@@ -8,8 +8,9 @@ import (
)
const (
- minSpaceSlugLength = 4
- maxSpaceSlugLength = 30
+ minSpaceSlugLength = 4
+ maxSpaceSlugLength = 30
+ spaceSlugReservedErrorCode = ente.ErrorCode("SPACE_SLUG_RESERVED")
)
var spaceSlugPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9._]*$`)
@@ -52,7 +53,10 @@ func validateSpaceSlug(input string, allowReserved bool) (string, error) {
return "", ente.NewBadRequestWithMessage("spaceSlug must be 4-30 characters")
}
if !allowReserved && isReservedSpaceSlug(slug) {
- return "", ente.NewBadRequestWithMessage("spaceSlug is reserved")
+ return "", ente.NewBadRequestError(&ente.ApiErrorParams{
+ Code: spaceSlugReservedErrorCode,
+ Message: "spaceSlug is reserved",
+ })
}
if !spaceSlugPattern.MatchString(slug) {
return "", ente.NewBadRequestWithMessage("spaceSlug can only contain lowercase letters, numbers, dots, or underscores, and must start with a letter or number")
diff --git a/server/space/repo/slug_validation_test.go b/server/space/repo/slug_validation_test.go
index d0170e6b52..7aca67557a 100644
--- a/server/space/repo/slug_validation_test.go
+++ b/server/space/repo/slug_validation_test.go
@@ -4,6 +4,7 @@ import (
"strings"
"testing"
+ "github.com/ente/museum/ente"
"github.com/stretchr/testify/require"
)
@@ -63,5 +64,6 @@ func TestValidateSpaceSlugAllowReservedAcceptsReservedSlug(t *testing.T) {
func TestValidateSpaceSlugRejectsReservedFileSuffix(t *testing.T) {
_, err := ValidateSpaceSlug("theme.css")
require.Error(t, err)
+ require.Equal(t, spaceSlugReservedErrorCode, err.(*ente.ApiError).Code)
require.Contains(t, err.Error(), "spaceSlug is reserved")
}
diff --git a/web/apps/space/src/services/spaceProfile.ts b/web/apps/space/src/services/spaceProfile.ts
index b8552710b3..71bb26a67c 100644
--- a/web/apps/space/src/services/spaceProfile.ts
+++ b/web/apps/space/src/services/spaceProfile.ts
@@ -617,6 +617,8 @@ export const spaceProfileErrorMessage = (error: unknown) => {
}
if (error.name == "space_slug_already_exists")
return "This username is already taken.";
+ if (error.name == "space_slug_reserved")
+ return "This username is reserved.";
if (error.name == "space_limit_reached") {
return "A Space already exists for this account. Please refresh and try again.";
}