refactor errors / isolate core/http errors

This commit is contained in:
mpostma 2020-05-22 12:03:57 +02:00
parent e2db197b3f
commit d69180ec67
29 changed files with 585 additions and 480 deletions

View file

@ -5,7 +5,7 @@ use log::error;
use rand::seq::SliceRandom;
use serde::{Deserialize, Serialize};
use crate::error::Error;
use crate::error::{Error, ResponseError};
use crate::helpers::Authentication;
use crate::routes::IndexParam;
use crate::Data;
@ -40,10 +40,9 @@ struct IndexResponse {
}
#[get("/indexes", wrap = "Authentication::Private")]
async fn list_indexes(data: web::Data<Data>) -> Result<HttpResponse, Error> {
async fn list_indexes(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
let reader = data.db.main_read_txn()?;
let mut response = Vec::new();
let mut indexes = Vec::new();
for index_uid in data.db.indexes_uids() {
let index = data.db.open_index(&index_uid);
@ -51,19 +50,19 @@ async fn list_indexes(data: web::Data<Data>) -> Result<HttpResponse, Error> {
match index {
Some(index) => {
let name = index.main.name(&reader)?.ok_or(Error::internal(
"Impossible to get the name of an index",
"Impossible to get the name of an index",
))?;
let created_at = index
.main
.created_at(&reader)?
.ok_or(Error::internal(
"Impossible to get the create date of an index",
"Impossible to get the create date of an index",
))?;
let updated_at = index
.main
.updated_at(&reader)?
.ok_or(Error::internal(
"Impossible to get the last update date of an index",
"Impossible to get the last update date of an index",
))?;
let primary_key = match index.main.schema(&reader) {
@ -81,7 +80,7 @@ async fn list_indexes(data: web::Data<Data>) -> Result<HttpResponse, Error> {
updated_at,
primary_key,
};
response.push(index_response);
indexes.push(index_response);
}
None => error!(
"Index {} is referenced in the indexes list but cannot be found",
@ -90,35 +89,34 @@ async fn list_indexes(data: web::Data<Data>) -> Result<HttpResponse, Error> {
}
}
Ok(HttpResponse::Ok().json(response))
Ok(HttpResponse::Ok().json(indexes))
}
#[get("/indexes/{index_uid}", wrap = "Authentication::Private")]
async fn get_index(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, Error> {
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(Error::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?;
let name = index.main.name(&reader)?.ok_or(Error::internal(
"Impossible to get the name of an index",
"Impossible to get the name of an index",
))?;
let created_at = index
.main
.created_at(&reader)?
.ok_or(Error::internal(
"Impossible to get the create date of an index",
"Impossible to get the create date of an index",
))?;
let updated_at = index
.main
.updated_at(&reader)?
.ok_or(Error::internal(
"Impossible to get the last update date of an index",
"Impossible to get the last update date of an index",
))?;
let primary_key = match index.main.schema(&reader) {
@ -128,14 +126,15 @@ async fn get_index(
},
_ => None,
};
Ok(HttpResponse::Ok().json(IndexResponse {
let index_response = IndexResponse {
name,
uid: path.index_uid.clone(),
created_at,
updated_at,
primary_key,
}))
};
Ok(HttpResponse::Ok().json(index_response))
}
#[derive(Debug, Deserialize)]
@ -150,11 +149,11 @@ struct IndexCreateRequest {
async fn create_index(
data: web::Data<Data>,
body: web::Json<IndexCreateRequest>,
) -> Result<HttpResponse, Error> {
) -> Result<HttpResponse, ResponseError> {
if let (None, None) = (body.name.clone(), body.uid.clone()) {
return Err(Error::bad_request(
"Index creation must have an uid",
));
).into());
}
let uid = match &body.uid {
@ -165,7 +164,7 @@ async fn create_index(
{
uid.to_owned()
} else {
return Err(Error::InvalidIndexUid);
return Err(Error::InvalidIndexUid.into());
}
}
None => loop {
@ -181,39 +180,39 @@ async fn create_index(
.create_index(&uid)
.map_err(Error::create_index)?;
let mut writer = data.db.main_write_txn()?;
let index_response = data.db.main_write::<_, _, ResponseError>(|mut writer| {
let name = body.name.as_ref().unwrap_or(&uid);
created_index.main.put_name(&mut writer, name)?;
let name = body.name.as_ref().unwrap_or(&uid);
created_index.main.put_name(&mut writer, name)?;
let created_at = created_index
.main
.created_at(&writer)?
.ok_or(Error::internal("Impossible to read created at"))?;
let created_at = created_index
.main
.created_at(&writer)?
.ok_or(Error::internal("Impossible to read created at"))?;
let updated_at = created_index
.main
.updated_at(&writer)?
.ok_or(Error::internal("Impossible to read updated at"))?;
let updated_at = created_index
.main
.updated_at(&writer)?
.ok_or(Error::internal("Impossible to read updated at"))?;
if let Some(id) = body.primary_key.clone() {
if let Some(mut schema) = created_index.main.schema(&writer)? {
schema
.set_primary_key(&id)
.map_err(Error::bad_request)?;
created_index.main.put_schema(&mut writer, &schema)?;
if let Some(id) = body.primary_key.clone() {
if let Some(mut schema) = created_index.main.schema(&writer)? {
schema
.set_primary_key(&id)
.map_err(Error::bad_request)?;
created_index.main.put_schema(&mut writer, &schema)?;
}
}
}
let index_response = IndexResponse {
name: name.to_string(),
uid,
created_at,
updated_at,
primary_key: body.primary_key.clone(),
};
Ok(index_response)
})?;
writer.commit()?;
Ok(HttpResponse::Created().json(IndexResponse {
name: name.to_string(),
uid,
created_at,
updated_at,
primary_key: body.primary_key.clone(),
}))
Ok(HttpResponse::Created().json(index_response))
}
#[derive(Debug, Deserialize)]
@ -238,53 +237,51 @@ async fn update_index(
data: web::Data<Data>,
path: web::Path<IndexParam>,
body: web::Json<IndexCreateRequest>,
) -> Result<HttpResponse, Error> {
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
.ok_or(Error::index_not_found(&path.index_uid))?;
let mut writer = data.db.main_write_txn()?;
data.db.main_write::<_, _, ResponseError>(|mut writer| {
if let Some(name) = &body.name {
index.main.put_name(&mut writer, name)?;
}
if let Some(name) = &body.name {
index.main.put_name(&mut writer, name)?;
}
if let Some(id) = body.primary_key.clone() {
if let Some(mut schema) = index.main.schema(&writer)? {
match schema.primary_key() {
Some(_) => {
return Err(Error::bad_request(
"The primary key cannot be updated",
));
}
None => {
schema.set_primary_key(&id)?;
index.main.put_schema(&mut writer, &schema)?;
if let Some(id) = body.primary_key.clone() {
if let Some(mut schema) = index.main.schema(&writer)? {
match schema.primary_key() {
Some(_) => {
return Err(Error::bad_request(
"The primary key cannot be updated",
).into());
}
None => {
schema.set_primary_key(&id)?;
index.main.put_schema(&mut writer, &schema)?;
}
}
}
}
}
index.main.put_updated_at(&mut writer)?;
writer.commit()?;
index.main.put_updated_at(&mut writer)?;
Ok(())
})?;
let reader = data.db.main_read_txn()?;
let name = index.main.name(&reader)?.ok_or(Error::internal(
"Impossible to get the name of an index",
"Impossible to get the name of an index",
))?;
let created_at = index
.main
.created_at(&reader)?
.ok_or(Error::internal(
"Impossible to get the create date of an index",
"Impossible to get the create date of an index",
))?;
let updated_at = index
.main
.updated_at(&reader)?
.ok_or(Error::internal(
"Impossible to get the last update date of an index",
"Impossible to get the last update date of an index",
))?;
let primary_key = match index.main.schema(&reader) {
@ -295,20 +292,22 @@ async fn update_index(
_ => None,
};
Ok(HttpResponse::Ok().json(IndexResponse {
let index_response = IndexResponse {
name,
uid: path.index_uid.clone(),
created_at,
updated_at,
primary_key,
}))
};
Ok(HttpResponse::Ok().json(index_response))
}
#[delete("/indexes/{index_uid}", wrap = "Authentication::Private")]
async fn delete_index(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, Error> {
) -> Result<HttpResponse, ResponseError> {
data.db.delete_index(&path.index_uid)?;
Ok(HttpResponse::NoContent().finish())
@ -327,7 +326,7 @@ struct UpdateParam {
async fn get_update_status(
data: web::Data<Data>,
path: web::Path<UpdateParam>,
) -> Result<HttpResponse, Error> {
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)
@ -342,7 +341,7 @@ async fn get_update_status(
None => Err(Error::NotFound(format!(
"Update {} not found",
path.update_id
))),
)).into()),
}
}
@ -350,7 +349,7 @@ async fn get_update_status(
async fn get_all_updates_status(
data: web::Data<Data>,
path: web::Path<IndexParam>,
) -> Result<HttpResponse, Error> {
) -> Result<HttpResponse, ResponseError> {
let index = data
.db
.open_index(&path.index_uid)