fix: add validation to check for empty file array during gist creation

master
Aravinth Manivannan 2022-02-23 20:32:32 +05:30
parent 0ba7559425
commit f5a22a2916
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
3 changed files with 15 additions and 0 deletions

View File

@ -69,6 +69,10 @@ async fn new(
id: Identity,
db: crate::DB,
) -> ServiceResult<impl Responder> {
if payload.files.is_empty() {
return Err(ServiceError::GistEmpty);
}
let username = id.identity().unwrap();
let mut gist = data
.new_gist(db.as_ref(), &payload.to_create_gist(&username))

View File

@ -191,6 +191,9 @@ impl Data {
gist_id: GistID<'_>,
files: &[FileInfo],
) -> ServiceResult<()> {
if files.is_empty() {
return Err(ServiceError::GistEmpty);
}
// TODO change updated in DB
let inner = |repo: &mut Repository| -> ServiceResult<()> {
let mut tree_builder = repo.treebuilder(None).unwrap();

View File

@ -128,6 +128,12 @@ pub enum ServiceError {
#[display(fmt = "Unauthorized {}", _0)]
UnauthorizedOperation(#[error(not(source))] String),
#[display(fmt = "Bad request: {}", _0)]
BadRequest(#[error(not(source))] String),
#[display(fmt = "Gist is empty, at least one file is required to create gist")]
GistEmpty,
}
impl From<CredsError> for ServiceError {
@ -231,6 +237,8 @@ impl ResponseError for ServiceError {
ServiceError::EmptyComment => StatusCode::BAD_REQUEST,
ServiceError::UnauthorizedOperation(_) => StatusCode::UNAUTHORIZED,
ServiceError::BadRequest(_) => StatusCode::BAD_REQUEST,
ServiceError::GistEmpty => StatusCode::BAD_REQUEST,
}
}
}