From aeab5afb12a550c1fb71717c15cc7af1e676803a Mon Sep 17 00:00:00 2001 From: Bugen Zhao Date: Wed, 7 Feb 2024 18:33:23 +0800 Subject: [PATCH] revert anyhow newtype Signed-off-by: Bugen Zhao --- src/connector/src/error.rs | 29 ++++++--- src/error/src/anyhow.rs | 123 ------------------------------------- src/error/src/lib.rs | 1 - 3 files changed, 20 insertions(+), 133 deletions(-) delete mode 100644 src/error/src/anyhow.rs diff --git a/src/connector/src/error.rs b/src/connector/src/error.rs index 99aaa15d871c..db773add5636 100644 --- a/src/connector/src/error.rs +++ b/src/connector/src/error.rs @@ -12,15 +12,26 @@ // See the License for the specific language governing permissions and // limitations under the License. -use risingwave_common::error::v2::def_anyhow_newtype; +use thiserror::Error; -def_anyhow_newtype! { - /// The error type for the `connector` crate. - /// - /// We use [`anyhow::Error`] under the hood as the connector has to deal with - /// various kinds of errors from different external crates. It acts more like an - /// application and callers may not expect to handle it in a fine-grained way. - pub ConnectorError; +#[derive(Error, Debug)] +pub enum ConnectorError { + #[error("MySQL error: {0}")] + MySql( + #[from] + #[backtrace] + mysql_async::Error, + ), + + #[error("Postgres error: {0}")] + Postgres(#[from] tokio_postgres::Error), + + #[error(transparent)] + Uncategorized( + #[from] + #[backtrace] + anyhow::Error, + ), } -pub type ConnectorResult = std::result::Result; +pub type ConnectorResult = Result; diff --git a/src/error/src/anyhow.rs b/src/error/src/anyhow.rs deleted file mode 100644 index e765d4194f23..000000000000 --- a/src/error/src/anyhow.rs +++ /dev/null @@ -1,123 +0,0 @@ -// Copyright 2024 RisingWave Labs -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -/// Define a newtype wrapper around [`anyhow::Error`]. -/// -/// # Motivation -/// -/// [`anyhow::Error`] is good enough if one just wants to make the error type -/// informative but not necessarily actionable. However, given a value of type -/// [`anyhow::Error`], it is hard to tell which module or crate it comes from, -/// which may blur the boundary between modules when passing it around, leading -/// to abuse. -/// -/// # Usage -/// -/// ```ignore -/// def_anyhow_newtype! { -/// /// Documentation for the newtype. -/// #[derive(..)] -/// pub MyError; -/// } -/// ``` -/// -/// This will define a newtype `MyError` around [`anyhow::Error`]. -/// -/// The newtype can be converted from any type that implements `Into`, -/// so the developing experience is kept almost the same. To construct a new error, -/// one can still use macros like `anyhow::anyhow!` or `risingwave_common::bail!`. -/// -/// Since `bail!` and `?` already imply an `into()` call, developers do not need to -/// care about the conversion from [`anyhow::Error`] to the newtype most of the time. -/// -/// # Limitation -/// -/// Note that the newtype does not implement [`std::error::Error`] just like -/// [`anyhow::Error`]. However, it can be dereferenced to `dyn std::error::Error` -/// to be used in places like `thiserror`'s `#[source]` attribute. -#[macro_export] -macro_rules! def_anyhow_newtype { - ($(#[$attr:meta])* $vis:vis $name:ident $(;)?) => { - $(#[$attr])* $vis struct $name(::anyhow::Error); - - impl $name { - /// Unwrap the newtype to get the inner [`anyhow::Error`]. - pub fn into_inner(self) -> ::anyhow::Error { - self.0 - } - } - - impl std::ops::Deref for $name { - type Target = ::anyhow::Error; - - fn deref(&self) -> &Self::Target { - &self.0 - } - } - - impl std::fmt::Debug for $name { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.0.fmt(f) - } - } - - impl std::fmt::Display for $name { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.0.fmt(f) - } - } - - impl From for $name - where - E: Into<::anyhow::Error>, - { - fn from(value: E) -> Self { - Self(value.into()) - } - } - - impl From<$name> for Box { - fn from(value: $name) -> Self { - value.0.into() - } - } - - paste::paste! { - /// Provides the `context` method for `Result` of [`ConnectorError`]. - /// - /// This trait is the supplement of the [`anyhow::Context`] trait as it cannot be - /// implemented for types outside of the crate. - #[easy_ext::ext([< $name Context >])] - $vis impl Result { - /// Wrap the error value with additional context. - fn context(self, context: C) -> Result - where - C: std::fmt::Display + Send + Sync + 'static, - { - ::anyhow::Context::context(self.map_err(|error| error.0), context) - } - - /// Wrap the error value with additional context that is evaluated lazily - /// only once an error does occur. - fn with_context(self, context: F) -> Result - where - C: std::fmt::Display + Send + Sync + 'static, - F: FnOnce() -> C, - { - ::anyhow::Context::with_context(self.map_err(|error| error.0), context) - } - } - } - }; -} diff --git a/src/error/src/lib.rs b/src/error/src/lib.rs index d3364485e8f2..ccfec0cfbcc1 100644 --- a/src/error/src/lib.rs +++ b/src/error/src/lib.rs @@ -21,5 +21,4 @@ #![feature(register_tool)] #![register_tool(rw)] -pub mod anyhow; pub mod tonic;