feat: add specific error code for invalid publish names
This commit is contained in:
parent
d9fbb20869
commit
a6af0300ee
|
|
@ -161,6 +161,15 @@ pub enum AppError {
|
||||||
workspace_id: Uuid,
|
workspace_id: Uuid,
|
||||||
publish_name: String,
|
publish_name: String,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
#[error("There is an invalid character in the publish name: {character}")]
|
||||||
|
PublishNameInvalidCharacter { character: char },
|
||||||
|
|
||||||
|
#[error("The publish name is too long, given length: {given_length}, max length: {max_length}")]
|
||||||
|
PublishNameTooLong {
|
||||||
|
given_length: usize,
|
||||||
|
max_length: usize,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppError {
|
impl AppError {
|
||||||
|
|
@ -232,6 +241,8 @@ impl AppError {
|
||||||
AppError::AccessRequestAlreadyExists { .. } => ErrorCode::AccessRequestAlreadyExists,
|
AppError::AccessRequestAlreadyExists { .. } => ErrorCode::AccessRequestAlreadyExists,
|
||||||
AppError::TooManyImportTask(_) => ErrorCode::TooManyImportTask,
|
AppError::TooManyImportTask(_) => ErrorCode::TooManyImportTask,
|
||||||
AppError::PublishNameAlreadyExists { .. } => ErrorCode::PublishNameAlreadyExists,
|
AppError::PublishNameAlreadyExists { .. } => ErrorCode::PublishNameAlreadyExists,
|
||||||
|
AppError::PublishNameInvalidCharacter { .. } => ErrorCode::PublishNameInvalidCharacter,
|
||||||
|
AppError::PublishNameTooLong { .. } => ErrorCode::PublishNameTooLong,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -368,6 +379,8 @@ pub enum ErrorCode {
|
||||||
CustomNamespaceTooLong = 1048,
|
CustomNamespaceTooLong = 1048,
|
||||||
CustomNamespaceReserved = 1049,
|
CustomNamespaceReserved = 1049,
|
||||||
PublishNameAlreadyExists = 1050,
|
PublishNameAlreadyExists = 1050,
|
||||||
|
PublishNameInvalidCharacter = 1051,
|
||||||
|
PublishNameTooLong = 1052,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ErrorCode {
|
impl ErrorCode {
|
||||||
|
|
|
||||||
|
|
@ -61,19 +61,20 @@ async fn check_workspace_owner_or_publisher(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_collab_publish_name(publish_name: &str) -> Result<(), AppError> {
|
fn check_collab_publish_name(publish_name: &str) -> Result<(), AppError> {
|
||||||
|
const MAX_PUBLISH_NAME_LENGTH: usize = 128;
|
||||||
|
|
||||||
// Check len
|
// Check len
|
||||||
if publish_name.len() > 128 {
|
if publish_name.len() > MAX_PUBLISH_NAME_LENGTH {
|
||||||
return Err(AppError::InvalidRequest(
|
return Err(AppError::PublishNameTooLong {
|
||||||
"Publish name must be at most 128 characters long".to_string(),
|
given_length: publish_name.len(),
|
||||||
));
|
max_length: MAX_PUBLISH_NAME_LENGTH,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only contain alphanumeric characters and hyphens
|
// Only contain alphanumeric characters and hyphens
|
||||||
for c in publish_name.chars() {
|
for c in publish_name.chars() {
|
||||||
if !c.is_alphanumeric() && c != '-' {
|
if !c.is_alphanumeric() && c != '-' {
|
||||||
return Err(AppError::InvalidRequest(
|
return Err(AppError::PublishNameInvalidCharacter { character: c });
|
||||||
"Publish name must only contain alphanumeric characters and hyphens".to_string(),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -90,6 +90,50 @@ async fn test_publish_doc() {
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
{
|
||||||
|
// Invalid publish name
|
||||||
|
let err = c
|
||||||
|
.publish_collabs::<MyCustomMetadata, &[u8]>(
|
||||||
|
&workspace_id,
|
||||||
|
vec![PublishCollabItem {
|
||||||
|
meta: PublishCollabMetadata {
|
||||||
|
view_id: uuid::Uuid::new_v4(),
|
||||||
|
publish_name: "(*&^%$#!".to_string(), // invalid chars
|
||||||
|
metadata: MyCustomMetadata {
|
||||||
|
title: "my_title_1".to_string(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
data: "yrs_encoded_data_1".as_bytes(),
|
||||||
|
}],
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap_err();
|
||||||
|
assert_eq!(
|
||||||
|
err.code,
|
||||||
|
ErrorCode::PublishNameInvalidCharacter,
|
||||||
|
"{:?}",
|
||||||
|
err
|
||||||
|
);
|
||||||
|
// Publish name too long
|
||||||
|
let err = c
|
||||||
|
.publish_collabs::<MyCustomMetadata, &[u8]>(
|
||||||
|
&workspace_id,
|
||||||
|
vec![PublishCollabItem {
|
||||||
|
meta: PublishCollabMetadata {
|
||||||
|
view_id: uuid::Uuid::new_v4(),
|
||||||
|
publish_name: "a".repeat(1001), // too long
|
||||||
|
metadata: MyCustomMetadata {
|
||||||
|
title: "my_title_1".to_string(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
data: "yrs_encoded_data_1".as_bytes(),
|
||||||
|
}],
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap_err();
|
||||||
|
assert_eq!(err.code, ErrorCode::PublishNameTooLong, "{:?}", err);
|
||||||
|
}
|
||||||
|
|
||||||
let publish_name_1 = "publish-name-1";
|
let publish_name_1 = "publish-name-1";
|
||||||
let view_id_1 = uuid::Uuid::new_v4();
|
let view_id_1 = uuid::Uuid::new_v4();
|
||||||
let publish_name_2 = "publish-name-2";
|
let publish_name_2 = "publish-name-2";
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue