From cdfe1f02e8e7848c500fb83f952add7158c9368e Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Thu, 30 Mar 2023 19:16:55 -0700 Subject: use anyhow for error handling --- src/x509-info/Cargo.lock | 7 +++++++ src/x509-info/Cargo.toml | 1 + src/x509-info/src/client.rs | 6 +++--- 3 files changed, 11 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/x509-info/Cargo.lock b/src/x509-info/Cargo.lock index 005aee6..e24318b 100644 --- a/src/x509-info/Cargo.lock +++ b/src/x509-info/Cargo.lock @@ -11,6 +11,12 @@ dependencies = [ "libc", ] +[[package]] +name = "anyhow" +version = "1.0.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7de8ce5e0f9f8d88245311066a578d72b7af3e7088f32783804676302df237e4" + [[package]] name = "asn1-rs" version = "0.5.1" @@ -857,6 +863,7 @@ checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" name = "x509-info" version = "0.1.0" dependencies = [ + "anyhow", "chrono", "clap", "rustls", diff --git a/src/x509-info/Cargo.toml b/src/x509-info/Cargo.toml index f6b4e3e..e358a44 100644 --- a/src/x509-info/Cargo.toml +++ b/src/x509-info/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +anyhow = "1.0.70" chrono = {version = "0.4.20", features = ["clock"], default-features = false } clap = {version = "4.0.18", features = ["derive", "cargo"]} rustls = {version = "0.20.7", features = ["dangerous_configuration"]} diff --git a/src/x509-info/src/client.rs b/src/x509-info/src/client.rs index 344e300..8728f93 100644 --- a/src/x509-info/src/client.rs +++ b/src/x509-info/src/client.rs @@ -1,7 +1,7 @@ +use anyhow::{anyhow, Error}; use rustls::{ Certificate, ClientConfig, ClientConnection, OwnedTrustAnchor, RootCertStore, ServerName, }; -use std::error::Error; use std::io::Write; use std::sync::Arc; @@ -31,7 +31,7 @@ pub fn get_certificates( domain: String, port: u16, insecure: bool, -) -> Result, Box> { +) -> Result, Error> { let mut tcp_stream = std::net::TcpStream::connect(format!("{}:{}", domain, port))?; let config = if insecure { @@ -69,6 +69,6 @@ pub fn get_certificates( match conn.peer_certificates() { Some(c) => Ok(c.to_vec()), - None => Err("no certificate found")?, + None => Err(anyhow!("no certificate found for {}", domain))?, } } -- cgit v1.2.3