WIP Introduce the invalid_task_uid error code

This commit is contained in:
Clément Renault 2022-11-03 12:20:54 +01:00 committed by Loïc Lecrenier
parent b20025c01e
commit 932414bf72
3 changed files with 22 additions and 5 deletions

View File

@ -27,6 +27,10 @@ pub enum Error {
SwapDuplicateIndexesFound(Vec<String>),
#[error("Corrupted dump.")]
CorruptedDump,
#[error(
"Tasks uids must be a comma-separated list of numbers. `{task_uids}` is invalid {error_message}"
)]
InvalidTaskUids { task_uids: String, error_message: String },
#[error("Task `{0}` not found.")]
TaskNotFound(TaskId),
#[error("Query parameters to filter the tasks to delete are missing. Available query parameters are: `uid`, `indexUid`, `status`, `type`.")]
@ -71,6 +75,7 @@ impl ErrorCode for Error {
Error::IndexAlreadyExists(_) => Code::IndexAlreadyExists,
Error::SwapDuplicateIndexesFound(_) => Code::DuplicateIndexFound,
Error::SwapDuplicateIndexFound(_) => Code::DuplicateIndexFound,
Error::InvalidTaskUids { .. } => Code::InvalidTaskUid,
Error::TaskNotFound(_) => Code::TaskNotFound,
Error::TaskDeletionWithEmptyQuery => Code::TaskDeletionWithEmptyQuery,
Error::TaskCancelationWithEmptyQuery => Code::TaskCancelationWithEmptyQuery,

View File

@ -211,7 +211,7 @@ pub struct TaskDateQuery {
pub struct TasksFilterQuery {
#[serde(rename = "type")]
kind: Option<CS<StarOr<Kind>>>,
uid: Option<CS<u32>>,
uid: Option<CS<TaskId>>,
status: Option<CS<StarOr<Status>>>,
index_uid: Option<CS<StarOr<String>>>,
#[serde(default = "DEFAULT_LIMIT")]
@ -457,15 +457,25 @@ async fn get_tasks(
async fn get_task(
index_scheduler: GuardedData<ActionPolicy<{ actions::TASKS_GET }>, Data<IndexScheduler>>,
task_id: web::Path<TaskId>,
task_uid: web::Path<String>,
req: HttpRequest,
analytics: web::Data<dyn Analytics>,
) -> Result<HttpResponse, ResponseError> {
let task_id = task_id.into_inner();
let task_uid_string = task_uid.into_inner();
let task_uid: TaskId = match task_uid_string.parse() {
Ok(id) => id,
Err(e) => {
return Err(index_scheduler::Error::InvalidTaskUids {
task_uids: task_uid_string,
error_message: e.to_string(),
}
.into())
}
};
analytics.publish("Tasks Seen".to_string(), json!({ "per_task_uid": true }), Some(&req));
let query = index_scheduler::Query { uid: Some(vec![task_id]), ..Query::default() };
let query = index_scheduler::Query { uid: Some(vec![task_uid]), ..Query::default() };
if let Some(task) = index_scheduler
.get_tasks_from_authorized_indexes(
@ -477,7 +487,7 @@ async fn get_task(
let task_view = TaskView::from_task(task);
Ok(HttpResponse::Ok().json(task_view))
} else {
Err(index_scheduler::Error::TaskNotFound(task_id).into())
Err(index_scheduler::Error::TaskNotFound(task_uid).into())
}
}

View File

@ -147,6 +147,7 @@ pub enum Code {
MissingMasterKey,
NoSpaceLeftOnDevice,
DumpNotFound,
InvalidTaskUid,
TaskNotFound,
TaskDeletionWithEmptyQuery,
TaskCancelationWithEmptyQuery,
@ -238,6 +239,7 @@ impl Code {
MissingMasterKey => {
ErrCode::authentication("missing_master_key", StatusCode::UNAUTHORIZED)
}
InvalidTaskUid => ErrCode::invalid("invalid_task_uid", StatusCode::BAD_REQUEST),
TaskNotFound => ErrCode::invalid("task_not_found", StatusCode::NOT_FOUND),
TaskDeletionWithEmptyQuery => {
ErrCode::invalid("missing_task_filters", StatusCode::BAD_REQUEST)