-use anyhow::{anyhow, bail, Result};
+use anyhow::{bail, Context, Result};
use clap::builder::PossibleValuesParser;
use clap::{value_parser, Arg, ArgAction, ArgMatches, Command};
use clap_complete::{generate, Generator, Shell};
p.push(path); // If path is absolute, it replaces the current path.
std::fs::canonicalize(p)
})
- .map_err(|err| anyhow!("Failed to access path `{}`: {}", path.display(), err,))
+ .with_context(|| format!("Failed to access path `{}`", path.display()))
}
fn parse_assets_path<P: AsRef<Path>>(path: P) -> Result<PathBuf> {
#[cfg(feature = "tls")]
use crate::tls::{TlsAcceptor, TlsStream};
-use anyhow::{anyhow, Result};
+use anyhow::{anyhow, Context, Result};
use std::net::{IpAddr, SocketAddr, TcpListener as StdTcpListener};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use rustls::ServerConfig;
#[tokio::main]
-async fn main() {
- run().await.unwrap_or_else(|err| {
- eprintln!("error: {err}");
- std::process::exit(1);
- })
-}
-
-async fn run() -> Result<()> {
+async fn main() -> Result<()> {
logger::init().map_err(|e| anyhow!("Failed to init logger, {e}"))?;
let cmd = build_cli();
let matches = cmd.get_matches();
match bind_addr {
BindAddr::Address(ip) => {
let incoming = create_addr_incoming(SocketAddr::new(*ip, port))
- .map_err(|e| anyhow!("Failed to bind `{ip}:{port}`, {e}"))?;
+ .with_context(|| format!("Failed to bind `{ip}:{port}`"))?;
match args.tls.as_ref() {
#[cfg(feature = "tls")]
Some((certs, key)) => {
#[cfg(unix)]
{
let listener = tokio::net::UnixListener::bind(path)
- .map_err(|e| anyhow!("Failed to bind `{}`, {e}", path.display()))?;
+ .with_context(|| format!("Failed to bind `{}`", path.display()))?;
let acceptor = unix::UnixAcceptor::from_listener(listener);
let new_service = make_service_fn(move |_| serve_func(None));
let server = tokio::spawn(hyper::Server::builder(acceptor).serve(new_service));
}
}
if ipv4 || ipv6 {
- let ifaces = if_addrs::get_if_addrs()
- .map_err(|e| anyhow!("Failed to get local interface addresses: {e}"))?;
+ let ifaces =
+ if_addrs::get_if_addrs().with_context(|| "Failed to get local interface addresses")?;
for iface in ifaces.into_iter() {
let local_ip = iface.ip();
if ipv4 && local_ip.is_ipv4() {
-use anyhow::{anyhow, bail, Result};
+use anyhow::{anyhow, bail, Context as AnyhowContext, Result};
use core::task::{Context, Poll};
use futures::ready;
use hyper::server::accept::Accept;
pub fn load_certs<T: AsRef<Path>>(filename: T) -> Result<Vec<Certificate>> {
// Open certificate file.
let cert_file = fs::File::open(filename.as_ref())
- .map_err(|e| anyhow!("Failed to access `{}`, {e}", filename.as_ref().display()))?;
+ .with_context(|| format!("Failed to access `{}`", filename.as_ref().display()))?;
let mut reader = io::BufReader::new(cert_file);
// Load and return certificate.
- let certs =
- rustls_pemfile::certs(&mut reader).map_err(|_| anyhow!("Failed to load certificate"))?;
+ let certs = rustls_pemfile::certs(&mut reader).with_context(|| "Failed to load certificate")?;
if certs.is_empty() {
bail!("No supported certificate in file");
}
// Load private key from file.
pub fn load_private_key<T: AsRef<Path>>(filename: T) -> Result<PrivateKey> {
let key_file = fs::File::open(filename.as_ref())
- .map_err(|e| anyhow!("Failed to access `{}`, {e}", filename.as_ref().display()))?;
+ .with_context(|| format!("Failed to access `{}`", filename.as_ref().display()))?;
let mut reader = io::BufReader::new(key_file);
// Load and return a single private key.
let keys = rustls_pemfile::read_all(&mut reader)
- .map_err(|e| anyhow!("There was a problem with reading private key: {e}"))?
+ .with_context(|| "There was a problem with reading private key")?
.into_iter()
.find_map(|item| match item {
rustls_pemfile::Item::RSAKey(key)
-use anyhow::{anyhow, Result};
+use anyhow::{anyhow, Context, Result};
use std::{
borrow::Cow,
path::Path,
pub fn unix_now() -> Result<Duration> {
SystemTime::now()
.duration_since(UNIX_EPOCH)
- .map_err(|err| anyhow!("Invalid system time, {err}"))
+ .with_context(|| "Invalid system time")
}
pub fn encode_uri(v: &str) -> String {
.args(["--tls-cert", "wrong", "--tls-key", "tests/data/key.pem"])
.assert()
.failure()
- .stderr(contains("error: Failed to access `wrong`"));
+ .stderr(contains("Failed to access `wrong`"));
Ok(())
}
.args(["--tls-cert", "tests/data/cert.pem", "--tls-key", "wrong"])
.assert()
.failure()
- .stderr(contains("error: Failed to access `wrong`"));
+ .stderr(contains("Failed to access `wrong`"));
Ok(())
}