Skip to content

Commit

Permalink
Use color-eyre instead of error-chain for errors
Browse files Browse the repository at this point in the history
I prefer this crate.
  • Loading branch information
Luflosi committed May 31, 2024
1 parent 72f21aa commit 9bc416e
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 39 deletions.
104 changes: 98 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ license = "AGPL-3.0-only"
[dependencies]
argon2 = { version = "0.5", features = ["std"] }
clap = { version = "4.5", features = ["derive"] }
color-eyre = "0.6"
env_logger = "0.11"
error-chain = "0.12"
log = "0.4"
serde = "1.0"
serde_derive = "1.0"
Expand Down
23 changes: 9 additions & 14 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
// SPDX-FileCopyrightText: 2024 Luflosi <[email protected]>
// SPDX-License-Identifier: AGPL-3.0-only

mod errors {
error_chain! {}
}
use errors::{Error, Result, ResultExt};

use argon2::password_hash::PasswordHash;
use color_eyre::eyre::{eyre, Result, WrapErr};
use serde_derive::Deserialize;
use std::collections::HashMap;
use std::fs;
Expand Down Expand Up @@ -71,9 +67,10 @@ pub struct User<'a> {
impl Config<'_> {
pub fn read(filename: &Path) -> Result<Config<'static>> {
let contents = fs::read_to_string(filename)
.chain_err(|| format!("Cannot read config file `{}`", filename.display()))?;
.wrap_err_with(|| format!("Cannot read config file `{}`", filename.display()))?;
let config_parse_err_msg = || format!("Cannot parse config file `{}`", filename.display());
let raw_config: RawConfig = toml::from_str(&contents).chain_err(config_parse_err_msg)?;
let raw_config: RawConfig =
toml::from_str(&contents).wrap_err_with(config_parse_err_msg)?;
let users: Result<HashMap<_, _>> = raw_config
.users
.into_iter()
Expand All @@ -86,20 +83,18 @@ impl Config<'_> {
};
if props.ipv6prefixlen > 128 {
let prefixlen = props.ipv6prefixlen;
return Err(Error::from(format!(
"Prefix is longer than 128 bits: {prefixlen}"
))
.chain_err(ipv6prefixlen_parse_err_msg)
.chain_err(config_parse_err_msg));
return Err(eyre!("Prefix is longer than 128 bits: {prefixlen}"))
.wrap_err_with(ipv6prefixlen_parse_err_msg)
.wrap_err_with(config_parse_err_msg);
};
}
// TODO: figure out how to do this without leaking memory. I wish PasswordHash::new() took a String instead of &str
let raw_hash = Box::leak(Box::new(raw_user.hash));
let user = User {
// TODO: get rid of this piece of the code by somehow implementing deserialization for PasswordHash
hash: PasswordHash::new(raw_hash)
.chain_err(|| format!("Cannot parse password hash of user {username}"))
.chain_err(config_parse_err_msg)?,
.wrap_err_with(|| format!("Cannot parse password hash of user {username}"))
.wrap_err_with(config_parse_err_msg)?,
domains: raw_user.domains,
};
Ok((username, user))
Expand Down
26 changes: 8 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ mod config;
mod logging;
mod process;

#[macro_use]
extern crate error_chain;

use crate::config::Config;
use crate::process::{update, QueryParameters};
use clap::Parser;
use log::{error, info};
use color_eyre::eyre::Result;
use log::info;
use warp::Filter;

#[derive(Parser, Debug)]
Expand All @@ -23,24 +21,14 @@ struct Args {
}

#[tokio::main]
async fn main() {
async fn main() -> Result<()> {
color_eyre::install()?;

logging::setup();

let args = Args::parse();

let config = match Config::read(&args.config) {
Ok(v) => v,
Err(e) => {
error!("ERROR: {e}");

/////// look at the chain of errors... ///////
for e in e.iter().skip(1) {
error!("caused by: {e}");
}

std::process::exit(1);
}
};
let config = Config::read(&args.config)?;

let listen = config.listen;
let update = warp::get()
Expand All @@ -51,4 +39,6 @@ async fn main() {

info!("Listening on {listen}");
warp::serve(update).run(listen).await;

Ok(())
}

0 comments on commit 9bc416e

Please sign in to comment.