aquatic_ws: add more anyhow error context annotations

This commit is contained in:
Joakim Frostegård 2020-05-23 17:15:11 +02:00
parent 65684472a6
commit b9ee290ca0
4 changed files with 19 additions and 12 deletions

View file

@ -1,7 +1,6 @@
# TODO
## aquatic_ws
* add more anyhow context annotations for errors
* add sensible logging method, maybe stderrlog with quiet as default
## aquatic_udp

View file

@ -119,16 +119,19 @@ pub fn create_tls_acceptor(
) -> anyhow::Result<Option<TlsAcceptor>> {
if config.network.use_tls {
let mut identity_bytes = Vec::new();
let mut file = File::open(&config.network.tls_pkcs12_path)?;
let mut file = File::open(&config.network.tls_pkcs12_path)
.context("Couldn't open pkcs12 identity file")?;
file.read_to_end(&mut identity_bytes)?;
file.read_to_end(&mut identity_bytes)
.context("Couldn't read pkcs12 identity file")?;
let identity = Identity::from_pkcs12(
&mut identity_bytes,
&config.network.tls_pkcs12_password
)?;
).context("Couldn't parse pkcs12 identity file")?;
let acceptor = TlsAcceptor::new(identity)?;
let acceptor = TlsAcceptor::new(identity)
.context("Couldn't create TlsAcceptor from pkcs12 identity")?;
Ok(Some(acceptor))
} else {

View file

@ -42,7 +42,7 @@ pub fn run_socket_worker(
},
Err(err) => {
socket_worker_statuses.lock()[socket_worker_index] = Some(
Err(format!("Couldn't create TCP listener: {}", err))
Err(format!("Couldn't open socket: {:#}", err))
);
}
}

View file

@ -16,19 +16,24 @@ pub fn create_listener(
TcpBuilder::new_v4()
} else {
TcpBuilder::new_v6()
}?;
}.context("Couldn't create TcpBuilder")?;
if config.network.ipv6_only {
builder = builder.only_v6(true)
.context("Failed setting ipv6_only to true")?
.context("Couldn't put socket in ipv6 only mode")?
}
builder = builder.reuse_port(true)?;
builder = builder.bind(&config.network.address)?;
builder = builder.reuse_port(true)
.context("Couldn't put socket in reuse_port mode")?;
builder = builder.bind(&config.network.address).with_context(||
format!("Couldn't bind socket to address {}", config.network.address)
)?;
let listener = builder.listen(128)?;
let listener = builder.listen(128)
.context("Couldn't listen for connections on socket")?;
listener.set_nonblocking(true)?;
listener.set_nonblocking(true)
.context("Couldn't put tcp listener in non-blocking mode")?;
Ok(listener)
}