api/router.rs: return Result in upload handler

This commit is contained in:
Dietmar Maurer 2019-01-17 12:03:38 +01:00
parent 23bb8780d4
commit 0ee0ad5bf3
3 changed files with 12 additions and 5 deletions

View File

@ -12,7 +12,7 @@ pub type BoxFut = Box<Future<Item = Response<Body>, Error = failure::Error> + Se
type ApiHandlerFn = fn(Value, &ApiMethod) -> Result<Value, Error>; type ApiHandlerFn = fn(Value, &ApiMethod) -> Result<Value, Error>;
type ApiUploadHandlerFn = fn(hyper::Body, Value, &ApiUploadMethod) -> BoxFut; type ApiUploadHandlerFn = fn(hyper::Body, Value, &ApiUploadMethod) -> Result<BoxFut, Error>;
pub struct ApiMethod { pub struct ApiMethod {
pub parameters: ObjectSchema, pub parameters: ObjectSchema,

View File

@ -39,7 +39,7 @@ impl Future for UploadCaTar {
} }
} }
fn upload_catar(req_body: hyper::Body, param: Value, _info: &ApiUploadMethod) -> BoxFut { fn upload_catar(req_body: hyper::Body, param: Value, _info: &ApiUploadMethod) -> Result<BoxFut, Error> {
let store = param["name"].as_str().unwrap(); let store = param["name"].as_str().unwrap();
let archive_name = param["archive_name"].as_str().unwrap(); let archive_name = param["archive_name"].as_str().unwrap();
@ -47,7 +47,8 @@ fn upload_catar(req_body: hyper::Body, param: Value, _info: &ApiUploadMethod) ->
println!("Upload {}.catar to {} ({}.aidx)", archive_name, store, archive_name); println!("Upload {}.catar to {} ({}.aidx)", archive_name, store, archive_name);
let chunk_size = 4*1024*1024; let chunk_size = 4*1024*1024;
let datastore = DataStore::lookup_datastore(store).unwrap().clone();
let datastore = DataStore::lookup_datastore(store)?;
let mut full_archive_name = PathBuf::from(archive_name); let mut full_archive_name = PathBuf::from(archive_name);
full_archive_name.set_extension("aidx"); full_archive_name.set_extension("aidx");
@ -66,7 +67,7 @@ fn upload_catar(req_body: hyper::Body, param: Value, _info: &ApiUploadMethod) ->
Ok(response) Ok(response)
}); });
Box::new(resp) Ok(Box::new(resp))
} }
pub fn api_method_upload_catar() -> ApiUploadMethod { pub fn api_method_upload_catar() -> ApiUploadMethod {

View File

@ -208,7 +208,13 @@ fn handle_upload_api_request(
} }
}; };
(info.handler)(req_body, params, info) match (info.handler)(req_body, params, info) {
Ok(future) => future,
Err(err) => {
let resp = (formatter.format_result)(Err(Error::from(err)));
Box::new(future::ok(resp))
}
}
} }
fn get_index() -> BoxFut { fn get_index() -> BoxFut {