-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance: optimize self defined rust error (#37975)
Prepare for issue: #37930 Signed-off-by: sunby <[email protected]>
- Loading branch information
Showing
4 changed files
with
256 additions
and
197 deletions.
There are no files selected for viewing
47 changes: 21 additions & 26 deletions
47
internal/core/thirdparty/tantivy/tantivy-binding/src/error.rs
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,40 +1,35 @@ | ||
use core::fmt; | ||
|
||
use serde_json as json; | ||
|
||
#[derive(Debug)] | ||
pub struct TantivyError{ | ||
reason: String, | ||
pub enum TantivyBindingError { | ||
JsonError(serde_json::Error), | ||
InternalError(String), | ||
} | ||
|
||
impl TantivyError{ | ||
fn new(reason:String) -> Self{ | ||
TantivyError{reason:reason} | ||
} | ||
|
||
pub fn reason(&self) -> String{ | ||
return self.reason.clone() | ||
impl From<serde_json::Error> for TantivyBindingError { | ||
fn from(value: serde_json::Error) -> Self { | ||
TantivyBindingError::JsonError(value) | ||
} | ||
} | ||
|
||
impl From<&str> for TantivyError{ | ||
fn from(value: &str) -> Self { | ||
Self::new(value.to_string()) | ||
impl fmt::Display for TantivyBindingError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
TantivyBindingError::JsonError(e) => write!(f, "JsonError: {}", e), | ||
TantivyBindingError::InternalError(e) => write!(f, "InternalError: {}", e), | ||
} | ||
} | ||
} | ||
|
||
impl From<String> for TantivyError{ | ||
fn from(value: String) -> Self { | ||
Self::new(value) | ||
impl std::error::Error for TantivyBindingError { | ||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { | ||
match self { | ||
TantivyBindingError::JsonError(e) => Some(e), | ||
TantivyBindingError::InternalError(_) => None, | ||
} | ||
} | ||
} | ||
|
||
impl From<json::Error> for TantivyError{ | ||
fn from(value: json::Error) -> Self { | ||
Self::new(value.to_string()) | ||
} | ||
} | ||
|
||
impl ToString for TantivyError{ | ||
fn to_string(&self) -> String { | ||
return self.reason() | ||
} | ||
} | ||
pub type Result<T> = std::result::Result<T, TantivyBindingError>; |
Oops, something went wrong.