backend.rs (3954B)
1 //! Redacted SQL failure classification shared by the existing storage adapters. 2 3 use radroots_storage::Error; 4 5 pub(crate) fn map_backend(source: sqlx::Error) -> Error { 6 if is_capacity(&source) { 7 Error::SpaceInsufficient 8 } else if source.as_database_error().is_some_and(|error| { 9 error.code().as_deref() == Some("1811") 10 && error.message() == "runtime event mutation epoch overflow" 11 }) { 12 Error::EventMutationOverflow 13 } else { 14 Error::BackendUnavailable 15 } 16 } 17 18 pub(crate) fn is_capacity(source: &sqlx::Error) -> bool { 19 match source { 20 sqlx::Error::Database(error) => error 21 .code() 22 .and_then(|code| code.parse::<u32>().ok()) 23 .is_some_and(|code| code & 0xff == 13), 24 sqlx::Error::Io(error) => is_io_capacity(error), 25 _ => false, 26 } 27 } 28 29 pub(crate) fn is_io_capacity(source: &std::io::Error) -> bool { 30 matches!( 31 source.kind(), 32 std::io::ErrorKind::StorageFull | std::io::ErrorKind::QuotaExceeded 33 ) 34 } 35 36 pub(crate) fn startup_error(source: &sqlx::Error, fallback: crate::Error) -> crate::Error { 37 if is_capacity(source) { 38 crate::Error::SpaceInsufficient 39 } else { 40 fallback 41 } 42 } 43 44 #[cfg(test)] 45 mod tests { 46 use super::*; 47 use sqlx::error::{DatabaseError, ErrorKind}; 48 use std::{borrow::Cow, error::Error as StdError, fmt}; 49 50 #[derive(Debug)] 51 struct Coded(Option<&'static str>); 52 impl fmt::Display for Coded { 53 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 54 f.write_str("private path and raw database detail") 55 } 56 } 57 impl StdError for Coded {} 58 impl DatabaseError for Coded { 59 fn message(&self) -> &str { 60 "private path and raw database detail" 61 } 62 fn code(&self) -> Option<Cow<'_, str>> { 63 self.0.map(Cow::Borrowed) 64 } 65 fn as_error(&self) -> &(dyn StdError + Send + Sync + 'static) { 66 self 67 } 68 fn as_error_mut(&mut self) -> &mut (dyn StdError + Send + Sync + 'static) { 69 self 70 } 71 fn into_error(self: Box<Self>) -> Box<dyn StdError + Send + Sync + 'static> { 72 self 73 } 74 fn kind(&self) -> ErrorKind { 75 ErrorKind::Other 76 } 77 } 78 79 #[test] 80 fn numeric_capacity_codes_are_redacted_and_other_database_errors_stay_generic() { 81 for code in [Some("13"), Some("269"), Some("525")] { 82 let mapped = map_backend(sqlx::Error::Database(Box::new(Coded(code)))); 83 assert_eq!(mapped, Error::SpaceInsufficient); 84 assert_eq!(mapped.to_string(), "storage space is insufficient"); 85 assert_eq!(format!("{mapped:?}"), "SpaceInsufficient"); 86 } 87 for code in [ 88 None, 89 Some(""), 90 Some("full"), 91 Some("-13"), 92 Some("4294967296"), 93 Some("11"), 94 Some("5"), 95 Some("10"), 96 Some("1811"), 97 ] { 98 assert_eq!( 99 map_backend(sqlx::Error::Database(Box::new(Coded(code)))), 100 Error::BackendUnavailable 101 ); 102 } 103 } 104 105 #[test] 106 fn typed_io_capacity_is_distinct_without_inventing_a_no_effect_receipt() { 107 use std::io::ErrorKind as Io; 108 for kind in [Io::StorageFull, Io::QuotaExceeded] { 109 let source = std::io::Error::new(kind, "private path and raw file detail"); 110 assert_eq!( 111 map_backend(sqlx::Error::Io(source)), 112 Error::SpaceInsufficient 113 ); 114 } 115 for kind in [Io::PermissionDenied, Io::Other] { 116 assert_eq!( 117 map_backend(sqlx::Error::Io(std::io::Error::from(kind))), 118 Error::BackendUnavailable 119 ); 120 } 121 assert_eq!( 122 map_backend(sqlx::Error::PoolClosed), 123 Error::BackendUnavailable 124 ); 125 } 126 }