Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#wip hitting resource limits #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 12 additions & 12 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ impl Client {
let res = client.post(url.clone())
.json(args)
.send()
.map_err(|e| Error::could_not_access_url(&url, e))?;
.map_err(|e| Error::parse_error(&url, e))?;
self.handle_response(res)
.map_err(|e| Error::could_not_access_url(&url, e))
.map_err(|e| Error::parse_error(&url, e))
}

/// Create a new resource, and wait until it is ready.
Expand All @@ -90,9 +90,9 @@ impl Client {
.header(reqwest::header::ContentType(body.mime_type()))
.body(body)
.send()
.map_err(|e| Error::could_not_access_url(&url, e))?;
.map_err(|e| Error::parse_error(&url, e))?;
self.handle_response(res)
.map_err(|e| Error::could_not_access_url(&url, e))
.map_err(|e| Error::parse_error(&url, e))
}

/// Create a BigML data source using data from the specified path. We
Expand Down Expand Up @@ -122,12 +122,12 @@ impl Client {
let res = client.request(reqwest::Method::Put, url.clone())
.json(update)
.send()
.map_err(|e| Error::could_not_access_url(&url, e))?;
.map_err(|e| Error::parse_error(&url, e))?;
// Parse our result as JSON, because it often seems to be missing
// fields like `name` for `Source`. It's not always a complete,
// valid resource.
let _json: serde_json::Value = self.handle_response(res)
.map_err(|e| Error::could_not_access_url(&url, e))?;
.map_err(|e| Error::parse_error(&url, e))?;

Ok(())
}
Expand All @@ -138,9 +138,9 @@ impl Client {
let client = reqwest::Client::new();
let res = client.get(url.clone())
.send()
.map_err(|e| Error::could_not_access_url(&url, e))?;
.map_err(|e| Error::parse_error(&url, e))?;
self.handle_response(res)
.map_err(|e| Error::could_not_access_url(&url, e))
.map_err(|e| Error::parse_error(&url, e))
}

/// Poll an existing resource, returning it once it's ready.
Expand Down Expand Up @@ -177,7 +177,7 @@ impl Client {
} else {
WaitStatus::Waiting
}
}).map_err(|e| Error::could_not_access_url(&url, e))
}).map_err(|e| Error::parse_error(&url, e))
}

/// Download a resource as a CSV file. This only makes sense for
Expand Down Expand Up @@ -220,7 +220,7 @@ impl Client {
try_with_temporary_failure!(self.response_to_err(res));
unreachable!()
}
}).map_err(|e| Error::could_not_access_url(&url, e))
}).map_err(|e| Error::parse_error(&url, e))
}

/// Delete the specified resource.
Expand All @@ -229,13 +229,13 @@ impl Client {
let client = reqwest::Client::new();
let res = client.request(reqwest::Method::Delete, url.clone())
.send()
.map_err(|e| Error::could_not_access_url(&url, e))?;
.map_err(|e| Error::parse_error(&url, e))?;
if res.status().is_success() {
debug!("Deleted {}", &resource);
Ok(())
} else {
self.response_to_err(res)
.map_err(|e| Error::could_not_access_url(&url, e))
.map_err(|e| Error::parse_error(&url, e))
}
}

Expand Down
32 changes: 31 additions & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ pub enum Error {
/// We could not access the specified URL.
///
/// **WARNING:** Do not construct this directly, but use
/// `Error::could_not_access_url` to handle various URL sanitization and
/// `Error::parse_error` to handle various URL sanitization and
/// security issues.
#[fail(display = "error accessing '{}': {}", url, error)]
CouldNotAccessUrl { url: Url, /*#[cause]*/ error: failure::Error },

#[fail(display = "hit resource limits '{}': {}", url, error)]
HitResourceLimits { url: Url, /*#[cause]*/ error: failure::Error },

/// We could not get an output value from a WhizzML script.
#[fail(display = "could not get WhizzML output '{}': {}", name, error)]
CouldNotGetOutput { name: String, /*#[cause]*/ error: failure::Error },
Expand Down Expand Up @@ -72,6 +75,20 @@ pub enum Error {
}

impl Error {
pub(crate) fn parse_error<E>(
url: &Url,
error: E,
) -> Error
where
E: Into<failure::Error>,
{
// this won't work
match error.http_status {
402 => Error::hit_resource_limits(&url, error),
_ => Error::could_not_access_url(&url, error),
}
}

/// Construct an `Error::CouldNotAccessUrl` value, taking care to
/// sanitize the URL query.
pub(crate) fn could_not_access_url<E>(
Expand All @@ -87,6 +104,19 @@ impl Error {
}
}

pub(crate) fn hit_resource_limits<E>(
url: &Url,
error: E,
) -> Error
where
E: Into<failure::Error>,
{
Error::HitResourceLimits {
url: url_without_api_key(&url),
error: error.into(),
}
}

pub(crate) fn could_not_get_output<E>(
name: &str,
error: E,
Expand Down