Skip to content

Commit

Permalink
use derived ctor macro and bail macros
Browse files Browse the repository at this point in the history
Signed-off-by: Bugen Zhao <[email protected]>
  • Loading branch information
BugenZhao committed Nov 24, 2023
1 parent 64aad6c commit bb34b5e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 58 deletions.
7 changes: 3 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ arrow-flight = "49"
arrow-select = "49"
arrow-ord = "49"
arrow-row = "49"
thiserror-ext = "0.0.7"
# thiserror-ext = "0.0.7"
thiserror-ext = { git = "https://github.com/risingwavelabs/thiserror-ext.git", rev = "0912af" }
tikv-jemalloc-ctl = { git = "https://github.com/risingwavelabs/jemallocator.git", rev = "64a2d9" }
tikv-jemallocator = { git = "https://github.com/risingwavelabs/jemallocator.git", features = [
"profiling",
Expand Down
61 changes: 8 additions & 53 deletions src/common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use memcomparable::Error as MemComparableError;
use risingwave_error::tonic::{ToTonicStatus, TonicStatusWrapper};
use risingwave_pb::PbFieldNotFound;
use thiserror::Error;
use thiserror_ext::Macro;
use tokio::task::JoinError;

use crate::array::ArrayError;
Expand All @@ -36,7 +37,7 @@ pub type BoxedError = Box<dyn Error>;

pub use anyhow::anyhow as anyhow_error;

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, Default)]
pub struct TrackingIssue(Option<u32>);

impl TrackingIssue {
Expand Down Expand Up @@ -70,59 +71,13 @@ impl Display for TrackingIssue {
}
}

#[derive(Error, Debug)]
#[error("Feature is not yet implemented: {feature}. {tracking_issue}")]
#[derive(Error, Debug, Macro)]
#[error("Feature is not yet implemented: {feature}. {issue}")]
#[thiserror_ext(macro(path = "crate::error"))]
pub struct NotImplemented {
#[message]
pub feature: String,
pub tracking_issue: TrackingIssue,
}

impl<S> From<S> for NotImplemented
where
S: Into<String>,
{
fn from(feature: S) -> Self {
Self::new(feature)
}
}

impl NotImplemented {
pub fn new(feature: impl Into<String>) -> Self {
Self::with_tracking_issue(feature, TrackingIssue::none())
}

pub fn with_tracking_issue(
feature: impl Into<String>,
tracking_issue: impl Into<TrackingIssue>,
) -> Self {
Self {
feature: feature.into(),
tracking_issue: tracking_issue.into(),
}
}
}

#[macro_export]
macro_rules! not_implemented {
(issue = $issue:expr, $($arg:tt)*) => {
$crate::error::NotImplemented::with_tracking_issue(
::std::format!($($arg)*),
$issue,
)
};
($($arg:tt)*) => {
not_implemented!(issue = None, $($arg)*)
};
}

#[macro_export(local_inner_macros)]
macro_rules! bail_not_implemented {
(issue = $issue:expr, $($arg:tt)*) => {
return Err(not_implemented!(issue = $issue, $($arg)*).into())
};
($($arg:tt)*) => {
bail_not_implemented!(issue = None, $($arg)*)
};
pub issue: TrackingIssue,
}

#[derive(Error, Debug)]
Expand Down Expand Up @@ -672,7 +627,7 @@ mod tests {
check_grpc_error(ErrorCode::TaskNotFound, Code::Internal);
check_grpc_error(ErrorCode::InternalError(String::new()), Code::Internal);
check_grpc_error(
ErrorCode::NotImplemented(NotImplemented::new("test")),
ErrorCode::NotImplemented(not_implemented!("test")),
Code::Internal,
);
}
Expand Down

0 comments on commit bb34b5e

Please sign in to comment.