From c5f12f10f652de0683b85aa3bfea38042651759a Mon Sep 17 00:00:00 2001 From: Tpt Date: Sun, 6 Mar 2022 22:36:28 +0100 Subject: [PATCH] Better RocksDB error Debug implementation --- lib/src/storage/backend/rocksdb.rs | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/src/storage/backend/rocksdb.rs b/lib/src/storage/backend/rocksdb.rs index 0b6ca298..a27235d4 100644 --- a/lib/src/storage/backend/rocksdb.rs +++ b/lib/src/storage/backend/rocksdb.rs @@ -1021,7 +1021,6 @@ impl SstFileWriter { } } -#[derive(Debug)] struct ErrorStatus(rocksdb_status_t); unsafe impl Send for ErrorStatus {} @@ -1037,15 +1036,32 @@ impl Drop for ErrorStatus { } } -impl fmt::Display for ErrorStatus { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(if self.0.string.is_null() { +impl ErrorStatus { + fn message(&self) -> &str { + if self.0.string.is_null() { "Unknown error" } else { unsafe { CStr::from_ptr(self.0.string) } .to_str() - .map_err(|_| fmt::Error)? - }) + .unwrap_or("Invalid error message") + } + } +} + +impl fmt::Debug for ErrorStatus { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("ErrorStatus") + .field("code", &self.0.code) + .field("subcode", &self.0.subcode) + .field("severity", &self.0.severity) + .field("message", &self.message()) + .finish() + } +} + +impl fmt::Display for ErrorStatus { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(self.message()) } }