use std::fmt::{Display, Formatter};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RelationalErrorKind {
Constraint,
Schema,
}
impl RelationalErrorKind {
pub const fn as_str(self) -> &'static str {
match self {
Self::Constraint => "constraint",
Self::Schema => "schema",
}
}
}
impl Display for RelationalErrorKind {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
formatter.write_str(match self {
Self::Constraint => "relational constraint violation",
Self::Schema => "relational schema error",
})
}
}
impl std::error::Error for RelationalErrorKind {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RelationalErrorSurface {
Storage,
Execution,
}
impl RelationalErrorSurface {
pub(super) const fn prefix(self) -> &'static str {
match self {
Self::Storage => "storage error",
Self::Execution => "execution error",
}
}
}
pub fn find_relational_error_kind(
error: &(dyn std::error::Error + 'static),
) -> Option<RelationalErrorKind> {
let mut current = Some(error);
while let Some(error) = current {
if let Some(kind) = error.downcast_ref::<RelationalErrorKind>() {
return Some(*kind);
}
if let Some(kind) = error
.downcast_ref::<crate::HawDBError>()
.and_then(crate::HawDBError::relational_error_kind)
{
return Some(kind);
}
if let Some(error) = error.downcast_ref::<std::io::Error>()
&& let Some(inner) = error.get_ref()
&& let Some(kind) = find_relational_error_kind(inner)
{
return Some(kind);
}
current = error.source();
}
None
}
#[cfg(test)]
mod tests {
use super::*;
use crate::HawDBError;
#[derive(Debug)]
struct Refusal(RelationalErrorKind, &'static str);
impl Display for Refusal {
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
write!(formatter, "{}: {}", self.0, self.1)
}
}
impl std::error::Error for Refusal {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(match self.0 {
RelationalErrorKind::Constraint => &RelationalErrorKind::Constraint,
RelationalErrorKind::Schema => &RelationalErrorKind::Schema,
})
}
}
#[test]
fn storage_conversion_keeps_the_kind_and_the_storage_text() {
for (kind, detail) in [
(RelationalErrorKind::Constraint, "duplicate primary key 1"),
(RelationalErrorKind::Schema, "column id expects Int64"),
] {
let text = format!("storage error: {kind}: {detail}");
let direct = HawDBError::from_storage_error(Refusal(kind, detail));
assert_eq!(direct.relational_error_kind(), Some(kind));
assert_eq!(direct.to_string(), text);
let wrapped =
HawDBError::from_storage_error(std::io::Error::other(Refusal(kind, detail)));
assert_eq!(wrapped, direct);
assert_eq!(HawDBError::from_storage_error(direct.clone()), direct);
let context = HawDBError::from_storage_error_with_context(Refusal(kind, detail), "ctx");
assert_eq!(context.relational_error_kind(), Some(kind));
assert_eq!(
context.to_string(),
format!("storage error: ctx: {kind}: {detail}")
);
}
}
#[test]
fn execution_surface_keeps_the_execution_text() {
let error = HawDBError::relational(
RelationalErrorKind::Constraint,
RelationalErrorSurface::Execution,
"relational constraint violation: duplicate",
);
assert_eq!(
error.to_string(),
"execution error: relational constraint violation: duplicate"
);
assert_eq!(
error.relational_error_kind(),
Some(RelationalErrorKind::Constraint)
);
assert_eq!(
error.relational_error_surface(),
Some(RelationalErrorSurface::Execution)
);
}
#[test]
fn other_storage_errors_have_no_relational_kind() {
let error = HawDBError::from_storage_error(std::io::Error::other("disk"));
assert!(matches!(error, HawDBError::Storage(_)));
assert_eq!(error.relational_error_kind(), None);
assert_eq!(
HawDBError::Execution("x".into()).relational_error_kind(),
None
);
}
}