From 0ee0ad5bf3db6642c7002d1ec58066ad028806f3 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 17 Jan 2019 12:03:38 +0100 Subject: [PATCH] api/router.rs: return Result in upload handler --- src/api/router.rs | 2 +- src/api3/admin/datastore/upload_catar.rs | 7 ++++--- src/server/rest.rs | 8 +++++++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/api/router.rs b/src/api/router.rs index 03d5c732..bdcc0295 100644 --- a/src/api/router.rs +++ b/src/api/router.rs @@ -12,7 +12,7 @@ pub type BoxFut = Box, Error = failure::Error> + Se type ApiHandlerFn = fn(Value, &ApiMethod) -> Result; -type ApiUploadHandlerFn = fn(hyper::Body, Value, &ApiUploadMethod) -> BoxFut; +type ApiUploadHandlerFn = fn(hyper::Body, Value, &ApiUploadMethod) -> Result; pub struct ApiMethod { pub parameters: ObjectSchema, diff --git a/src/api3/admin/datastore/upload_catar.rs b/src/api3/admin/datastore/upload_catar.rs index 165841d7..5434cb45 100644 --- a/src/api3/admin/datastore/upload_catar.rs +++ b/src/api3/admin/datastore/upload_catar.rs @@ -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 { let store = param["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); 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); full_archive_name.set_extension("aidx"); @@ -66,7 +67,7 @@ fn upload_catar(req_body: hyper::Body, param: Value, _info: &ApiUploadMethod) -> Ok(response) }); - Box::new(resp) + Ok(Box::new(resp)) } pub fn api_method_upload_catar() -> ApiUploadMethod { diff --git a/src/server/rest.rs b/src/server/rest.rs index 4ca98815..49f3f676 100644 --- a/src/server/rest.rs +++ b/src/server/rest.rs @@ -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 {