chore(db): make database error codes signed (#1236)

Co-authored-by: lambdaclass-user <github@lambdaclass.com>
This commit is contained in:
Tomás
2023-02-16 15:39:30 -03:00
committed by GitHub
parent f979c6c1fb
commit db4c4fb8d1
3 changed files with 17 additions and 18 deletions

View File

@ -3,28 +3,28 @@
pub enum Error {
/// Failed to open database.
#[error("{0:?}")]
DatabaseLocation(u32),
DatabaseLocation(i32),
/// Failed to create a table in database.
#[error("Table Creating error code: {0:?}")]
TableCreation(u32),
TableCreation(i32),
/// Failed to insert a value into a table.
#[error("Database write error code: {0:?}")]
Write(u32),
Write(i32),
/// Failed to get a value into a table.
#[error("Database read error code: {0:?}")]
Read(u32),
Read(i32),
/// Failed to delete a `(key, value)` pair into a table.
#[error("Database delete error code: {0:?}")]
Delete(u32),
Delete(i32),
/// Failed to commit transaction changes into the database.
#[error("Database commit error code: {0:?}")]
Commit(u32),
Commit(i32),
/// Failed to initiate a transaction.
#[error("Initialization of transaction errored with code: {0:?}")]
InitTransaction(u32),
InitTransaction(i32),
/// Failed to initiate a cursor.
#[error("Initialization of cursor errored with code: {0:?}")]
InitCursor(u32),
InitCursor(i32),
/// Failed to decode a key from a table.
#[error("Error decoding value.")]
DecodeError,

View File

@ -378,7 +378,7 @@ mod tests {
assert_eq!(cursor.current(), Ok(Some((key_to_insert, H256::zero()))));
// INSERT (failure)
assert_eq!(cursor.insert(key_to_insert, H256::zero()), Err(Error::Write(4294936497)));
assert_eq!(cursor.insert(key_to_insert, H256::zero()), Err(Error::Write(-30799)));
assert_eq!(cursor.current(), Ok(Some((key_to_insert, H256::zero()))));
tx.commit().expect(ERROR_COMMIT);
@ -467,7 +467,7 @@ mod tests {
let key_to_append = 2;
let tx = db.tx_mut().expect(ERROR_INIT_TX);
let mut cursor = tx.cursor_write::<CanonicalHeaders>().unwrap();
assert_eq!(cursor.append(key_to_append, H256::zero()), Err(Error::Write(4294936878)));
assert_eq!(cursor.append(key_to_append, H256::zero()), Err(Error::Write(-30418)));
assert_eq!(cursor.current(), Ok(Some((5, H256::zero())))); // the end of table
tx.commit().expect(ERROR_COMMIT);
@ -507,14 +507,14 @@ mod tests {
transition_id,
AccountBeforeTx { address: Address::from_low_u64_be(subkey_to_append), info: None }
),
Err(Error::Write(4294936878))
Err(Error::Write(-30418))
);
assert_eq!(
cursor.append(
transition_id - 1,
AccountBeforeTx { address: Address::from_low_u64_be(subkey_to_append), info: None }
),
Err(Error::Write(4294936878))
Err(Error::Write(-30418))
);
assert_eq!(
cursor.append(

View File

@ -96,8 +96,8 @@ impl Error {
}
/// Converts an [Error] to the raw error code.
pub fn to_err_code(&self) -> u32 {
let err_code = match self {
pub fn to_err_code(&self) -> i32 {
match self {
Error::KeyExist => ffi::MDBX_KEYEXIST,
Error::NotFound => ffi::MDBX_NOTFOUND,
Error::NoData => ffi::MDBX_ENODATA,
@ -129,12 +129,11 @@ impl Error {
Error::BadSignature => ffi::MDBX_EBADSIGN,
Error::Other(err_code) => *err_code,
_ => unreachable!(),
};
err_code as u32
}
}
}
impl From<Error> for u32 {
impl From<Error> for i32 {
fn from(value: Error) -> Self {
value.to_err_code()
}
@ -145,7 +144,7 @@ impl fmt::Display for Error {
let value = match self {
Self::DecodeErrorLenDiff => "Mismatched data length",
_ => unsafe {
let err = ffi::mdbx_strerror(self.to_err_code() as i32);
let err = ffi::mdbx_strerror(self.to_err_code());
str::from_utf8_unchecked(CStr::from_ptr(err).to_bytes())
},
};