Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit

Permalink
added better error handling with anyhow
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-schneider-vtex committed Mar 21, 2023
1 parent 3a89b9f commit a713d91
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
9 changes: 8 additions & 1 deletion 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 @@ -19,3 +19,4 @@ serde_json = "1.0"
colored = "2.0"
xmltree = "0.10"
itertools = "0.10.5"
anyhow = "1.0.70"
11 changes: 6 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use clap::{Parser, ValueEnum};
use colored::Colorize;
use itertools::Itertools;
use reqwest;
use anyhow::{Result, Context};

#[derive(Copy, Clone, Debug, ValueEnum, PartialEq)]
#[clap(rename_all = "UPPER")]
Expand Down Expand Up @@ -105,7 +106,7 @@ fn get_xml_keys(xml: &xmltree::Element) -> Vec<String> {
}

//TODO: Run any script from bash here
fn run_scripts(scripts: &[String]) -> Result<(), Box<dyn std::error::Error>> {
fn run_scripts(scripts: &[String]) -> Result<()> {
todo!("Not implemented")
}

Expand All @@ -123,7 +124,7 @@ fn get_format(data: &str) -> Option<DataFormat> {
}
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
fn main() -> Result<()> {
let args = Args::parse();

let nurl = if !(args.url.starts_with("http://") || args.url.starts_with("https://")) {
Expand All @@ -142,7 +143,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Verb::HEAD => reqwest::blocking::Client::new().head(nurl),
}
.body(args.data.unwrap_or("".into()))
.send()?;
.send().context("While sending request")?;

let headers = resp.headers().clone();

Expand All @@ -157,8 +158,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut data: Option<String> = None;

let len = if resp.content_length().is_some() {
let len = resp.content_length().unwrap();
data = Some(resp.text()?);
let len = resp.content_length().context("While readig Content-Length")?;
data = Some(resp.text().context("While reading reponse as text")?);
len
} else {
data = Some(resp.text()?);
Expand Down

0 comments on commit a713d91

Please sign in to comment.