fix: HasError for Result

Signed-off-by: ekexium <ekexium@gmail.com>
This commit is contained in:
ekexium 2021-02-26 17:43:13 +08:00
parent 3fad149ff3
commit 740d889253
2 changed files with 8 additions and 2 deletions

View File

@ -69,6 +69,8 @@ pub enum Error {
KvError { message: String },
#[error("{}", message)]
InternalError { message: String },
#[error("{0}")]
StringError(String),
}
impl From<tikv_client_proto::errorpb::Error> for Error {

View File

@ -1,6 +1,7 @@
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
use crate::Error;
use std::fmt::Display;
use tikv_client_proto::kvrpcpb;
pub trait HasRegionError {
@ -150,9 +151,12 @@ impl HasError for kvrpcpb::PessimisticRollbackResponse {
}
}
impl<T: HasError, E> HasError for Result<T, E> {
impl<T: HasError, E: Display> HasError for Result<T, E> {
fn error(&mut self) -> Option<Error> {
self.as_mut().ok().and_then(|t| t.error())
match self {
Ok(x) => x.error(),
Err(e) => Some(Error::StringError(e.to_string())),
}
}
}