Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: optimize self defined rust error #37975

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 21 additions & 26 deletions internal/core/thirdparty/tantivy/tantivy-binding/src/error.rs
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>;
Loading
Loading