diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2020-03-09 11:42:45 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2020-03-09 18:09:50 +0100 |
commit | 391a4b92c977cd64dfd131f3e29b0bc8d756d064 (patch) | |
tree | b5b96ff935853cef9ee22e01890c248a791e724e /openpgp/src | |
parent | 58d662c6d37dd1b0dccd4d0ce30290b8ede408e9 (diff) |
Switch from failure to anyhow.
- Use the anyhow crate instead of failure to implement the dynamic
side of our error handling. anyhow::Error derefs to dyn
std::error::Error, allowing better interoperability with other
stdlib-based error handling libraries.
- Fixes #444.
Diffstat (limited to 'openpgp/src')
-rw-r--r-- | openpgp/src/cert/key_amalgamation.rs | 10 | ||||
-rw-r--r-- | openpgp/src/cert/mod.rs | 6 | ||||
-rw-r--r-- | openpgp/src/cert/parser/low_level/mod.rs | 2 | ||||
-rw-r--r-- | openpgp/src/cert/parser/mod.rs | 4 | ||||
-rw-r--r-- | openpgp/src/crypto/aead.rs | 10 | ||||
-rw-r--r-- | openpgp/src/crypto/keygrip.rs | 2 | ||||
-rw-r--r-- | openpgp/src/crypto/sexp.rs | 4 | ||||
-rw-r--r-- | openpgp/src/fingerprint.rs | 2 | ||||
-rw-r--r-- | openpgp/src/keyhandle.rs | 4 | ||||
-rw-r--r-- | openpgp/src/keyid.rs | 2 | ||||
-rw-r--r-- | openpgp/src/lib.rs | 5 | ||||
-rw-r--r-- | openpgp/src/message/mod.rs | 8 | ||||
-rw-r--r-- | openpgp/src/packet/header/ctb.rs | 2 | ||||
-rw-r--r-- | openpgp/src/packet/key/mod.rs | 4 | ||||
-rw-r--r-- | openpgp/src/packet/one_pass_sig.rs | 2 | ||||
-rw-r--r-- | openpgp/src/packet/unknown.rs | 12 | ||||
-rw-r--r-- | openpgp/src/packet/userid/mod.rs | 6 | ||||
-rw-r--r-- | openpgp/src/packet_pile.rs | 2 | ||||
-rw-r--r-- | openpgp/src/parse/parse.rs | 11 | ||||
-rw-r--r-- | openpgp/src/parse/stream.rs | 54 | ||||
-rw-r--r-- | openpgp/src/policy.rs | 20 | ||||
-rw-r--r-- | openpgp/src/serialize/partial_body.rs | 5 | ||||
-rw-r--r-- | openpgp/src/types/timestamp.rs | 8 |
23 files changed, 72 insertions, 113 deletions
diff --git a/openpgp/src/cert/key_amalgamation.rs b/openpgp/src/cert/key_amalgamation.rs index 8fef9f5c..e008c9a4 100644 --- a/openpgp/src/cert/key_amalgamation.rs +++ b/openpgp/src/cert/key_amalgamation.rs @@ -38,7 +38,7 @@ use std::ops::Deref; use std::convert::TryFrom; use std::convert::TryInto; -use failure::ResultExt; +use anyhow::Context; use crate::{ Cert, @@ -313,7 +313,7 @@ impl<'a, P, P2> TryFrom<ErasedKeyAmalgamation<'a, P>> where P: 'a + key::KeyParts, P2: 'a + key::KeyParts, { - type Error = failure::Error; + type Error = anyhow::Error; fn try_from(ka: ErasedKeyAmalgamation<'a, P>) -> Result<Self> { if ka.primary { @@ -335,7 +335,7 @@ impl<'a, P, P2> TryFrom<ErasedKeyAmalgamation<'a, P>> where P: 'a + key::KeyParts, P2: 'a + key::KeyParts, { - type Error = failure::Error; + type Error = anyhow::Error; fn try_from(ka: ErasedKeyAmalgamation<'a, P>) -> Result<Self> { if ka.primary { @@ -544,7 +544,7 @@ impl<'a, P, P2> TryFrom<ValidErasedKeyAmalgamation<'a, P>> where P: 'a + key::KeyParts, P2: 'a + key::KeyParts, { - type Error = failure::Error; + type Error = anyhow::Error; fn try_from(vka: ValidErasedKeyAmalgamation<'a, P>) -> Result<Self> { Ok(ValidPrimaryKeyAmalgamation { @@ -561,7 +561,7 @@ impl<'a, P, P2> TryFrom<ValidErasedKeyAmalgamation<'a, P>> where P: 'a + key::KeyParts, P2: 'a + key::KeyParts, { - type Error = failure::Error; + type Error = anyhow::Error; fn try_from(vka: ValidErasedKeyAmalgamation<'a, P>) -> Result<Self> { Ok(ValidSubordinateKeyAmalgamation { diff --git a/openpgp/src/cert/mod.rs b/openpgp/src/cert/mod.rs index 8be42fb2..df555a10 100644 --- a/openpgp/src/cert/mod.rs +++ b/openpgp/src/cert/mod.rs @@ -9,7 +9,7 @@ use std::fmt; use std::ops::{Deref, DerefMut}; use std::time; -use failure::ResultExt; +use anyhow::Context; use crate::{ crypto::Signer, @@ -447,7 +447,7 @@ pub struct Cert { } // doc-hack, see above impl std::str::FromStr for Cert { - type Err = failure::Error; + type Err = anyhow::Error; fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { Self::from_bytes(s.as_bytes()) @@ -3229,7 +3229,7 @@ Pu1xwz57O4zo1VYf6TqHJzVC3OMvMUM2hhdecMUe5x6GorNaj6g= // Add a component that Sequoia doesn't understand. let mut fake_key = packet::Unknown::new( - packet::Tag::PublicSubkey, failure::err_msg("fake key")); + packet::Tag::PublicSubkey, anyhow::anyhow!("fake key")); fake_key.set_body("fake key".into()); let fake_binding = signature::Builder::new(SignatureType::SubkeyBinding) .set_issuer(primary_pair.public().keyid())? diff --git a/openpgp/src/cert/parser/low_level/mod.rs b/openpgp/src/cert/parser/low_level/mod.rs index 207c556e..3b1aa48c 100644 --- a/openpgp/src/cert/parser/low_level/mod.rs +++ b/openpgp/src/cert/parser/low_level/mod.rs @@ -70,7 +70,7 @@ pub enum CertParserError { OpenPGP(Error), } -impl From<CertParserError> for failure::Error { +impl From<CertParserError> for anyhow::Error { fn from(err: CertParserError) -> Self { match err { CertParserError::Parser(p) => p.into(), diff --git a/openpgp/src/cert/parser/mod.rs b/openpgp/src/cert/parser/mod.rs index 2666ccf7..a3a83c51 100644 --- a/openpgp/src/cert/parser/mod.rs +++ b/openpgp/src/cert/parser/mod.rs @@ -39,7 +39,7 @@ pub enum KeyringValidity { /// The packet sequence is a valid key ring prefix. KeyringPrefix, /// The packet sequence is definitely not a key ring. - Error(failure::Error), + Error(anyhow::Error), } impl KeyringValidity { @@ -246,7 +246,7 @@ pub enum CertValidity { /// The packet sequence is a valid Cert prefix. CertPrefix, /// The packet sequence is definitely not a Cert. - Error(failure::Error), + Error(anyhow::Error), } impl CertValidity { diff --git a/openpgp/src/crypto/aead.rs b/openpgp/src/crypto/aead.rs index ff8a309f..03d5c995 100644 --- a/openpgp/src/crypto/aead.rs +++ b/openpgp/src/crypto/aead.rs @@ -396,9 +396,8 @@ impl<'a> io::Read for Decryptor<'a> { Err(e) => match e.downcast::<io::Error>() { // An io::Error. Pass as-is. Ok(e) => Err(e), - // A failure. Create a compat object and wrap it. - Err(e) => Err(io::Error::new(io::ErrorKind::Other, - e.compat())), + // A failure. Wrap it. + Err(e) => Err(io::Error::new(io::ErrorKind::Other, e)), }, } } @@ -744,9 +743,8 @@ impl<W: io::Write> io::Write for Encryptor<W> { Err(e) => match e.downcast::<io::Error>() { // An io::Error. Pass as-is. Ok(e) => Err(e), - // A failure. Create a compat object and wrap it. - Err(e) => Err(io::Error::new(io::ErrorKind::Other, - e.compat())), + // A failure. Wrap it. + Err(e) => Err(io::Error::new(io::ErrorKind::Other, e)), }, } } diff --git a/openpgp/src/crypto/keygrip.rs b/openpgp/src/crypto/keygrip.rs index 81c21eff..e2dce111 100644 --- a/openpgp/src/crypto/keygrip.rs +++ b/openpgp/src/crypto/keygrip.rs @@ -30,7 +30,7 @@ impl fmt::Display for Keygrip { } impl std::str::FromStr for Keygrip { - type Err = failure::Error; + type Err = anyhow::Error; fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { Self::from_hex(s) diff --git a/openpgp/src/crypto/sexp.rs b/openpgp/src/crypto/sexp.rs index 86211dfb..a7da7f0c 100644 --- a/openpgp/src/crypto/sexp.rs +++ b/openpgp/src/crypto/sexp.rs @@ -101,7 +101,7 @@ impl Sexp { where R: crate::packet::key::KeyRole { use crate::crypto::mpis::PublicKey; - let not_a_session_key = || -> failure::Error { + let not_a_session_key = || -> anyhow::Error { Error::MalformedMPI( format!("Not a session key: {:?}", self)).into() }; @@ -190,7 +190,7 @@ impl Sexp { /// Such an expression is returned from gpg-agent's `PKSIGN` /// command. pub fn to_signature(&self) -> Result<mpis::Signature> { - let not_a_signature = || -> failure::Error { + let not_a_signature = || -> anyhow::Error { Error::MalformedMPI( format!("Not a signature: {:?}", self)).into() }; diff --git a/openpgp/src/fingerprint.rs b/openpgp/src/fingerprint.rs index a09a838d..aba44293 100644 --- a/openpgp/src/fingerprint.rs +++ b/openpgp/src/fingerprint.rs @@ -18,7 +18,7 @@ impl fmt::Debug for Fingerprint { } impl std::str::FromStr for Fingerprint { - type Err = failure::Error; + type Err = anyhow::Error; fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { Self::from_hex(s) diff --git a/openpgp/src/keyhandle.rs b/openpgp/src/keyhandle.rs index 2dea1e35..22c654d1 100644 --- a/openpgp/src/keyhandle.rs +++ b/openpgp/src/keyhandle.rs @@ -73,7 +73,7 @@ impl From<&Fingerprint> for KeyHandle { } impl TryFrom<KeyHandle> for Fingerprint { - type Error = failure::Error; + type Error = anyhow::Error; fn try_from(i: KeyHandle) -> Result<Self> { match i { KeyHandle::Fingerprint(i) => Ok(i), @@ -84,7 +84,7 @@ impl TryFrom<KeyHandle> for Fingerprint { } impl TryFrom<&KeyHandle> for Fingerprint { - type Error = failure::Error; + type Error = anyhow::Error; fn try_from(i: &KeyHandle) -> Result<Self> { match i { KeyHandle::Fingerprint(i) => Ok(i.clone()), diff --git a/openpgp/src/keyid.rs b/openpgp/src/keyid.rs index 2a9f121d..1224a70c 100644 --- a/openpgp/src/keyid.rs +++ b/openpgp/src/keyid.rs @@ -21,7 +21,7 @@ impl fmt::Debug for KeyID { } impl std::str::FromStr for KeyID { - type Err = failure::Error; + type Err = anyhow::Error; fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { Self::from_hex(s) diff --git a/openpgp/src/lib.rs b/openpgp/src/lib.rs index 792d91db..ca5de2cd 100644 --- a/openpgp/src/lib.rs +++ b/openpgp/src/lib.rs @@ -46,9 +46,6 @@ extern crate lalrpop_util; -#[macro_use] -extern crate failure; - extern crate buffered_reader; extern crate memsec; @@ -186,7 +183,7 @@ fn frozen_time() -> std::time::SystemTime { } /// Crate result specialization. -pub type Result<T> = ::std::result::Result<T, failure::Error>; +pub type Result<T> = ::std::result::Result<T, anyhow::Error>; #[derive(thiserror::Error, Debug, Clone)] /// Errors returned by this module. diff --git a/openpgp/src/message/mod.rs b/openpgp/src/message/mod.rs index 5e1c2fba..7ec6d0ef 100644 --- a/openpgp/src/message/mod.rs +++ b/openpgp/src/message/mod.rs @@ -14,8 +14,6 @@ use std::fmt; use std::io; use std::path::Path; -use failure; - use crate::Result; use crate::Error; use crate::Packet; @@ -51,7 +49,7 @@ pub enum MessageParserError { #[doc(hidden)] __Nonexhaustive, } -impl From<MessageParserError> for failure::Error { +impl From<MessageParserError> for anyhow::Error { fn from(err: MessageParserError) -> Self { match err { MessageParserError::Parser(p) => p.into(), @@ -72,7 +70,7 @@ pub enum MessageValidity { /// prefix of an OpenPGP message. MessagePrefix, /// The message is definitely not valid. - Error(failure::Error), + Error(anyhow::Error), } impl MessageValidity { @@ -347,7 +345,7 @@ impl<'a> Parse<'a, Message> for Message { } impl std::str::FromStr for Message { - type Err = failure::Error; + type Err = anyhow::Error; fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { Self::from_bytes(s.as_bytes()) diff --git a/openpgp/src/packet/header/ctb.rs b/openpgp/src/packet/header/ctb.rs index 3f48cdf5..eabfce0e 100644 --- a/openpgp/src/packet/header/ctb.rs +++ b/openpgp/src/packet/header/ctb.rs @@ -75,7 +75,7 @@ pub enum PacketLengthType { } impl TryFrom<u8> for PacketLengthType { - type Error = failure::Error; + type Error = anyhow::Error; fn try_from(u: u8) -> Result<Self> { match u { diff --git a/openpgp/src/packet/key/mod.rs b/openpgp/src/packet/key/mod.rs index 1fbca529..a5d60fdf 100644 --- a/openpgp/src/packet/key/mod.rs +++ b/openpgp/src/packet/key/mod.rs @@ -486,7 +486,7 @@ macro_rules! create_part_conversions { impl<$($l, )* $($g, )* > TryFrom<$Key<$($l, )* $from_parts, $($g, )* >> for $Key<$($l, )* $to_parts, $($g, )* > where $($w: $c ),* { - type Error = failure::Error; + type Error = anyhow::Error; fn try_from(p: $Key<$($l, )* $from_parts, $($g, )* >) -> Result<Self> { p.mark_parts_secret() } @@ -495,7 +495,7 @@ macro_rules! create_part_conversions { impl<$($l, )* $($g, )* > TryFrom<&$($l)* $Key<$($l, )* $from_parts, $($g, )* >> for &$($l)* $Key<$($l, )* $to_parts, $($g, )* > where $($w: $c ),* { - type Error = failure::Error; + type Error = anyhow::Error; fn try_from(p: &$($l)* $Key<$($l, )* $from_parts, $($g, )* >) -> Result<Self> { if p.has_secret() { Ok(convert_ref!(p)) diff --git a/openpgp/src/packet/one_pass_sig.rs b/openpgp/src/packet/one_pass_sig.rs index 0fc72286..f0f2cfa0 100644 --- a/openpgp/src/packet/one_pass_sig.rs +++ b/openpgp/src/packet/one_pass_sig.rs @@ -161,7 +161,7 @@ impl From<OnePassSig3> for Packet { } impl<'a> std::convert::TryFrom<&'a Signature> for OnePassSig3 { - type Error = failure::Error; + type Error = anyhow::Error; fn try_from(s: &'a Signature) -> Result<Self> { let issuer = match s.issuer() { diff --git a/openpgp/src/packet/unknown.rs b/openpgp/src/packet/unknown.rs index 0b5ddf61..d407200d 100644 --- a/openpgp/src/packet/unknown.rs +++ b/openpgp/src/packet/unknown.rs @@ -1,8 +1,6 @@ use std::hash::{Hash, Hasher}; use std::cmp::Ordering; -use failure; - use crate::packet::Tag; use crate::packet; use crate::Packet; @@ -20,7 +18,7 @@ pub struct Unknown { /// Packet tag. tag: Tag, /// Error that caused parsing or processing to abort. - error: failure::Error, + error: anyhow::Error, /// The unknown data packet is a container packet, but cannot /// store packets. /// @@ -50,7 +48,7 @@ impl Clone for Unknown { Unknown { common: self.common.clone(), tag: self.tag, - error: failure::err_msg(format!("{}", self.error)), + error: anyhow::anyhow!("{}", self.error), container: self.container.clone(), } } @@ -59,7 +57,7 @@ impl Clone for Unknown { impl Unknown { /// Returns a new `Unknown` packet. - pub fn new(tag: Tag, error: failure::Error) -> Self { + pub fn new(tag: Tag, error: anyhow::Error) -> Self { Unknown { common: Default::default(), tag: tag, @@ -81,14 +79,14 @@ impl Unknown { /// Gets the unknown packet's error. /// /// This is the error that caused parsing or processing to abort. - pub fn error(&self) -> &failure::Error { + pub fn error(&self) -> &anyhow::Error { &self.error } /// Sets the unknown packet's error. /// /// This is the error that caused parsing or processing to abort. - pub fn set_error(&mut self, error: failure::Error) -> failure::Error { + pub fn set_error(&mut self, error: anyhow::Error) -> anyhow::Error { ::std::mem::replace(&mut self.error, error) } diff --git a/openpgp/src/packet/userid/mod.rs b/openpgp/src/packet/userid/mod.rs index abc04b9e..a0a4ab4e 100644 --- a/openpgp/src/packet/userid/mod.rs +++ b/openpgp/src/packet/userid/mod.rs @@ -6,7 +6,7 @@ use std::cmp::Ordering; use std::sync::Mutex; use quickcheck::{Arbitrary, Gen}; -use failure::ResultExt; +use anyhow::Context; use regex::Regex; use crate::Result; @@ -717,7 +717,7 @@ impl UserID { Ok(puid) => puid, Err(err) => { // Return the error from the NameAddrOrOther parser. - let err : failure::Error = err.into(); + let err : anyhow::Error = err.into(); return Err(err).context(format!( "Failed to parse User ID: {:?}", s))?; } @@ -796,7 +796,7 @@ impl UserID { // Normalize Unicode in domains. let domain = idna::domain_to_ascii(domain) - .map_err(|e| failure::format_err!( + .map_err(|e| anyhow::anyhow!( "punycode conversion failed: {:?}", e))?; // Join. diff --git a/openpgp/src/packet_pile.rs b/openpgp/src/packet_pile.rs index f3b3d9c9..e51c78de 100644 --- a/openpgp/src/packet_pile.rs +++ b/openpgp/src/packet_pile.rs @@ -65,7 +65,7 @@ impl<'a> Parse<'a, PacketPile> for PacketPile { } impl std::str::FromStr for PacketPile { - type Err = failure::Error; + type Err = anyhow::Error; fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { Self::from_bytes(s.as_bytes()) diff --git a/openpgp/src/parse/parse.rs b/openpgp/src/parse/parse.rs index 7d899fcc..a6824bc1 100644 --- a/openpgp/src/parse/parse.rs +++ b/openpgp/src/parse/parse.rs @@ -6,7 +6,6 @@ use std::str; use std::mem; use std::fmt; use std::path::Path; -use failure; use ::buffered_reader::*; @@ -343,7 +342,7 @@ impl<'a, T: 'a + BufferedReader<Cookie>> PacketHeaderParser<T> { self.error(Error::MalformedPacket(reason.into()).into()) } - fn error(mut self, error: failure::Error) -> Result<PacketParser<'a>> { + fn error(mut self, error: anyhow::Error) -> Result<PacketParser<'a>> { // Rewind the dup reader, so that the caller has a chance to // buffer the whole body of the unknown packet. self.reader.rewind(); @@ -941,7 +940,7 @@ fn body_length_old_format() { impl Unknown { /// Parses the body of any packet and returns an Unknown. - fn parse<'a, T: 'a + BufferedReader<Cookie>>(php: PacketHeaderParser<T>, error: failure::Error) + fn parse<'a, T: 'a + BufferedReader<Cookie>>(php: PacketHeaderParser<T>, error: anyhow::Error) -> Result<PacketParser<'a>> { let tag = php.header.ctb().tag(); @@ -979,7 +978,7 @@ pub(crate) fn to_unknown_packet<R: Read>(reader: R) -> Result<Unknown> reader, PacketParserState::new(Default::default()), vec![ 0 ], header, Vec::new()); let mut pp = Unknown::parse(parser, - failure::err_msg("explicit conversion to unknown"))?; + anyhow::anyhow!("explicit conversion to unknown"))?; pp.buffer_unread_content()?; pp.finish()?; @@ -2263,7 +2262,7 @@ impl SKESK { // digest. We don't know the size of the former, but // we know the size of the latter. let mut esk = php_try!(php.reader.steal_eof() - .map_err(|e| failure::Error::from(e))); + .map_err(|e| anyhow::Error::from(e))); let l = esk.len(); let aead_digest = esk.split_off(l - digest_size); // Now fix the map. @@ -3177,7 +3176,7 @@ impl <'a> PacketParser<'a> { // Read the header. let mut skip = 0; - let mut orig_error : Option<failure::Error> = None; + let mut orig_error : Option<anyhow::Error> = None; loop { bio.rewind(); bio.data_consume_hard(skip)?; diff --git a/openpgp/src/parse/stream.rs b/openpgp/src/parse/stream.rs index af64d709..3f3e61b6 100644 --- a/openpgp/src/parse/stream.rs +++ b/openpgp/src/parse/stream.rs @@ -108,7 +108,7 @@ pub enum VerificationError<'a> { sig: &'a Signature, /// The reason why the signature is malformed. - error: failure::Error, + error: anyhow::Error, }, /// Missing Key MissingKey { @@ -127,7 +127,7 @@ pub enum VerificationError<'a> { cert: &'a Cert, /// The reason why the key is bad. - error: failure::Error, + error: anyhow::Error, }, /// Bad key (have a key, but it is not alive, etc.) BadKey { @@ -138,7 +138,7 @@ pub enum VerificationError<'a> { ka: ValidErasedKeyAmalgamation<'a, key::PublicParts>, /// The reason why the key is bad. - error: failure::Error, + error: anyhow::Error, }, /// Bad signature (have a valid key, but the signature didn't check out) BadSignature { @@ -149,7 +149,7 @@ pub enum VerificationError<'a> { ka: ValidErasedKeyAmalgamation<'a, key::PublicParts>, /// The reason why the signature is bad. - error: failure::Error, + error: anyhow::Error, }, } @@ -503,7 +503,6 @@ impl<V: VerificationHelper> DecryptionHelper for NoDecryptionHelper<V> { /// /// ``` /// extern crate sequoia_openpgp as openpgp; |