replace failure with thiserror and backtrace (#40)

This commit is contained in:
Tarik Eshaq 2020-05-20 10:04:37 -07:00 коммит произвёл GitHub
Родитель 82a569c573
Коммит 55f0bea806
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 46 добавлений и 35 удалений

Просмотреть файл

@ -10,8 +10,8 @@ keywords = ["http-ece", "web-push"]
[dependencies]
byteorder = "1.3"
failure = "0.1"
failure_derive = "0.1"
thiserror = "1.0"
backtrace = "0.3"
base64 = "0.12"
hkdf = { version = "0.7", optional = true }
lazy_static = { version = "1.2", optional = true }

Просмотреть файл

@ -3,13 +3,12 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use super::Cryptographer;
use failure::Fail;
use once_cell::sync::OnceCell;
static CRYPTOGRAPHER: OnceCell<&'static dyn Cryptographer> = OnceCell::new();
#[derive(Debug, Fail)]
#[fail(display = "Cryptographer already initialized")]
#[derive(Debug, thiserror::Error)]
#[error("Cryptographer already initialized")]
pub struct SetCryptographerError(());
/// Sets the global object that will be used for cryptographic operations.

Просмотреть файл

@ -2,30 +2,42 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use failure::{Backtrace, Context, Fail};
use backtrace::Backtrace;
use std::{boxed::Box, fmt, result};
struct Context<T: fmt::Display + Sync + 'static> {
context: T,
backtrace: Backtrace,
}
impl<T: fmt::Display + Send + Sync + 'static> Context<T> {
pub fn new(context: T) -> Self {
Context {
context,
backtrace: Backtrace::new(),
}
}
pub fn get_context(&self) -> &T {
&self.context
}
}
impl<D: fmt::Display + Send + Sync + 'static> fmt::Debug for Context<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}\n\n{}", self.backtrace, self.context)
}
}
pub type Result<T> = result::Result<T, Error>;
#[derive(Debug)]
pub struct Error(Box<Context<ErrorKind>>);
impl Fail for Error {
#[inline]
fn cause(&self) -> Option<&dyn Fail> {
self.0.cause()
}
#[inline]
fn backtrace(&self) -> Option<&Backtrace> {
self.0.backtrace()
}
}
impl fmt::Display for Error {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&*self.0, f)
fmt::Display::fmt(&*self.0.get_context(), f)
}
}
@ -50,50 +62,50 @@ impl From<Context<ErrorKind>> for Error {
}
}
#[derive(Debug, Fail)]
#[derive(Debug, thiserror::Error)]
pub enum ErrorKind {
#[fail(display = "Invalid auth secret")]
#[error("Invalid auth secret")]
InvalidAuthSecret,
#[fail(display = "Invalid salt")]
#[error("Invalid salt")]
InvalidSalt,
#[fail(display = "Invalid key length")]
#[error("Invalid key length")]
InvalidKeyLength,
#[fail(display = "Invalid record size")]
#[error("Invalid record size")]
InvalidRecordSize,
#[fail(display = "Invalid header size (too short)")]
#[error("Invalid header size (too short)")]
HeaderTooShort,
#[fail(display = "Truncated ciphertext")]
#[error("Truncated ciphertext")]
DecryptTruncated,
#[fail(display = "Zero-length ciphertext")]
#[error("Zero-length ciphertext")]
ZeroCiphertext,
#[fail(display = "Zero-length plaintext")]
#[error("Zero-length plaintext")]
ZeroPlaintext,
#[fail(display = "Block too short")]
#[error("Block too short")]
BlockTooShort,
#[fail(display = "Invalid decryption padding")]
#[error("Invalid decryption padding")]
DecryptPadding,
#[fail(display = "Invalid encryption padding")]
#[error("Invalid encryption padding")]
EncryptPadding,
#[fail(display = "Could not decode base64 entry")]
#[error("Could not decode base64 entry")]
DecodeError,
#[fail(display = "Crypto backend error")]
#[error("Crypto backend error")]
CryptoError,
#[cfg(feature = "backend-openssl")]
#[fail(display = "OpenSSL error: {}", _0)]
OpenSSLError(#[fail(cause)] openssl::error::ErrorStack),
#[error("OpenSSL error: {0}")]
OpenSSLError(#[source] openssl::error::ErrorStack),
}
impl From<base64::DecodeError> for Error {