Factor usage of common error type more

This commit is contained in:
Dominik Werder
2021-12-08 13:20:07 +01:00
parent c39af81097
commit 3c64eafd14
56 changed files with 751 additions and 354 deletions

View File

@@ -1,21 +1,21 @@
[package]
name = "err"
version = "0.0.1-a.0"
version = "0.0.2"
authors = ["Dominik Werder <dominik.werder@gmail.com>"]
edition = "2018"
edition = "2021"
[dependencies]
hyper = { version = "0.14", features = ["http1", "http2", "client", "server", "tcp"] }
http = "0.2"
tokio = { version = "1.7.1", features = ["rt-multi-thread", "io-util", "net", "time", "sync", "fs"] }
#hyper = { version = "0.14", features = ["http1", "http2", "client", "server", "tcp"] }
#http = "0.2"
backtrace = "0.3.56"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
bincode = "1.3.3"
serde_cbor = "0.11.1"
async-channel = "1.6"
chrono = { version = "0.4.19", features = ["serde"] }
nom = "6.1.2"
tokio-postgres = { version = "0.7", features = ["runtime", "with-chrono-0_4", "with-serde_json-1"] }
serde_cbor = "0.11.1"
regex = "1.5.4"
url = "2.2"
regex = "1.5.4"
bincode = "1.3.3"
#tokio = { version = "1.14.77", features = ["rt-multi-thread", "io-util", "net", "time", "sync", "fs"] }
#tokio-postgres = { version = "0.7", features = ["runtime", "with-chrono-0_4", "with-serde_json-1"] }
#nom = "6.1.2"

View File

@@ -2,9 +2,6 @@
Error handling and reporting.
*/
use http::header::InvalidHeaderValue;
use http::uri::InvalidUri;
use nom::error::ErrorKind;
use serde::{Deserialize, Serialize};
use std::array::TryFromSliceError;
use std::fmt;
@@ -12,8 +9,6 @@ use std::net::AddrParseError;
use std::num::{ParseFloatError, ParseIntError};
use std::string::FromUtf8Error;
use std::sync::PoisonError;
use tokio::task::JoinError;
use tokio::time::error::Elapsed;
#[derive(Clone, Serialize, Deserialize)]
pub enum Reason {
@@ -56,6 +51,13 @@ impl Error {
}
}
pub fn from_string<E>(e: E) -> Self
where
E: ToString,
{
Self::with_msg(e.to_string())
}
pub fn mark_bad_request(mut self) -> Self {
self.reason = Some(Reason::BadRequest);
self
@@ -143,6 +145,38 @@ impl fmt::Display for Error {
impl std::error::Error for Error {}
pub trait ErrConv<T> {
fn err_conv(self) -> Result<T, Error>;
}
impl<T, E> ErrConv<T> for Result<T, E>
where
E: Into<Error>,
{
fn err_conv(self) -> Result<T, Error> {
match self {
Ok(k) => Ok(k),
Err(e) => Err(e.into()),
}
}
}
pub trait ErrStr<T> {
fn errstr(self) -> Result<T, Error>;
}
impl<T, E> ErrStr<T> for Result<T, E>
where
E: ToString,
{
fn errstr(self) -> Result<T, Error> {
match self {
Ok(k) => Ok(k),
Err(e) => Err(Error::with_msg_no_trace(e.to_string())),
}
}
}
impl From<String> for Error {
fn from(k: String) -> Self {
Self::with_msg(k)
@@ -167,18 +201,6 @@ impl From<AddrParseError> for Error {
}
}
impl From<http::Error> for Error {
fn from(k: http::Error) -> Self {
Self::with_msg(k.to_string())
}
}
impl From<hyper::Error> for Error {
fn from(k: hyper::Error) -> Self {
Self::with_msg(k.to_string())
}
}
impl From<serde_json::Error> for Error {
fn from(k: serde_json::Error) -> Self {
Self::with_msg(k.to_string())
@@ -215,6 +237,7 @@ impl From<FromUtf8Error> for Error {
}
}
/*
impl<T: fmt::Debug> From<nom::Err<T>> for Error {
fn from(k: nom::Err<T>) -> Self {
Self::with_msg(format!("nom::Err<T> {:?}", k))
@@ -222,20 +245,23 @@ impl<T: fmt::Debug> From<nom::Err<T>> for Error {
}
impl<I> nom::error::ParseError<I> for Error {
fn from_error_kind(_input: I, kind: ErrorKind) -> Self {
fn from_error_kind(_input: I, kind: nom::error::ErrorKind) -> Self {
Self::with_msg(format!("ParseError {:?}", kind))
}
fn append(_input: I, kind: ErrorKind, other: Self) -> Self {
fn append(_input: I, kind: nom::error::ErrorKind, other: Self) -> Self {
Self::with_msg(format!("ParseError kind {:?} other {:?}", kind, other))
}
}
*/
/*
impl From<JoinError> for Error {
fn from(k: JoinError) -> Self {
Self::with_msg(format!("JoinError {:?}", k))
}
}
*/
impl From<Box<bincode::ErrorKind>> for Error {
fn from(k: Box<bincode::ErrorKind>) -> Self {
@@ -243,24 +269,6 @@ impl From<Box<bincode::ErrorKind>> for Error {
}
}
impl From<tokio_postgres::Error> for Error {
fn from(k: tokio_postgres::Error) -> Self {
Self::with_msg(k.to_string())
}
}
impl<T> From<async_channel::SendError<T>> for Error {
fn from(k: async_channel::SendError<T>) -> Self {
Self::with_msg(k.to_string())
}
}
impl From<InvalidUri> for Error {
fn from(k: InvalidUri) -> Self {
Self::with_msg(k.to_string())
}
}
impl From<serde_cbor::Error> for Error {
fn from(k: serde_cbor::Error) -> Self {
Self::with_msg(k.to_string())
@@ -285,18 +293,6 @@ impl<T> From<PoisonError<T>> for Error {
}
}
impl From<InvalidHeaderValue> for Error {
fn from(k: InvalidHeaderValue) -> Self {
Self::with_msg(format!("{:?}", k))
}
}
impl From<Elapsed> for Error {
fn from(k: Elapsed) -> Self {
Self::with_msg(format!("{:?}", k))
}
}
impl From<url::ParseError> for Error {
fn from(k: url::ParseError) -> Self {
Self::with_msg(format!("{:?}", k))