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

Handle pass timeout error #11

Merged
merged 1 commit into from
Mar 10, 2024
Merged
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
32 changes: 25 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub enum Error {
Http(reqwest::Error),
MissingDataFile(String),
NoRouteFound(isize),
PassTimeout { pass: String, timeout: String },
Other(String),
}

Expand All @@ -62,6 +63,9 @@ impl std::fmt::Display for Error {
Error::Other(e) => write!(f, "Error: {}", e),
Error::Http(e) => write!(f, "HTTP error: {}", e),
Error::MissingDataFile(s) => write!(f, "Missing data file: {}", s),
Error::PassTimeout { pass, timeout } => {
write!(f, "Pass {} timeout after {} seconds", pass, timeout)
}
Error::NoRouteFound(i) => write!(f, "No route found: {}", i),
}
}
Expand Down Expand Up @@ -128,13 +132,6 @@ impl Brouter {
.send()
.map_err(Error::Http)?;

if response.status() == reqwest::StatusCode::BAD_REQUEST {
// 400 is used by brouter for any sort of error
let text = response.text().map_err(Error::Http)?;

return Err(Error::Other(text));
}

response.error_for_status().map_err(Error::Http).map(|_| ())
}

Expand Down Expand Up @@ -278,6 +275,8 @@ impl Brouter {
.error_for_status()
.map_err(Error::Http)?;

let status = response.status();

let text = response.bytes().map_err(Error::Http)?.to_vec();

if let Some(m) = regex!("datafile (.*) not found\n"B).captures(text.as_slice()) {
Expand All @@ -295,6 +294,25 @@ impl Brouter {
));
}

if let Some(m) =
regex!("pass([0-9]) timeout after ([0-9]+) seconds\n"B).captures(text.as_slice())
{
let pass = String::from_utf8_lossy(m.get(1).unwrap().as_bytes())
.to_string()
.parse()
.unwrap();

let timeout = String::from_utf8_lossy(m.get(2).unwrap().as_bytes())
.to_string()
.parse()
.unwrap();
return Err(Error::PassTimeout { pass, timeout });
}

if status == reqwest::StatusCode::BAD_REQUEST {
return Err(Error::Other(format!("HTTP error: {}", status)));
}

let gpx: gpx::Gpx = gpx::read(BufReader::new(text.as_slice())).map_err(|_e| {
Error::InvalidGpx(String::from_utf8_lossy(text.as_slice()).to_string())
})?;
Expand Down