Skip to content

Commit

Permalink
chore: panic if the database is corrupted
Browse files Browse the repository at this point in the history
  • Loading branch information
yangby-cryptape committed Jul 21, 2021
1 parent dc3cf8d commit 893654a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion db/src/db.rs
Expand Up @@ -138,7 +138,7 @@ impl RocksDB {
See https://github.com/facebook/rocksdb/wiki/RocksDB-Repairer for detail",
err_str
);
Err(internal_error("DB corrupted"))
Err(internal_error(err_str))
} else {
Err(internal_error(format!(
"failed to open the database: {}",
Expand Down
7 changes: 6 additions & 1 deletion db/src/lib.rs
Expand Up @@ -28,5 +28,10 @@ pub use rocksdb::{
pub type Result<T> = result::Result<T, Error>;

fn internal_error<S: fmt::Display>(reason: S) -> Error {
InternalErrorKind::Database.other(reason).into()
let message = reason.to_string();
if message.starts_with("Corruption:") {
InternalErrorKind::Database.other(message).into()
} else {
InternalErrorKind::DataCorrupted.other(message).into()
}
}
15 changes: 12 additions & 3 deletions sync/src/utils.rs
Expand Up @@ -78,13 +78,22 @@ fn item_id<Message: Entity>(protocol_id: ProtocolId, message: &Message) -> u32 {
}

/// return whether the error's kind is `InternalErrorKind::Database`
///
/// ### Panic
///
/// Panic if the error kind is `InternalErrorKind::DataCorrupted`.
/// If the database is corrupted, panic is better than handle it silently.
pub(crate) fn is_internal_db_error(error: &CKBError) -> bool {
if error.kind() == ErrorKind::Internal {
return error
let error_kind = error
.downcast_ref::<InternalError>()
.expect("error kind checked")
.kind()
== InternalErrorKind::Database;
.kind();
if error_kind == InternalErrorKind::DataCorrupted {
panic!("{}", error)
} else {
return error_kind == InternalErrorKind::Database;
}
}
false
}

0 comments on commit 893654a

Please sign in to comment.