#[derive(Debug, Clone)]
pub struct ErrorBuilder {
kind: crate::error::ErrorKind,
code: Option<u16>,
name: String,
message: Option<String>,
details: std::collections::BTreeMap<String, serde_value::Value>,
}
impl ErrorBuilder {
pub fn new(kind: crate::error::ErrorKind, name: &str) -> Self {
Self {
kind,
name: name.to_string(),
code: None,
message: None,
details: std::collections::BTreeMap::new(),
}
}
pub fn with_code(mut self, code: u16) -> Self {
self.code = Some(code);
self
}
pub fn with_message(mut self, message: String) -> Self {
self.message = Some(message);
self
}
pub fn with_details(mut self, details: std::collections::BTreeMap<String, serde_value::Value>) -> Self {
self.details = details;
self
}
pub fn build(self) -> crate::error::Error {
crate::error::Error::new(
self.code.unwrap_or(self.kind.code()),
format!("{}::{}::{}", self.kind.side(), self.kind.name(), self.name),
self.message.unwrap_or(self.kind.description().to_string()),
self.details,
)
}
}
impl Default for ErrorBuilder {
fn default() -> Self {
ErrorBuilder::new(
crate::error::ErrorKind("InternalServerError", 500, "Internal Server Error"),
"UnknownError".into(),
)
}
}