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

Gabbi/improve bulk error handling #466

Merged
merged 7 commits into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ ws = "0.9.0"
url = "2.1.0"
walkdir = "2.2.9"
percent-encoding = "1.0.1"
http = "0.1.1"

[dev-dependencies]
assert_cmd = "0.11.1"
Expand Down
15 changes: 14 additions & 1 deletion src/commands/kv/delete_bulk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use failure::bail;

use crate::terminal::message;

const MAX_PAIRS: usize = 10000;

pub fn delete_bulk(namespace_id: &str, filename: &Path) -> Result<(), failure::Error> {
let client = super::api_client()?;
let account_id = super::account_id()?;
Expand All @@ -36,10 +38,21 @@ pub fn delete_bulk(namespace_id: &str, filename: &Path) -> Result<(), failure::E
Err(e) => bail!(e),
};

// Validate that bulk delete is within API constraints
let keys = keys?;
// Check number of pairs is under limit
if keys.len() > MAX_PAIRS {
bail!(
"Number of keys to delete ({}) exceeds max of {}",
keys.len(),
MAX_PAIRS
);
}

let response = client.request(&DeleteBulk {
account_identifier: &account_id,
namespace_identifier: namespace_id,
bulk_keys: keys?,
bulk_keys: keys,
});

match response {
Expand Down
13 changes: 12 additions & 1 deletion src/commands/kv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::path::Path;
use cloudflare::framework::auth::Credentials;
use cloudflare::framework::response::ApiFailure;
use cloudflare::framework::HttpApiClient;
use http::status::StatusCode;
use failure::bail;

use crate::settings;
Expand Down Expand Up @@ -48,7 +49,8 @@ fn account_id() -> Result<String, failure::Error> {

fn print_error(e: ApiFailure) {
match e {
ApiFailure::Error(_status, api_errors) => {
ApiFailure::Error(status, api_errors) => {
give_status_code_context(status);
for error in api_errors.errors {
message::warn(&format!("Error {}: {}", error.code, error.message));

Expand All @@ -62,6 +64,15 @@ fn print_error(e: ApiFailure) {
}
}

// For handling cases where the API gateway returns errors via HTTP status codes
// (no KV error code is given).
fn give_status_code_context(status_code: StatusCode) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should probably eventually be pulled up and out bc the same issue would come up on any request to any api endpoint; however there's no use doing that now.

match status_code {
StatusCode::PAYLOAD_TOO_LARGE => message::warn("Returned status code 413, Payload Too Large. Make sure your upload is less than 100MB in size"),
_ => (),
}
}

fn help(error_code: u16) -> &'static str {
// https://api.cloudflare.com/#workers-kv-namespace-errors
match error_code {
Expand Down
18 changes: 16 additions & 2 deletions src/commands/kv/write_bulk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use failure::bail;

use crate::terminal::message;

const MAX_PAIRS: usize = 10000;

pub fn write_bulk(namespace_id: &str, filename: &Path) -> Result<(), failure::Error> {
let client = super::api_client()?;
let account_id = super::account_id()?;
Expand All @@ -38,10 +40,22 @@ pub fn write_bulk(namespace_id: &str, filename: &Path) -> Result<(), failure::Er
Err(e) => bail!(e),
};

// Validate that bulk upload is within size constraints
let pairs = pairs?;
if pairs.len() > MAX_PAIRS {
bail!(
"Number of key-value pairs to upload ({}) exceeds max of {}",
pairs.len(),
MAX_PAIRS
);
}

message::working("Parsing successful. Uploading all files above");

let response = client.request(&WriteBulk {
account_identifier: &account_id,
namespace_identifier: namespace_id,
bulk_key_value_pairs: pairs?,
bulk_key_value_pairs: pairs,
});

match response {
Expand All @@ -64,7 +78,7 @@ fn parse_directory(directory: &Path) -> Result<Vec<KeyValuePair>, failure::Error

// Need to base64 encode value
let b64_value = base64::encode(&value);
message::working(&format!("Uploading {}...", key.clone()));
message::working(&format!("Parsing {}...", key.clone()));
upload_vec.push(KeyValuePair {
key: key,
value: b64_value,
Expand Down