chaokunyang commented on code in PR #2765:
URL: https://github.com/apache/fory/pull/2765#discussion_r2429694639
##########
rust/fory-core/src/error.rs:
##########
@@ -15,38 +15,98 @@
// specific language governing permissions and limitations
// under the License.
+use std::borrow::Cow;
+use std::io;
+
use thiserror::Error;
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum Error {
- #[error("Fory on Rust not support Ref type")]
- Ref,
+ #[error("Type mismatch: type_a = {0}, type_b = {1}")]
+ TypeMismatch(u32, u32),
+
+ #[error("Buffer out of bound: {0} + {1} > {2}")]
+ BufferOutOfBound(usize, usize, usize),
+
+ #[error("IO error: {0}")]
+ Io(#[from] io::Error),
+
+ /// A simple error message, stored as a [`Cow<'static, str>`].
+ /// Do not construct this variant directly; use [`Error::msg`] instead.
+ #[error("{0}")]
+ Unknown(Cow<'static, str>),
+}
- #[error(transparent)]
- Other(#[from] anyhow::Error),
+impl Error {
+ /// Creates a new [`Error::Msg`] from a string or static message.
+ ///
+ /// This function is a convenient way to produce an error message
+ /// from a literal, `String`, or any type that can be converted into
+ /// a [`Cow<'static, str>`].
+ ///
+ /// # Example
+ /// ```
+ /// use fory_core::error::Error;
+ ///
+ /// let err = Error::msg("Something went wrong");
+ /// let err = Error::msg(format!("ID:{} not found", 1));
+ /// ```
+ #[inline(always)]
+ pub fn msg<S: Into<Cow<'static, str>>>(s: S) -> Self {
Review Comment:
you can provide static func like serde:
```rust
pub trait Error: Sized + StdError {
fn custom<T>(msg: T) -> Self
where
T: Display;
fn invalid_type(unexp: Unexpected<'_>, exp: &dyn Expected) -> Self { ...
}
fn invalid_value(unexp: Unexpected<'_>, exp: &dyn Expected) -> Self {
... }
fn invalid_length(len: usize, exp: &dyn Expected) -> Self { ... }
fn unknown_variant(variant: &str, expected: &'static [&'static str]) ->
Self { ... }
fn unknown_field(field: &str, expected: &'static [&'static str]) -> Self
{ ... }
fn missing_field(field: &'static str) -> Self { ... }
fn duplicate_field(field: &'static str) -> Self { ... }
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]