use std::fmt;
use crate::protocol::AppendErrorDetails;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorKind {
Unexpected,
ConfigInvalid,
AppendRowsFailed,
}
impl ErrorKind {
pub fn into_static(self) -> &'static str {
self.into()
}
}
impl fmt::Display for ErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.into_static())
}
}
impl From<ErrorKind> for &'static str {
fn from(v: ErrorKind) -> &'static str {
match v {
ErrorKind::Unexpected => "Unexpected",
ErrorKind::ConfigInvalid => "ConfigInvalid",
ErrorKind::AppendRowsFailed => "AppendRowsFailed",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum ErrorStatus {
Permanent,
Temporary,
Persistent,
}
impl fmt::Display for ErrorStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ErrorStatus::Permanent => write!(f, "permanent"),
ErrorStatus::Temporary => write!(f, "temporary"),
ErrorStatus::Persistent => write!(f, "persistent"),
}
}
}
pub struct Error {
kind: ErrorKind,
message: String,
status: ErrorStatus,
context: Vec<(&'static str, String)>,
append_details: Option<AppendErrorDetails>,
source: Option<anyhow::Error>,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} ({})", self.kind, self.status)?;
if !self.context.is_empty() {
write!(f, ", context: {{ ")?;
write!(
f,
"{}",
self.context
.iter()
.map(|(k, v)| format!("{k}: {v}"))
.collect::<Vec<_>>()
.join(", ")
)?;
write!(f, " }}")?;
}
if !self.message.is_empty() {
write!(f, " => {}", self.message)?;
}
if let Some(source) = &self.source {
write!(f, ", source: {source}")?;
}
Ok(())
}
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if f.alternate() {
let mut de = f.debug_struct("Error");
de.field("kind", &self.kind);
de.field("message", &self.message);
de.field("status", &self.status);
de.field("context", &self.context);
de.field("append_details", &self.append_details);
de.field("source", &self.source);
return de.finish();
}
write!(f, "{} ({})", self.kind, self.status)?;
if !self.message.is_empty() {
write!(f, " => {}", self.message)?;
}
writeln!(f)?;
if !self.context.is_empty() {
writeln!(f)?;
writeln!(f, "Context:")?;
for (k, v) in self.context.iter() {
writeln!(f, " {k}: {v}")?;
}
}
if let Some(source) = &self.source {
writeln!(f)?;
writeln!(f, "Source:")?;
writeln!(f, " {source:#}")?;
}
Ok(())
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.source.as_ref().map(|v| v.as_ref())
}
}
impl Error {
pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
Self {
kind,
message: message.into(),
status: ErrorStatus::Permanent,
context: Vec::default(),
append_details: None,
source: None,
}
}
pub fn with_context(mut self, key: &'static str, value: impl ToString) -> Self {
self.context.push((key, value.to_string()));
self
}
pub fn set_source(mut self, src: impl Into<anyhow::Error>) -> Self {
debug_assert!(self.source.is_none(), "the source error has been set");
self.source = Some(src.into());
self
}
pub fn append_details(&self) -> Option<&AppendErrorDetails> {
self.append_details.as_ref()
}
pub(crate) fn set_append_details(mut self, details: AppendErrorDetails) -> Self {
self.append_details = Some(details);
self
}
pub fn set_permanent(mut self) -> Self {
self.status = ErrorStatus::Permanent;
self
}
pub fn set_temporary(mut self) -> Self {
self.status = ErrorStatus::Temporary;
self
}
pub fn set_persistent(mut self) -> Self {
self.status = ErrorStatus::Persistent;
self
}
pub fn kind(&self) -> ErrorKind {
self.kind
}
pub fn message(&self) -> &str {
&self.message
}
pub fn is_temporary(&self) -> bool {
self.status == ErrorStatus::Temporary
}
pub fn is_permanent(&self) -> bool {
self.status == ErrorStatus::Permanent
}
pub fn is_persistent(&self) -> bool {
self.status == ErrorStatus::Persistent
}
}