Factor out channel config handler

This commit is contained in:
Dominik Werder
2022-02-28 13:55:34 +01:00
parent 36ecc858fd
commit f82989f5c9
14 changed files with 248 additions and 83 deletions

View File

@@ -1,16 +1,16 @@
[package]
name = "err"
version = "0.0.2"
version = "0.0.3"
authors = ["Dominik Werder <dominik.werder@gmail.com>"]
edition = "2021"
[dependencies]
backtrace = "0.3.56"
backtrace = "0.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_cbor = "0.11.1"
serde_cbor = "0.11"
async-channel = "1.6"
chrono = { version = "0.4.19", features = ["serde"] }
chrono = { version = "0.4", features = ["serde"] }
url = "2.2"
regex = "1.5.4"
bincode = "1.3.3"
regex = "1.5"
bincode = "1.3"

View File

@@ -10,7 +10,7 @@ use std::num::{ParseFloatError, ParseIntError};
use std::string::FromUtf8Error;
use std::sync::PoisonError;
#[derive(Clone, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum Reason {
InternalError,
BadRequest,
@@ -77,6 +77,11 @@ impl Error {
self
}
pub fn mark_io_error(mut self) -> Self {
self.reason = Some(Reason::IoError);
self
}
pub fn add_public_msg(mut self, msg: impl Into<String>) -> Self {
if self.public_msg.is_none() {
self.public_msg = Some(vec![]);
@@ -96,6 +101,16 @@ impl Error {
pub fn reason(&self) -> Option<Reason> {
self.reason.clone()
}
pub fn to_public_error(&self) -> PublicError {
PublicError {
reason: self.reason(),
msg: self
.public_msg()
.map(|k| k.join("\n"))
.unwrap_or("No error message".into()),
}
}
}
fn fmt_backtrace(trace: &backtrace::Backtrace) -> String {
@@ -195,6 +210,18 @@ where
}
}
impl From<PublicError> for Error {
fn from(k: PublicError) -> Self {
Self {
msg: String::new(),
trace: None,
trace_str: None,
public_msg: Some(vec![k.msg().into()]),
reason: k.reason(),
}
}
}
impl From<String> for Error {
fn from(k: String) -> Self {
Self::with_msg(k)
@@ -323,6 +350,22 @@ impl From<TryFromSliceError> for Error {
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PublicError {
reason: Option<Reason>,
msg: String,
}
impl PublicError {
pub fn reason(&self) -> Option<Reason> {
self.reason.clone()
}
pub fn msg(&self) -> &str {
&self.msg
}
}
pub fn todo() {
todo!("TODO");
}