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

@@ -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");
}