Break your chains and just failure.

- Fixes #42.
This commit is contained in:
Justus Winter 2019-02-22 21:29:53 +01:00
parent 310f95ca5e
commit 88260c8e1f
No known key found for this signature in database
GPG Key ID: 686F55B4AB2B3386
9 changed files with 45 additions and 77 deletions

View File

@ -5,11 +5,11 @@ authors = ["Kai Michaelis <kai@sequoia-pgp.org>"]
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"

View File

@ -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));
}
}
}

View File

@ -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::<Vec<_>>();
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<Vec<u8>> {
@ -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));
}
};

View File

@ -1,5 +1,5 @@
use database::{Database, Delete, Filesystem, Memory, Verify};
use errors::Result;
use Result;
use types::{Email, Fingerprint, KeyID};
pub enum Polymorphic {

View File

@ -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();

View File

@ -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;

View File

@ -48,9 +48,8 @@ impl TryFrom<sequoia_openpgp::Fingerprint> for Fingerprint {
fn try_from(fpr: sequoia_openpgp::Fingerprint) -> Result<Self> {
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<sequoia_openpgp::Fingerprint> 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<Error> for String {
fn from(error: Error) -> Self {
format!("{:?}", error)
}
}
// impl From<Error> for String {
// fn from(error: Error) -> Self {
// format!("{:?}", error)
// }
// }

View File

@ -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: error::Error>(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<String> {
@ -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())

View File

@ -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<String> = 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());
}