tikv_client/common/
errors.rs

1// Copyright 2018 TiKV Project Authors. Licensed under Apache-2.0.
2
3use std::result;
4
5use thiserror::Error;
6
7use crate::proto::kvrpcpb;
8use crate::BoundRange;
9
10/// An error originating from the TiKV client or dependencies.
11#[derive(Debug, Error)]
12#[allow(clippy::large_enum_variant)]
13pub enum Error {
14    /// Feature is not implemented.
15    #[error("Unimplemented feature")]
16    Unimplemented,
17    /// Duplicate key insertion happens.
18    #[error("Duplicate key insertion")]
19    DuplicateKeyInsertion,
20    /// Failed to resolve a lock
21    #[error("Failed to resolve lock")]
22    ResolveLockError(Vec<kvrpcpb::LockInfo>),
23    /// Will raise this error when using a pessimistic txn only operation on an optimistic txn
24    #[error("Invalid operation for this type of transaction")]
25    InvalidTransactionType,
26    /// It's not allowed to perform operations in a transaction after it has been committed or rolled back.
27    #[error("Cannot read or write data after any attempt to commit or roll back the transaction")]
28    OperationAfterCommitError,
29    /// We tried to use 1pc for a transaction, but it didn't work. Probably should have used 2pc.
30    #[error("1PC transaction could not be committed.")]
31    OnePcFailure,
32    /// An operation requires a primary key, but the transaction was empty.
33    #[error("transaction has no primary key")]
34    NoPrimaryKey,
35    /// For raw client, operation is not supported in atomic/non-atomic mode.
36    #[error(
37        "The operation is not supported in current mode, please consider using RawClient with or without atomic mode"
38    )]
39    UnsupportedMode,
40    #[error("There is no current_regions in the EpochNotMatch error")]
41    NoCurrentRegions,
42    #[error("The specified entry is not found in the region cache")]
43    EntryNotFoundInRegionCache,
44    /// Wraps a `std::io::Error`.
45    #[error("IO error: {0}")]
46    Io(#[from] std::io::Error),
47    /// Wraps a `std::io::Error`.
48    #[error("tokio channel error: {0}")]
49    Channel(#[from] tokio::sync::oneshot::error::RecvError),
50    /// Wraps a `grpcio::Error`.
51    #[error("gRPC error: {0}")]
52    Grpc(#[from] tonic::transport::Error),
53    /// Wraps a `grpcio::Error`.
54    #[error("gRPC api error: {0}")]
55    GrpcAPI(#[from] tonic::Status),
56    /// Wraps a `grpcio::Error`.
57    #[error("url error: {0}")]
58    Url(#[from] tonic::codegen::http::uri::InvalidUri),
59    /// Represents that a futures oneshot channel was cancelled.
60    #[error("A futures oneshot channel was canceled. {0}")]
61    Canceled(#[from] futures::channel::oneshot::Canceled),
62    /// Errors caused by changes of region information
63    #[error("Region error: {0:?}")]
64    RegionError(Box<crate::proto::errorpb::Error>),
65    /// Whether the transaction is committed or not is undetermined
66    #[error("Whether the transaction is committed or not is undetermined")]
67    UndeterminedError(Box<Error>),
68    /// Wraps `crate::proto::kvrpcpb::KeyError`
69    #[error("{0:?}")]
70    KeyError(Box<crate::proto::kvrpcpb::KeyError>),
71    /// Multiple errors generated from the ExtractError plan.
72    #[error("Multiple errors: {0:?}")]
73    ExtractedErrors(Vec<Error>),
74    /// Multiple key errors
75    #[error("Multiple key errors: {0:?}")]
76    MultipleKeyErrors(Vec<Error>),
77    /// Invalid ColumnFamily
78    #[error("Unsupported column family {}", _0)]
79    ColumnFamilyError(String),
80    /// Can't join tokio tasks
81    #[error("Failed to join tokio tasks")]
82    JoinError(#[from] tokio::task::JoinError),
83    /// No region is found for the given key.
84    #[error("Region is not found for key: {:?}", key)]
85    RegionForKeyNotFound { key: Vec<u8> },
86    #[error("Region is not found for range: {:?}", range)]
87    RegionForRangeNotFound { range: BoundRange },
88    /// No region is found for the given id. note: distinguish it with the RegionNotFound error in errorpb.
89    #[error("Region {} is not found in the response", region_id)]
90    RegionNotFoundInResponse { region_id: u64 },
91    /// No leader is found for the given id.
92    #[error("Leader of region {} is not found", region_id)]
93    LeaderNotFound { region_id: u64 },
94    /// Scan limit exceeds the maximum
95    #[error("Limit {} exceeds max scan limit {}", limit, max_limit)]
96    MaxScanLimitExceeded { limit: u32, max_limit: u32 },
97    #[error("Invalid Semver string: {0:?}")]
98    InvalidSemver(#[from] semver::Error),
99    /// A string error returned by TiKV server
100    #[error("Kv error. {}", message)]
101    KvError { message: String },
102    #[error("{}", message)]
103    InternalError { message: String },
104    #[error("{0}")]
105    StringError(String),
106    #[error("PessimisticLock error: {:?}", inner)]
107    PessimisticLockError {
108        inner: Box<Error>,
109        success_keys: Vec<Vec<u8>>,
110    },
111}
112
113impl From<crate::proto::errorpb::Error> for Error {
114    fn from(e: crate::proto::errorpb::Error) -> Error {
115        Error::RegionError(Box::new(e))
116    }
117}
118
119impl From<crate::proto::kvrpcpb::KeyError> for Error {
120    fn from(e: crate::proto::kvrpcpb::KeyError) -> Error {
121        Error::KeyError(Box::new(e))
122    }
123}
124
125/// A result holding an [`Error`](enum@Error).
126pub type Result<T> = result::Result<T, Error>;
127
128#[doc(hidden)]
129#[macro_export]
130macro_rules! internal_err {
131    ($e:expr) => ({
132        $crate::Error::InternalError {
133            message: format!("[{}:{}]: {}", file!(), line!(),  $e)
134        }
135    });
136    ($f:tt, $($arg:expr),+) => ({
137        internal_err!(format!($f, $($arg),+))
138    });
139}