This repository has been archived by the owner on Aug 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace failure with thiserror (#52)
* Replace failure with thiserror * Mark transparent for some errors
- Loading branch information
Showing
36 changed files
with
358 additions
and
394 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,179 +1,115 @@ | ||
use failure::Fail; | ||
use crate::kernel::lsm::compactor::CompactTask; | ||
use crate::kernel::lsm::version::cleaner::CleanTag; | ||
use std::io; | ||
use thiserror::Error; | ||
use tokio::sync::mpsc::error::SendError; | ||
use tokio::sync::oneshot::error::RecvError; | ||
|
||
/// Error type for kvs | ||
#[derive(Fail, Debug)] | ||
#[derive(Error, Debug)] | ||
#[non_exhaustive] | ||
pub enum KernelError { | ||
/// IO error | ||
#[fail(display = "{}", _0)] | ||
Io(#[cause] io::Error), | ||
#[fail(display = "{}", _0)] | ||
Recv(#[cause] RecvError), | ||
#[error(transparent)] | ||
Io(#[from] io::Error), | ||
|
||
#[error(transparent)] | ||
RecvError(#[from] RecvError), | ||
|
||
#[error("Failed to send compact task")] | ||
SendCompactTaskError(#[from] SendError<CompactTask>), | ||
|
||
#[error("Failed to send clean tag")] | ||
SendCleanTagError(#[from] SendError<CleanTag>), | ||
|
||
/// Serialization or deserialization error | ||
#[fail(display = "{}", _0)] | ||
SerdeBinCode(#[cause] Box<bincode::ErrorKind>), | ||
#[error(transparent)] | ||
SerdeBinCode(#[from] Box<bincode::ErrorKind>), | ||
|
||
/// Remove no-existent key error | ||
#[fail(display = "Key not found")] | ||
#[error("Key not found")] | ||
KeyNotFound, | ||
#[fail(display = "Data is empty")] | ||
|
||
#[error("Data is empty")] | ||
DataEmpty, | ||
#[fail(display = "Max Level is 7")] | ||
|
||
#[error("Max Level is 7")] | ||
LevelOver, | ||
#[fail(display = "Not the correct type of Cmd")] | ||
|
||
#[error("Not the correct type of Cmd")] | ||
NotMatchCmd, | ||
#[fail(display = "CRC code does not match")] | ||
|
||
#[error("CRC code does not match")] | ||
CrcMisMatch, | ||
#[fail(display = "{}", _0)] | ||
SledErr(#[cause] sled::Error), | ||
#[fail(display = "Cache size overflow")] | ||
|
||
#[error(transparent)] | ||
SledErr(#[from] sled::Error), | ||
|
||
#[error("Cache size overflow")] | ||
CacheSizeOverFlow, | ||
#[fail(display = "Cache sharding and size overflow")] | ||
|
||
#[error("Cache sharding and size overflow")] | ||
CacheShardingNotAlign, | ||
#[fail(display = "File not found")] | ||
|
||
#[error("File not found")] | ||
FileNotFound, | ||
|
||
/// 正常情况wal在内存中存在索引则表示硬盘中存在有对应的数据 | ||
/// 而错误则是内存存在索引却在硬盘中不存在这个数据 | ||
#[fail(display = "WAL log load error")] | ||
#[error("WAL log load error")] | ||
WalLoad, | ||
|
||
/// Unexpected command type error. | ||
/// It indicated a corrupted log or a program bug. | ||
#[fail(display = "Unexpected command type")] | ||
#[error("Unexpected command type")] | ||
UnexpectedCommandType, | ||
#[fail(display = "Process already exists")] | ||
|
||
#[error("Process already exists")] | ||
ProcessExists, | ||
#[fail(display = "channel is closed")] | ||
|
||
#[error("channel is closed")] | ||
ChannelClose, | ||
#[fail(display = "{}", _0)] | ||
NotSupport(&'static str), | ||
} | ||
|
||
#[derive(Fail, Debug)] | ||
#[non_exhaustive] | ||
pub enum ConnectionError { | ||
#[fail(display = "{}", _0)] | ||
IO(#[cause] io::Error), | ||
#[fail(display = "disconnected")] | ||
Disconnected, | ||
#[fail(display = "write failed")] | ||
WriteFailed, | ||
#[fail(display = "wrong instruction")] | ||
WrongInstruction, | ||
#[fail(display = "encode error")] | ||
EncodeErr, | ||
#[fail(display = "decode error")] | ||
DecodeErr, | ||
#[fail(display = "server flush error")] | ||
FlushError, | ||
#[fail(display = "{}", _0)] | ||
StoreErr(#[cause] KernelError), | ||
#[fail(display = "Failed to connect to server, {}", _0)] | ||
TonicTransportErr(#[cause] tonic::transport::Error), | ||
#[fail(display = "Failed to call server, {}", _0)] | ||
TonicFailureStatus(#[cause] tonic::Status), | ||
#[fail(display = "Failed to parse addr, {}", _0)] | ||
AddrParseError(#[cause] std::net::AddrParseError), | ||
} | ||
#[error("{0}")] | ||
NotSupport(&'static str), | ||
|
||
#[derive(Fail, Debug)] | ||
#[non_exhaustive] | ||
#[allow(missing_copy_implementations)] | ||
pub enum CacheError { | ||
#[fail(display = "The number of caches cannot be divisible by the number of shards")] | ||
#[error("The number of caches cannot be divisible by the number of shards")] | ||
ShardingNotAlign, | ||
#[fail(display = "Cache size overflow")] | ||
CacheSizeOverFlow, | ||
#[fail(display = "{}", _0)] | ||
StoreErr(#[cause] KernelError), | ||
} | ||
|
||
impl<T> From<SendError<T>> for KernelError { | ||
#[inline] | ||
fn from(_: SendError<T>) -> Self { | ||
KernelError::ChannelClose | ||
} | ||
} | ||
|
||
impl From<io::Error> for ConnectionError { | ||
#[inline] | ||
fn from(err: io::Error) -> Self { | ||
ConnectionError::IO(err) | ||
} | ||
} | ||
#[derive(Error, Debug)] | ||
#[non_exhaustive] | ||
pub enum ConnectionError { | ||
#[error(transparent)] | ||
IO(#[from] io::Error), | ||
|
||
impl From<io::Error> for KernelError { | ||
#[inline] | ||
fn from(err: io::Error) -> Self { | ||
KernelError::Io(err) | ||
} | ||
} | ||
#[error("disconnected")] | ||
Disconnected, | ||
|
||
impl From<RecvError> for KernelError { | ||
#[inline] | ||
fn from(err: RecvError) -> Self { | ||
KernelError::Recv(err) | ||
} | ||
} | ||
#[error("write failed")] | ||
WriteFailed, | ||
|
||
impl From<Box<bincode::ErrorKind>> for KernelError { | ||
#[inline] | ||
fn from(err: Box<bincode::ErrorKind>) -> Self { | ||
KernelError::SerdeBinCode(err) | ||
} | ||
} | ||
#[error("wrong instruction")] | ||
WrongInstruction, | ||
|
||
impl From<sled::Error> for KernelError { | ||
#[inline] | ||
fn from(err: sled::Error) -> Self { | ||
KernelError::SledErr(err) | ||
} | ||
} | ||
#[error("encode error")] | ||
EncodeErr, | ||
|
||
impl From<KernelError> for ConnectionError { | ||
#[inline] | ||
fn from(err: KernelError) -> Self { | ||
ConnectionError::StoreErr(err) | ||
} | ||
} | ||
#[error("decode error")] | ||
DecodeErr, | ||
|
||
impl From<tonic::Status> for ConnectionError { | ||
#[inline] | ||
fn from(status: tonic::Status) -> Self { | ||
ConnectionError::TonicFailureStatus(status) | ||
} | ||
} | ||
#[error("server flush error")] | ||
FlushError, | ||
|
||
impl From<tonic::transport::Error> for ConnectionError { | ||
#[inline] | ||
fn from(err: tonic::transport::Error) -> Self { | ||
ConnectionError::TonicTransportErr(err) | ||
} | ||
} | ||
#[error("Failed to connect to server, {0}")] | ||
TonicTransportErr(#[from] tonic::transport::Error), | ||
|
||
impl From<std::net::AddrParseError> for ConnectionError { | ||
#[inline] | ||
fn from(err: std::net::AddrParseError) -> Self { | ||
ConnectionError::AddrParseError(err) | ||
} | ||
} | ||
#[error("Failed to call server, {0}")] | ||
TonicFailureStatus(#[from] tonic::Status), | ||
|
||
impl From<CacheError> for KernelError { | ||
#[inline] | ||
fn from(value: CacheError) -> Self { | ||
match value { | ||
CacheError::StoreErr(kv_error) => kv_error, | ||
CacheError::CacheSizeOverFlow => KernelError::CacheSizeOverFlow, | ||
CacheError::ShardingNotAlign => KernelError::CacheShardingNotAlign, | ||
} | ||
} | ||
} | ||
#[error("Failed to parse addr, {0}")] | ||
AddrParseError(#[from] std::net::AddrParseError), | ||
|
||
impl From<KernelError> for CacheError { | ||
#[inline] | ||
fn from(value: KernelError) -> Self { | ||
CacheError::StoreErr(value) | ||
} | ||
#[error(transparent)] | ||
KernelError(#[from] KernelError), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.