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>), SwapDuplicateIndexesFound(Vec<String>),
#[error("Corrupted dump.")] #[error("Corrupted dump.")]
CorruptedDump, 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.")] #[error("Task `{0}` not found.")]
TaskNotFound(TaskId), TaskNotFound(TaskId),
#[error("Query parameters to filter the tasks to delete are missing. Available query parameters are: `uid`, `indexUid`, `status`, `type`.")] #[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::IndexAlreadyExists(_) => Code::IndexAlreadyExists,
Error::SwapDuplicateIndexesFound(_) => Code::DuplicateIndexFound, Error::SwapDuplicateIndexesFound(_) => Code::DuplicateIndexFound,
Error::SwapDuplicateIndexFound(_) => Code::DuplicateIndexFound, Error::SwapDuplicateIndexFound(_) => Code::DuplicateIndexFound,
Error::InvalidTaskUids { .. } => Code::InvalidTaskUid,
Error::TaskNotFound(_) => Code::TaskNotFound, Error::TaskNotFound(_) => Code::TaskNotFound,
Error::TaskDeletionWithEmptyQuery => Code::TaskDeletionWithEmptyQuery, Error::TaskDeletionWithEmptyQuery => Code::TaskDeletionWithEmptyQuery,
Error::TaskCancelationWithEmptyQuery => Code::TaskCancelationWithEmptyQuery, Error::TaskCancelationWithEmptyQuery => Code::TaskCancelationWithEmptyQuery,

View File

@ -211,7 +211,7 @@ pub struct TaskDateQuery {
pub struct TasksFilterQuery { pub struct TasksFilterQuery {
#[serde(rename = "type")] #[serde(rename = "type")]
kind: Option<CS<StarOr<Kind>>>, kind: Option<CS<StarOr<Kind>>>,
uid: Option<CS<u32>>, uid: Option<CS<TaskId>>,
status: Option<CS<StarOr<Status>>>, status: Option<CS<StarOr<Status>>>,
index_uid: Option<CS<StarOr<String>>>, index_uid: Option<CS<StarOr<String>>>,
#[serde(default = "DEFAULT_LIMIT")] #[serde(default = "DEFAULT_LIMIT")]
@ -457,15 +457,25 @@ async fn get_tasks(
async fn get_task( async fn get_task(
index_scheduler: GuardedData<ActionPolicy<{ actions::TASKS_GET }>, Data<IndexScheduler>>, index_scheduler: GuardedData<ActionPolicy<{ actions::TASKS_GET }>, Data<IndexScheduler>>,
task_id: web::Path<TaskId>, task_uid: web::Path<String>,
req: HttpRequest, req: HttpRequest,
analytics: web::Data<dyn Analytics>, analytics: web::Data<dyn Analytics>,
) -> Result<HttpResponse, ResponseError> { ) -> 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)); 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 if let Some(task) = index_scheduler
.get_tasks_from_authorized_indexes( .get_tasks_from_authorized_indexes(
@ -477,7 +487,7 @@ async fn get_task(
let task_view = TaskView::from_task(task); let task_view = TaskView::from_task(task);
Ok(HttpResponse::Ok().json(task_view)) Ok(HttpResponse::Ok().json(task_view))
} else { } 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, MissingMasterKey,
NoSpaceLeftOnDevice, NoSpaceLeftOnDevice,
DumpNotFound, DumpNotFound,
InvalidTaskUid,
TaskNotFound, TaskNotFound,
TaskDeletionWithEmptyQuery, TaskDeletionWithEmptyQuery,
TaskCancelationWithEmptyQuery, TaskCancelationWithEmptyQuery,
@ -238,6 +239,7 @@ impl Code {
MissingMasterKey => { MissingMasterKey => {
ErrCode::authentication("missing_master_key", StatusCode::UNAUTHORIZED) 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), TaskNotFound => ErrCode::invalid("task_not_found", StatusCode::NOT_FOUND),
TaskDeletionWithEmptyQuery => { TaskDeletionWithEmptyQuery => {
ErrCode::invalid("missing_task_filters", StatusCode::BAD_REQUEST) ErrCode::invalid("missing_task_filters", StatusCode::BAD_REQUEST)