From e92a43b46679dbd63dff0ec1baf5e74af8442cd1 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Fri, 9 Feb 2024 03:06:03 -0500 Subject: [PATCH] Simplify some storage errors and deduplicate them --- lib/oxigraph/src/storage/backend/fallback.rs | 2 +- lib/oxigraph/src/storage/backend/rocksdb.rs | 2 +- lib/oxigraph/src/storage/error.rs | 14 +++++++++ lib/oxigraph/src/storage/numeric_encoder.rs | 30 ++++---------------- 4 files changed, 22 insertions(+), 26 deletions(-) diff --git a/lib/oxigraph/src/storage/backend/fallback.rs b/lib/oxigraph/src/storage/backend/fallback.rs index c2eb17ec..7214851e 100644 --- a/lib/oxigraph/src/storage/backend/fallback.rs +++ b/lib/oxigraph/src/storage/backend/fallback.rs @@ -36,7 +36,7 @@ impl Db { if self.0.read().unwrap().contains_key(&column_family) { Ok(column_family) } else { - Err(CorruptionError::msg(format!("Column family {name} does not exist")).into()) + Err(CorruptionError::from_missing_column_family_name(name).into()) } } diff --git a/lib/oxigraph/src/storage/backend/rocksdb.rs b/lib/oxigraph/src/storage/backend/rocksdb.rs index 184e30fe..14c45d0a 100644 --- a/lib/oxigraph/src/storage/backend/rocksdb.rs +++ b/lib/oxigraph/src/storage/backend/rocksdb.rs @@ -548,7 +548,7 @@ impl Db { return Ok(ColumnFamily(*cf_handle)); } } - Err(CorruptionError::msg(format!("Column family {name} does not exist")).into()) + Err(CorruptionError::from_missing_column_family_name(name).into()) } #[must_use] diff --git a/lib/oxigraph/src/storage/error.rs b/lib/oxigraph/src/storage/error.rs index 7a119637..26322353 100644 --- a/lib/oxigraph/src/storage/error.rs +++ b/lib/oxigraph/src/storage/error.rs @@ -1,5 +1,7 @@ use crate::io::{ParseError, RdfFormat}; +use crate::storage::numeric_encoder::EncodedTerm; use oxiri::IriParseError; +use oxrdf::TermRef; use std::error::Error; use std::io; @@ -45,6 +47,18 @@ impl CorruptionError { Self::Other(error.into()) } + #[inline] + pub(crate) fn from_encoded_term(encoded: &EncodedTerm, term: &TermRef<'_>) -> Self { + // TODO: eventually use a dedicated error enum value + Self::new(format!("Invalid term encoding {encoded:?} for {term}")) + } + + #[inline] + pub(crate) fn from_missing_column_family_name(name: &'static str) -> Self { + // TODO: eventually use a dedicated error enum value + Self::new(format!("Column family {name} does not exist")) + } + /// Builds an error from a printable error message. #[inline] pub(crate) fn msg(msg: impl Into) -> Self { diff --git a/lib/oxigraph/src/storage/numeric_encoder.rs b/lib/oxigraph/src/storage/numeric_encoder.rs index e730a163..bf4b070b 100644 --- a/lib/oxigraph/src/storage/numeric_encoder.rs +++ b/lib/oxigraph/src/storage/numeric_encoder.rs @@ -713,19 +713,13 @@ pub fn insert_term Result<(), StorageError>>( if let EncodedTerm::NamedNode { iri_id } = encoded { insert_str(iri_id, node.as_str()) } else { - Err( - CorruptionError::new(format!("Invalid term encoding {encoded:?} for {term}")) - .into(), - ) + Err(CorruptionError::from_encoded_term(encoded, &term).into()) } } TermRef::BlankNode(node) => match encoded { EncodedTerm::BigBlankNode { id_id } => insert_str(id_id, node.as_str()), EncodedTerm::SmallBlankNode(..) | EncodedTerm::NumericalBlankNode { .. } => Ok(()), - _ => Err( - CorruptionError::new(format!("Invalid term encoding {encoded:?} for {term}")) - .into(), - ), + _ => Err(CorruptionError::from_encoded_term(encoded, &term).into()), }, TermRef::Literal(literal) => match encoded { EncodedTerm::BigStringLiteral { value_id } @@ -736,10 +730,7 @@ pub fn insert_term Result<(), StorageError>>( if let Some(language) = literal.language() { insert_str(language_id, language) } else { - Err(CorruptionError::new(format!( - "Invalid term encoding {encoded:?} for {term}" - )) - .into()) + Err(CorruptionError::from_encoded_term(encoded, &term).into()) } } EncodedTerm::BigBigLangStringLiteral { @@ -750,10 +741,7 @@ pub fn insert_term Result<(), StorageError>>( if let Some(language) = literal.language() { insert_str(language_id, language) } else { - Err(CorruptionError::new(format!( - "Invalid term encoding {encoded:?} for {term}" - )) - .into()) + Err(CorruptionError::from_encoded_term(encoded, &term).into()) } } EncodedTerm::SmallTypedLiteral { datatype_id, .. } => { @@ -784,10 +772,7 @@ pub fn insert_term Result<(), StorageError>>( | EncodedTerm::DurationLiteral(..) | EncodedTerm::YearMonthDurationLiteral(..) | EncodedTerm::DayTimeDurationLiteral(..) => Ok(()), - _ => Err( - CorruptionError::new(format!("Invalid term encoding {encoded:?} for {term}")) - .into(), - ), + _ => Err(CorruptionError::from_encoded_term(encoded, &term).into()), }, TermRef::Triple(triple) => { if let EncodedTerm::Triple(encoded) = encoded { @@ -799,10 +784,7 @@ pub fn insert_term Result<(), StorageError>>( )?; insert_term(triple.object.as_ref(), &encoded.object, insert_str) } else { - Err( - CorruptionError::new(format!("Invalid term encoding {encoded:?} for {term}")) - .into(), - ) + Err(CorruptionError::from_encoded_term(encoded, &term).into()) } } }