diff options
author | Guillaume Pasquet <dev@etenil.net> | 2022-02-20 09:30:46 +0000 |
---|---|---|
committer | Guillaume Pasquet <dev@etenil.net> | 2022-02-20 09:30:46 +0000 |
commit | f69d2801069850b48c6424b50d0107799d56e2f8 (patch) | |
tree | eaba8081a01fa69bcb23576daf6b3178e8c13765 /src | |
parent | ca6531ed467d1772f7b4afed05a69d666b071c14 (diff) |
Handle errors (more) gracefully
Diffstat (limited to 'src')
-rw-r--r-- | src/barbfile.rs | 2 | ||||
-rw-r--r-- | src/executor.rs | 44 | ||||
-rw-r--r-- | src/main.rs | 17 |
3 files changed, 53 insertions, 10 deletions
diff --git a/src/barbfile.rs b/src/barbfile.rs index a039253..1a17796 100644 --- a/src/barbfile.rs +++ b/src/barbfile.rs @@ -158,7 +158,7 @@ impl FromStr for BarbFile { } if let Some(_) = line.find(':') { - headers.push(decode_header(line).unwrap()); + headers.push(decode_header(line).map_err(|_| BarbParseError {})?); } if let None = filter { diff --git a/src/executor.rs b/src/executor.rs index b5f8e96..4930c5f 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -2,6 +2,7 @@ use crate::barbfile::BarbFile; use std::collections::HashMap; use ureq; +use ureq::Error as UreqError; pub struct Context { vars: HashMap<String, String>, @@ -18,6 +19,7 @@ impl Context { Context { vars: toto } } + #[cfg(test)] pub fn empty() -> Context { Context { vars: HashMap::new(), @@ -55,7 +57,7 @@ impl Executor { Executor { context } } - pub fn execute(&mut self, bfile: BarbFile, print_headers: bool) -> Result<ureq::Response, String> { + fn make_req(&self, bfile: &BarbFile, print_headers: bool) -> ureq::Request { let mut req = ureq::request( bfile.method_as_string().as_str(), self.context.substitute(&bfile.url()).as_str(), @@ -74,15 +76,27 @@ impl Executor { ); } } + req + } - match bfile.method().takes_body() { + fn run(&self, bfile: &BarbFile, req: ureq::Request) -> Result<ureq::Response, String> { + let resp = match bfile.method().takes_body() { true => match bfile.body() { Some(body) => req.send_string(body.as_str()), None => req.call(), }, false => req.call(), + }; + + match resp { + Ok(resp) => Ok(resp), + Err(UreqError::Status(_, resp)) => Ok(resp), + Err(UreqError::Transport(transp)) => Err(String::from(transp.to_string())) } - .map_err(|_| String::from("Error")) + } + + pub fn execute(&mut self, bfile: BarbFile, print_headers: bool) -> Result<ureq::Response, String> { + self.run(&bfile, self.make_req(&bfile, print_headers)) } } @@ -90,6 +104,7 @@ impl Executor { #[cfg(test)] mod tests { use super::*; + use std::str::FromStr; #[test] fn test_context_key_str() { @@ -113,4 +128,27 @@ mod tests { String::from("blah bar baz blah") ); } + + #[test] + fn test_make_req_simple_get() { + let executor = Executor::new(Context::empty()); + let bfile = BarbFile::from_str("#GET^http://foo.bar\n\n").unwrap(); + let req = executor.make_req(&bfile, false); + + assert_eq!(req.url(), "http://foo.bar"); + assert_eq!(req.method(), "GET"); + } + + #[test] + fn test_make_req_simple_headers() { + let executor = Executor::new(Context::empty()); + let bfile = BarbFile::from_str( + "#GET^http://foo.bar\n#Foo: Bar\n#Bar: Baz\n\n" + ).unwrap(); + let req = executor.make_req(&bfile, false); + + assert_eq!(req.header_names(), vec![String::from("foo"), String::from("bar")]); + assert_eq!(req.header("foo"), Some("Bar")); + assert_eq!(req.header("bar"), Some("Baz")); + } } diff --git a/src/main.rs b/src/main.rs index 4ddc809..f3891ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,14 +50,13 @@ impl Args { } } -fn run_file(args: &Args, executor: &mut Executor, file_name: &String) { +fn run_file(args: &Args, executor: &mut Executor, file_name: &String) -> Result<(), String> { let bfile = BarbFile::from_str( fs::read_to_string(file_name.as_str()) - .expect("Failed to read file") + .map_err(|_| format!("Failed to read file '{}'", file_name))? .as_str(), - ) - .expect("Failed to parse file"); - let response = executor.execute(bfile, args.req_headers()).unwrap(); + ).map_err(|_| format!("Failed to parse file '{}'", file_name))?; + let response = executor.execute(bfile, args.req_headers())?; if args.print_headers() { println!("{} {}", response.status(), response.status_text()); @@ -65,6 +64,7 @@ fn run_file(args: &Args, executor: &mut Executor, file_name: &String) { println!( "{}: {}", header_name, + // Header is guaranteed to exist response.header(header_name.as_str()).unwrap() ); } @@ -82,6 +82,8 @@ fn run_file(args: &Args, executor: &mut Executor, file_name: &String) { } ); } + + Ok(()) } fn main() { @@ -90,6 +92,9 @@ fn main() { let mut executor = Executor::new(Context::new(env::vars())); for file in args.files_iter() { - run_file(&args, &mut executor, &file); + match run_file(&args, &mut executor, &file) { + Ok(()) => (), + Err(err) => println!("{}", err) + } } } |