Use Failure in more places

This commit is contained in:
asonix 2019-09-12 20:29:24 -05:00
parent 3a502055bf
commit c468513d5a
3 changed files with 21 additions and 64 deletions

View file

@ -1,5 +1,6 @@
use actix::System; use actix::System;
use actix_web::client::Client; use actix_web::client::Client;
use failure::Fail;
use futures::future::{lazy, Future}; use futures::future::{lazy, Future};
use http_signature_normalization_actix::prelude::*; use http_signature_normalization_actix::prelude::*;
use sha2::{Digest, Sha256}; use sha2::{Digest, Sha256};
@ -31,10 +32,13 @@ fn main() {
.unwrap(); .unwrap();
} }
#[derive(Debug)] #[derive(Debug, Fail)]
pub enum MyError { pub enum MyError {
Convert(ToStrError), #[fail(display = "Failed to read header, {}", _0)]
Header(InvalidHeaderValue), Convert(#[cause] ToStrError),
#[fail(display = "Failed to create header, {}", _0)]
Header(#[cause] InvalidHeaderValue),
} }
impl From<ToStrError> for MyError { impl From<ToStrError> for MyError {

View file

@ -1,8 +1,8 @@
use actix::System; use actix::System;
use actix_web::{web, App, HttpRequest, HttpServer, ResponseError}; use actix_web::{web, App, HttpRequest, HttpServer, ResponseError};
use failure::Fail;
use http_signature_normalization_actix::{prelude::*, verify::Algorithm}; use http_signature_normalization_actix::{prelude::*, verify::Algorithm};
use sha2::{Digest, Sha256}; use sha2::{Digest, Sha256};
use std::fmt;
fn index((req, config): (HttpRequest, web::Data<Config>)) -> Result<&'static str, MyError> { fn index((req, config): (HttpRequest, web::Data<Config>)) -> Result<&'static str, MyError> {
let unverified = req.begin_verify(&config)?; let unverified = req.begin_verify(&config)?;
@ -37,37 +37,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(()) Ok(())
} }
#[derive(Debug)] #[derive(Debug, Fail)]
enum MyError { enum MyError {
Verify(VerifyError), #[fail(display = "Failed to verify, {}", _0)]
Verify(#[cause] VerifyError),
#[fail(display = "Unsupported algorithm")]
Algorithm, Algorithm,
} }
impl fmt::Display for MyError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
MyError::Verify(ref e) => write!(f, "Verify Error, {}", e),
MyError::Algorithm => write!(f, "Unsupported algorithm"),
}
}
}
impl std::error::Error for MyError {
fn description(&self) -> &str {
match *self {
MyError::Verify(ref e) => e.description(),
MyError::Algorithm => "Unsupported algorithm",
}
}
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match *self {
MyError::Verify(ref e) => Some(e),
MyError::Algorithm => None,
}
}
}
impl ResponseError for MyError { impl ResponseError for MyError {
// default 500 // default 500
} }

View file

@ -3,11 +3,8 @@ use actix_web::http::{
uri::PathAndQuery, uri::PathAndQuery,
Method, Method,
}; };
use std::{ use failure::Fail;
collections::BTreeMap, use std::{collections::BTreeMap, fmt::Display};
error::Error,
fmt::{self, Display},
};
mod sign; mod sign;
@ -55,10 +52,13 @@ pub struct Config {
pub config: http_signature_normalization::Config, pub config: http_signature_normalization::Config,
} }
#[derive(Debug)] #[derive(Debug, Fail)]
pub enum VerifyError { pub enum VerifyError {
Sig(http_signature_normalization::VerifyError), #[fail(display = "Signature error, {}", _0)]
Header(ToStrError), Sig(#[cause] http_signature_normalization::VerifyError),
#[fail(display = "Failed to read header, {}", _0)]
Header(#[cause] ToStrError),
} }
impl Config { impl Config {
@ -107,31 +107,6 @@ impl Config {
} }
} }
impl fmt::Display for VerifyError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
VerifyError::Sig(ref e) => write!(f, "Sig error, {}", e),
VerifyError::Header(ref e) => write!(f, "Header error, {}", e),
}
}
}
impl Error for VerifyError {
fn description(&self) -> &str {
match *self {
VerifyError::Sig(ref e) => e.description(),
VerifyError::Header(ref e) => e.description(),
}
}
fn source(&self) -> Option<&(dyn Error + 'static)> {
match *self {
VerifyError::Sig(ref e) => Some(e),
VerifyError::Header(ref e) => Some(e),
}
}
}
impl From<http_signature_normalization::VerifyError> for VerifyError { impl From<http_signature_normalization::VerifyError> for VerifyError {
fn from(e: http_signature_normalization::VerifyError) -> Self { fn from(e: http_signature_normalization::VerifyError) -> Self {
VerifyError::Sig(e) VerifyError::Sig(e)