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,
|
||||
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 {
|
||||
|
|
@ -232,6 +241,8 @@ impl AppError {
|
|||
AppError::AccessRequestAlreadyExists { .. } => ErrorCode::AccessRequestAlreadyExists,
|
||||
AppError::TooManyImportTask(_) => ErrorCode::TooManyImportTask,
|
||||
AppError::PublishNameAlreadyExists { .. } => ErrorCode::PublishNameAlreadyExists,
|
||||
AppError::PublishNameInvalidCharacter { .. } => ErrorCode::PublishNameInvalidCharacter,
|
||||
AppError::PublishNameTooLong { .. } => ErrorCode::PublishNameTooLong,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -368,6 +379,8 @@ pub enum ErrorCode {
|
|||
CustomNamespaceTooLong = 1048,
|
||||
CustomNamespaceReserved = 1049,
|
||||
PublishNameAlreadyExists = 1050,
|
||||
PublishNameInvalidCharacter = 1051,
|
||||
PublishNameTooLong = 1052,
|
||||
}
|
||||
|
||||
impl ErrorCode {
|
||||
|
|
|
|||
|
|
@ -61,19 +61,20 @@ async fn check_workspace_owner_or_publisher(
|
|||
}
|
||||
|
||||
fn check_collab_publish_name(publish_name: &str) -> Result<(), AppError> {
|
||||
const MAX_PUBLISH_NAME_LENGTH: usize = 128;
|
||||
|
||||
// Check len
|
||||
if publish_name.len() > 128 {
|
||||
return Err(AppError::InvalidRequest(
|
||||
"Publish name must be at most 128 characters long".to_string(),
|
||||
));
|
||||
if publish_name.len() > MAX_PUBLISH_NAME_LENGTH {
|
||||
return Err(AppError::PublishNameTooLong {
|
||||
given_length: publish_name.len(),
|
||||
max_length: MAX_PUBLISH_NAME_LENGTH,
|
||||
});
|
||||
}
|
||||
|
||||
// Only contain alphanumeric characters and hyphens
|
||||
for c in publish_name.chars() {
|
||||
if !c.is_alphanumeric() && c != '-' {
|
||||
return Err(AppError::InvalidRequest(
|
||||
"Publish name must only contain alphanumeric characters and hyphens".to_string(),
|
||||
));
|
||||
return Err(AppError::PublishNameInvalidCharacter { character: c });
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -90,6 +90,50 @@ async fn test_publish_doc() {
|
|||
.await
|
||||
.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 view_id_1 = uuid::Uuid::new_v4();
|
||||
let publish_name_2 = "publish-name-2";
|
||||
|
|
|
|||
Loading…
Reference in New Issue