From 88260c8e1f770a2e91d09880331262e1e5d2b1bc Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Fri, 22 Feb 2019 21:29:53 +0100 Subject: [PATCH] Break your chains and just failure. - Fixes #42. --- Cargo.toml | 2 +- src/database/fs.rs | 19 ++++++------------- src/database/mod.rs | 26 ++++++++------------------ src/database/poly.rs | 2 +- src/mail.rs | 4 ++-- src/main.rs | 23 ++++------------------- src/types.rs | 25 ++++++++++++------------- src/web/mod.rs | 16 ++++++++-------- src/web/upload.rs | 5 +++-- 9 files changed, 45 insertions(+), 77 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bb4b310..b7bec44 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,11 +5,11 @@ authors = ["Kai Michaelis "] build = "build.rs" [dependencies] +failure = "0.1.5" rocket = "0" rocket_codegen = "0" sequoia-openpgp = { git = "https://gitlab.com/sequoia-pgp/sequoia.git", rev = "e07bb25de0b2291088f88dc5360f3d04f702049c" } multipart = "0" -error-chain = "0" log = "0" rand = "0.5" serde = "1.0" diff --git a/src/database/fs.rs b/src/database/fs.rs index 08e15fc..78f99a0 100644 --- a/src/database/fs.rs +++ b/src/database/fs.rs @@ -44,29 +44,22 @@ impl Filesystem { match meta { Ok(meta) => { if !meta.file_type().is_dir() { - return Err(format!( + return Err(failure::format_err!( "'{}' exists already and is not a directory", - base.display() - ) - .into()); + base.display())); } if meta.permissions().readonly() { - return Err(format!( + return Err(failure::format_err!( "Cannot write '{}'", - base.display() - ) - .into()); + base.display())); } } Err(e) => { - return Err(format!( + return Err(failure::format_err!( "Cannot read '{}': {}", - base.display(), - e - ) - .into()); + base.display(), e)); } } } diff --git a/src/database/mod.rs b/src/database/mod.rs index 5c13a60..a22a5a0 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -59,16 +59,10 @@ impl Verify { use sequoia_openpgp::serialize::Serialize; let mut cur = Cursor::new(Vec::default()); - let res: Result<()> = uid - .serialize(&mut cur) - .map_err(|e| format!("sequoia_openpgp: {}", e).into()); - res?; + uid.serialize(&mut cur)?; for s in sig { - let res: Result<()> = s - .serialize(&mut cur) - .map_err(|e| format!("sequoia_openpgp: {}", e).into()); - res?; + s.serialize(&mut cur)?; } Ok(Verify { @@ -144,8 +138,7 @@ pub trait Database: Sync + Send { }) .collect::>(); - TPK::from_packet_pile(pile.into()) - .map_err(|e| format!("openpgp: {}", e).into()) + Ok(TPK::from_packet_pile(pile.into())?) } fn tpk_into_bytes(tpk: &TPK) -> Result> { @@ -155,7 +148,6 @@ pub trait Database: Sync + Send { let mut cur = Cursor::new(Vec::default()); tpk.serialize(&mut cur) .map(|_| cur.into_inner()) - .map_err(|e| format!("{}", e).into()) } fn link_subkeys( @@ -311,7 +303,7 @@ pub trait Database: Sync + Send { } } } - None => Err("No such token".into()), + None => Err(failure::err_msg("No such token")), } } @@ -326,7 +318,7 @@ pub trait Database: Sync + Send { Ok(tpk) => tpk, Err(e) => { return Err( - format!("Failed to parse TPK: {:?}", e).into() + failure::format_err!("Failed to parse TPK: {:?}", e) ); } }; @@ -340,7 +332,7 @@ pub trait Database: Sync + Send { Ok((tok, emails)) } - None => Err("Unknown key".into()), + None => Err(failure::err_msg("Unknown key")), } } @@ -365,11 +357,9 @@ pub trait Database: Sync + Send { let tpk = match TPK::from_bytes(&old) { Ok(tpk) => tpk, Err(e) => { - return Err(format!( + return Err(failure::format_err!( "Failed to parse old TPK: {:?}", - e - ) - .into()); + e)); } }; diff --git a/src/database/poly.rs b/src/database/poly.rs index 66cb644..272ad32 100644 --- a/src/database/poly.rs +++ b/src/database/poly.rs @@ -1,5 +1,5 @@ use database::{Database, Delete, Filesystem, Memory, Verify}; -use errors::Result; +use Result; use types::{Email, Fingerprint, KeyID}; pub enum Polymorphic { diff --git a/src/mail.rs b/src/mail.rs index f792cbc..5ea56b1 100644 --- a/src/mail.rs +++ b/src/mail.rs @@ -39,8 +39,8 @@ where .from(from) .subject(subject) .alternative( - html.ok_or("Email template failed to render")?, - txt.ok_or("Email template failed to render")?, + html.ok_or(failure::err_msg("Email template failed to render"))?, + txt.ok_or(failure::err_msg("Email template failed to render"))?, ) .build() .unwrap(); diff --git a/src/main.rs b/src/main.rs index 2b6c26f..a0895b8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,10 @@ #![recursion_limit = "1024"] #![feature(try_from)] +extern crate failure; +use failure::Error; +use failure::Fallible as Result; + extern crate serde; #[macro_use] extern crate serde_derive; @@ -18,8 +22,6 @@ extern crate rocket_contrib; extern crate sequoia_openpgp; #[macro_use] -extern crate error_chain; -#[macro_use] extern crate log; extern crate base64; extern crate handlebars; @@ -36,23 +38,6 @@ mod mail; mod types; mod web; -mod errors { - error_chain! { - foreign_links { - Fmt(::std::fmt::Error); - Io(::std::io::Error); - Json(::serde_json::Error); - Persist(::tempfile::PersistError); - RktConfig(::rocket::config::ConfigError); - StringUtf8Error(::std::string::FromUtf8Error); - StrUtf8Error(::std::str::Utf8Error); - HexError(::hex::FromHexError); - SendmailError(::lettre::sendmail::error::Error); - } - } -} -use errors::*; - use std::path::PathBuf; use structopt::StructOpt; diff --git a/src/types.rs b/src/types.rs index f0674ad..f7c68b2 100644 --- a/src/types.rs +++ b/src/types.rs @@ -48,9 +48,8 @@ impl TryFrom for Fingerprint { fn try_from(fpr: sequoia_openpgp::Fingerprint) -> Result { match fpr { sequoia_openpgp::Fingerprint::V4(a) => Ok(Fingerprint(a)), - sequoia_openpgp::Fingerprint::Invalid(_) => { - Err("invalid fingerprint".into()) - } + sequoia_openpgp::Fingerprint::Invalid(_) => + Err(failure::err_msg("invalid fingerprint")), } } } @@ -92,7 +91,7 @@ impl FromStr for Fingerprint { } if s.len() != 40 { - return Err(format!("'{}' is not a valid fingerprint", s).into()); + return Err(failure::format_err!("'{}' is not a valid fingerprint", s)); } let vec = hex::decode(s)?; @@ -102,7 +101,7 @@ impl FromStr for Fingerprint { arr.copy_from_slice(&vec[..]); Ok(Fingerprint(arr)) } else { - Err(format!("'{}' is not a valid fingerprint", s).into()) + Err(failure::format_err!("'{}' is not a valid fingerprint", s)) } } } @@ -117,7 +116,7 @@ impl TryFrom for KeyID { match fpr { sequoia_openpgp::Fingerprint::V4(a) => Ok(Fingerprint(a).into()), sequoia_openpgp::Fingerprint::Invalid(_) => { - Err("invalid fingerprint".into()) + Err(failure::err_msg("invalid fingerprint")) } } } @@ -147,7 +146,7 @@ impl FromStr for KeyID { } if s.len() != 16 { - return Err(format!("'{}' is not a valid long key ID", s).into()); + return Err(failure::format_err!("'{}' is not a valid long key ID", s)); } let vec = hex::decode(s)?; @@ -157,13 +156,13 @@ impl FromStr for KeyID { arr.copy_from_slice(&vec[..]); Ok(KeyID(arr)) } else { - Err(format!("'{}' is not a valid long key ID", s).into()) + Err(failure::format_err!("'{}' is not a valid long key ID", s)) } } } -impl From for String { - fn from(error: Error) -> Self { - format!("{:?}", error) - } -} +// impl From for String { +// fn from(error: Error) -> Self { +// format!("{:?}", error) +// } +// } diff --git a/src/web/mod.rs b/src/web/mod.rs index 67e697f..c742e13 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -10,13 +10,12 @@ use rocket_contrib::templates::Template; use serde::Serialize; use handlebars::Handlebars; -use std::error; use std::path::{Path, PathBuf}; mod upload; use database::{Database, Polymorphic}; -use errors::Result; +use Result; use types::{Email, Fingerprint, KeyID}; use Opt; @@ -66,9 +65,9 @@ impl MyResponse { MyResponse::Plain(s) } - pub fn ise(e: E) -> Self { + pub fn ise(e: failure::Error) -> Self { let ctx = templates::FiveHundred{ - error: format!("{:?}", e), + error: format!("{}", e), version: env!("VERGEN_SEMVER").to_string(), commit: env!("VERGEN_SHA_SHORT").to_string(), }; @@ -208,7 +207,7 @@ fn key_to_response<'a>(query: String, domain: String, bytes: &'a [u8]) -> MyResp let key = match TPK::from_bytes(bytes) { Ok(key) => key, - Err(err) => { return MyResponse::ise(err.compat()); } + Err(err) => { return MyResponse::ise(err); } }; let fpr = key.primary().fingerprint(); let armored_res = || -> Result { @@ -242,7 +241,7 @@ fn key_to_hkp_index<'a>(bytes: &'a [u8]) -> MyResponse { let tpk = match TPK::from_bytes(bytes) { Ok(tpk) => tpk, - Err(err) => { return MyResponse::ise(err.compat()); } + Err(err) => { return MyResponse::ise(err); } }; let mut out = String::default(); let p = tpk.primary(); @@ -550,11 +549,12 @@ pub fn serve(opt: &Opt, db: Polymorphic) -> Result<()> { opt.base .join("templates") .to_str() - .ok_or("Template path invalid")?, + .ok_or(failure::err_msg("Template path invalid"))?, ) .extra( "static_dir", - opt.base.join("public").to_str().ok_or("Static path invalid")?, + opt.base.join("public").to_str() + .ok_or(failure::err_msg("Static path invalid"))?, ) .extra("domain", opt.domain.clone()) .extra("from", opt.from.clone()) diff --git a/src/web/upload.rs b/src/web/upload.rs index fddf04f..fe12ee6 100644 --- a/src/web/upload.rs +++ b/src/web/upload.rs @@ -183,7 +183,8 @@ where use sequoia_openpgp::TPK; let tpk = TPK::from_reader(reader).map_err(|err| err.to_string())?; - let tokens = db.merge_or_publish(tpk)?; + let tokens = db.merge_or_publish(tpk) + .map_err(|e| format!("{}", e))?; let mut results: Vec = vec!(); for (email,token) in tokens { @@ -193,7 +194,7 @@ where mail_templates, domain, from, - )?; + ).map_err(|e| format!("{}", e))?; results.push(email.to_string()); }