Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
refactor: handle success in endpoint caller
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleymichal committed Aug 7, 2019
1 parent 374860d commit 5301e51
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
5 changes: 4 additions & 1 deletion src/commands/kv/create_namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ pub fn create_namespace(title: &str) -> Result<(), failure::Error> {
},
});

super::print_response(response);
match response {
Ok(success) => message::success(&format!("Success: {:#?}", success.result)),
Err(e) => super::print_error(e),
}

Ok(())
}
26 changes: 11 additions & 15 deletions src/commands/kv/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use cloudflare::auth::Credentials;
use cloudflare::response::APIFailure;
use cloudflare::response::APIResponse;
use cloudflare::response::APIResult;

use cloudflare::HTTPAPIClient;

use crate::settings;
Expand All @@ -26,22 +25,19 @@ fn account_id() -> Result<String, failure::Error> {
Ok(project.account_id)
}

fn print_response<T: APIResult>(response: APIResponse<T>) {
match response {
Ok(success) => message::success(&format!("Success: {:#?}", success.result)),
Err(e) => match e {
APIFailure::Error(_status, errors) => {
for error in errors {
message::warn(&format!("Error {}: {}", error.code, error.message,));
fn print_error(e: APIFailure) {
match e {
APIFailure::Error(_status, errors) => {
for error in errors {
message::warn(&format!("Error {}: {}", error.code, error.message,));

let suggestion = help(error.code);
if !suggestion.is_empty() {
message::help(suggestion);
}
let suggestion = help(error.code);
if !suggestion.is_empty() {
message::help(suggestion);
}
}
APIFailure::Invalid(reqwest_err) => message::warn(&format!("Error: {}", reqwest_err)),
},
}
APIFailure::Invalid(reqwest_err) => message::warn(&format!("Error: {}", reqwest_err)),
}
}

Expand Down

0 comments on commit 5301e51

Please sign in to comment.