From 0521ddccf8415a98ac699cae2ba71dcf4f24347d Mon Sep 17 00:00:00 2001 From: xiao Date: Wed, 19 Apr 2023 17:52:20 +0800 Subject: [PATCH 1/2] =?UTF-8?q?coding=20=E6=94=B9=E5=8A=A8=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/util/hash_test.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/util/hash_test.rs b/src/util/hash_test.rs index bb8dc7b..28bf95d 100644 --- a/src/util/hash_test.rs +++ b/src/util/hash_test.rs @@ -47,13 +47,11 @@ fn test_hash_code() { let hash_val = Hash::hash_code(&data3, 0xbc9f1d34); assert_eq!(0x323c078f, hash_val); - // todo coding 重写后,用例报错 let hash_val = Hash::hash_code(&data4, 0xbc9f1d34); assert_eq!(0xed21633a, hash_val); - // todo coding 重写后,用例报错 - // let hash_val = Hash::hash_code(&data5, 0x12345678); - // assert_eq!(0xf333dabb, hash_val); + let hash_val = Hash::hash_code(&data5, 0x12345678); + assert_eq!(0xf333dabb, hash_val); } #[test] -- Gitee From 8a4f4ab28504869c7cf46fad5c6087704ace3182 Mon Sep 17 00:00:00 2001 From: fengyang Date: Mon, 5 Jun 2023 15:35:53 +0800 Subject: [PATCH 2/2] =?UTF-8?q?error=5Fcode=20=E5=AE=9E=E7=8E=B0=E3=80=82?= =?UTF-8?q?=20=E9=87=8D=E6=96=B0=E5=AE=9A=E4=B9=89=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 5 + src/util/error.rs | 272 +++++++++++++++++++++++++++++++++++++++++ src/util/error_code.rs | 41 +++++++ src/util/error_test.rs | 169 +++++++++++++++++++++++++ src/util/mod.rs | 3 + 5 files changed, 490 insertions(+) create mode 100644 src/util/error.rs create mode 100644 src/util/error_code.rs create mode 100644 src/util/error_test.rs diff --git a/Cargo.toml b/Cargo.toml index be435a3..8e125f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,11 @@ tokio = "1.24.1" jemallocator = "0.5" jemalloc-sys = { version = "0.5", features = ["stats"] } +# error +anyhow = { version = "1.0.65" } +anyerror = { version = "=0.1.8" } +thiserror = { version = "1.0.40" } + [dev-dependencies] criterion = { version = "0.4.0", features = ["html_reports"] } crc32fast = "1.3.2" diff --git a/src/util/error.rs b/src/util/error.rs new file mode 100644 index 0000000..f3d6be2 --- /dev/null +++ b/src/util/error.rs @@ -0,0 +1,272 @@ +// use std::backtrace::{Backtrace, BacktraceStatus}; +// use std::fmt::{Debug, Display, Formatter}; +// use std::io; +// use std::sync::Arc; +// use thiserror::Error; +// +// /// ErrorCodeBacktrace +// #[derive(Clone)] +// pub enum ErrorCodeBacktrace { +// Serialized(Arc), +// Origin(Arc), +// } +// +// impl ToString for ErrorCodeBacktrace { +// fn to_string(&self) -> String { +// match self { +// ErrorCodeBacktrace::Serialized(backtrace) => Arc::as_ref(backtrace).clone(), +// ErrorCodeBacktrace::Origin(backtrace) => { +// format!("{:?}", backtrace) +// } +// } +// } +// } +// +// impl From<&str> for ErrorCodeBacktrace { +// fn from(s: &str) -> Self { +// Self::Serialized(Arc::new(s.to_string())) +// } +// } +// +// impl From for ErrorCodeBacktrace { +// fn from(s: String) -> Self { +// Self::Serialized(Arc::new(s)) +// } +// } +// +// impl From> for ErrorCodeBacktrace { +// fn from(s: Arc) -> Self { +// Self::Serialized(s) +// } +// } +// +// impl From for ErrorCodeBacktrace { +// fn from(bt: Backtrace) -> Self { +// Self::Origin(Arc::new(bt)) +// } +// } +// +// impl From<&Backtrace> for ErrorCodeBacktrace { +// fn from(bt: &Backtrace) -> Self { +// Self::Serialized(Arc::new(bt.to_string())) +// } +// } +// +// impl From> for ErrorCodeBacktrace { +// fn from(bt: Arc) -> Self { +// Self::Origin(bt) +// } +// } +// +// +// /// ErrorCodeBacktrace +// /// Provides the `map_err_to_code` method for `Result`. +// /// +// /// ``` +// /// use crate::util::error::ErrorCode; +// /// use crate::util::error::ToErrorCode; +// /// +// /// let x: std::result::Result<(), std::fmt::Error> = Err(std::fmt::Error {}); +// /// let y: common_exception::Result<()> = x.map_err_to_code(ErrorCode::UnknownException, || 123); +// /// +// /// assert_eq!( +// /// "Code: 1067, Text = 123, cause: an error occurred when formatting an argument.", +// /// y.unwrap_err().to_string() +// /// ); +// /// ``` +// pub trait ToErrorCode +// where E: Display + Send + Sync + 'static +// { +// /// Wrap the error value with ErrorCode. It is lazily evaluated: +// /// only when an error does occur. +// /// +// /// `err_code_fn` is one of the ErrorCode builder function such as `ErrorCode::Ok`. +// /// `context_fn` builds display_text for the ErrorCode. +// fn map_err_to_code(self, err_code_fn: ErrFn, context_fn: CtxFn) -> Result +// where +// ErrFn: FnOnce(String) -> ErrorCode, +// D: Display, +// CtxFn: FnOnce() -> D; +// } +// +// impl ToErrorCode for std::result::Result +// where E: Display + Send + Sync + 'static +// { +// fn map_err_to_code(self, make_exception: ErrFn, context_fn: CtxFn) -> Result +// where +// ErrFn: FnOnce(String) -> ErrorCode, +// D: Display, +// CtxFn: FnOnce() -> D, +// { +// self.map_err(|error| { +// let err_text = format!("{}, cause: {}", context_fn(), error); +// make_exception(err_text) +// }) +// } +// } +// +// +// +// +// +// #[derive(Error)] +// pub struct ErrorCode { +// code: u16, +// display_text: String, +// cause: Option>, +// backtrace: Option, +// } +// +// pub type Result = std::result::Result; +// +// impl ErrorCode { +// pub fn code(&self) -> u16 { +// self.code +// } +// +// pub fn message(&self) -> String { +// self.cause +// .as_ref() +// .map(|cause| format!("{}\n{:?}", self.display_text, cause)) +// .unwrap_or_else(|| self.display_text.clone()) +// } +// +// #[must_use] +// pub fn add_message(self, msg: impl AsRef) -> Self { +// Self { +// display_text: format!("{}\n{}", msg.as_ref(), self.display_text), +// ..self +// } +// } +// +// #[must_use] +// pub fn add_message_back(self, msg: impl AsRef) -> Self { +// Self { +// display_text: format!("{}{}", self.display_text, msg.as_ref()), +// ..self +// } +// } +// +// /// Set backtrace info for this error. +// /// +// /// Useful when trying to keep original backtrace +// pub fn set_backtrace(mut self, bt: Option>) -> Self { +// if let Some(b) = bt { +// self.backtrace = Some(b.into()); +// } +// self +// } +// +// pub fn backtrace(&self) -> Option { +// self.backtrace.clone() +// } +// +// pub fn backtrace_str(&self) -> String { +// self.backtrace +// .as_ref() +// .map_or("".to_string(), |x| x.to_string()) +// } +// } +// +// impl ErrorCode { +// /// All std error will be converted to InternalError +// pub fn from_std_error(error: T) -> Self { +// ErrorCode { +// code: 1001, +// display_text: error.to_string(), +// cause: None, +// backtrace: Some(ErrorCodeBacktrace::Origin(Arc::new(Backtrace::capture()))), +// } +// } +// +// pub fn from_string(error: String) -> Self { +// ErrorCode { +// code: 1001, +// display_text: error, +// cause: None, +// backtrace: Some(ErrorCodeBacktrace::Origin(Arc::new(Backtrace::capture()))), +// } +// } +// +// pub fn from_string_no_backtrace(error: String) -> Self { +// ErrorCode { +// code: 1001, +// display_text: error, +// cause: None, +// backtrace: None, +// } +// } +// +// pub fn create( +// code: u16, +// display_text: String, +// cause: Option>, +// backtrace: Option, +// ) -> ErrorCode { +// ErrorCode { +// code, +// display_text, +// cause, +// backtrace, +// } +// } +// } +// +// impl Debug for ErrorCode { +// fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { +// write!(f, "Code: {}, Text = {}.", self.code(), self.message(),)?; +// +// match self.backtrace.as_ref() { +// None => Ok(()), // no backtrace +// Some(backtrace) => { +// // TODO: Custom stack frame format for print +// match backtrace { +// ErrorCodeBacktrace::Origin(backtrace) => { +// if backtrace.status() == BacktraceStatus::Disabled { +// write!( +// f, +// "\n\n " +// ) +// } else { +// write!(f, "\n\n{}", backtrace) +// } +// } +// ErrorCodeBacktrace::Serialized(backtrace) => write!(f, "\n\n{}", backtrace), +// } +// } +// } +// } +// } +// +// impl Clone for ErrorCode { +// fn clone(&self) -> Self { +// ErrorCode::create(self.code(), self.message(), None, +// self.backtrace()) +// } +// } +// +// impl Display for ErrorCode { +// fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { +// write!(f, "Code: {}, Text = {}.", self.code(), self.message(),) +// } +// } +// +// #[derive(Error, Debug)] +// pub enum StatusError { +// #[error("KOk")] +// KOk(), +// +// #[error("the key `{0}` is not found")] +// KNotFound(String), +// // KCorruption, +// // KNotSupported, +// // KInvalidArgument, +// #[error("data io Error")] +// KIOError(#[from] io::Error), +// // KBadRecord, +// // KRepeatedRecord, +// } +// +// impl StatusError for ErrorCode{ +// +// } \ No newline at end of file diff --git a/src/util/error_code.rs b/src/util/error_code.rs new file mode 100644 index 0000000..9b84166 --- /dev/null +++ b/src/util/error_code.rs @@ -0,0 +1,41 @@ +// +// #![allow(non_snake_case)] +// +// use std::backtrace::Backtrace; +// use std::sync::Arc; +// +// use crate::util::error::ErrorCodeBacktrace; +// use crate::util::error::ErrorCode; +// +// macro_rules! build_exceptions { +// ($($(#[$meta:meta])* $body:ident($code:expr)),*$(,)*) => { +// impl ErrorCode { +// $( +// +// paste::item! { +// $( +// #[$meta] +// )* +// pub const [< $body:snake:upper >]: u16 = $code; +// } +// $( +// #[$meta] +// )* +// pub fn $body(display_text: impl Into) -> ErrorCode { +// let bt = Some(ErrorCodeBacktrace::Origin(Arc::new(Backtrace::capture()))); +// ErrorCode::create( +// $code, +// display_text.into(), +// None, +// bt, +// ) +// } +// )* +// } +// } +// } +// k +// build_exceptions! { +// Ok(0), +// Internal(1001), +// } diff --git a/src/util/error_test.rs b/src/util/error_test.rs new file mode 100644 index 0000000..7dbc8e0 --- /dev/null +++ b/src/util/error_test.rs @@ -0,0 +1,169 @@ +// +// mod test { +// use std::borrow::Borrow; +// use crate::debug; +// use crate::util::r#const::COLON_WHITE_SPACE; +// use crate::util::slice::Slice; +// use crate::util::status::{LevelError, Status}; +// use crate::util::error::{ErrorCode, StatusError}; +// +// #[test] +// fn test_wraper() { +// ErrorCode::Ok; +// let err: StatusError = StatusError::KNotFound("a".to_string()); +// let ok_err: StatusError = StatusError::KOk(); +// +// debug!("{:?}", err.borrow()); +// // assert_eq!("KNotFound("a")", err.borrow()); +// +// // let status = Status::wrapper(LevelError::KIOError, String::from(msg1).into()); +// // assert!(&status.is_io_error()); +// // let slice: Slice = status.into_msg(); +// // assert_eq!("abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc", +// // String::from(slice)); +// // +// // let ss = Status::wrapper(LevelError::KOk, String::from(msg1).into()); +// // assert!(&ss.is_ok()); +// // assert_eq!("OK", &ss.to_string()); +// } +// +// // #[test] +// // fn test_wrappers() { +// // let msg1 = "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc"; +// // let msg2 = "456456456456456456456456456456456456456456456456"; +// // +// // let status = Status::wrappers(LevelError::KIOError, String::from(msg1).into(), String::from(msg2).into()); +// // let slice: Slice = status.into_msg(); +// // assert_eq!("abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc: 456456456456456456456456456456456456456456456456", +// // String::from(slice)); +// // +// // let err: Status = LevelError::invalid_argument(String::from(msg1).into(), +// // String::from(msg2).into()); +// // assert!(&err.is_invalid_argument()); +// // +// // let err: Status = LevelError::corruption(String::from(msg1).into(), +// // String::from(msg2).into()); +// // assert!(&err.is_corruption()); +// // +// // let err1: Status = LevelError::corruption_string("AAaaa", "bbhugy"); +// // assert!(&err1.is_corruption()); +// // +// // let err: Status = LevelError::not_found(String::from(msg1).into(), +// // String::from(msg2).into()); +// // assert!(&err.is_not_found()); +// // +// // let err: Status = LevelError::not_supported(String::from(msg1).into(), +// // String::from(msg2).into()); +// // assert!(&err.is_not_supported_error()); +// // +// // let err: LevelError = LevelError::KOk; +// // assert!(&err.is_ok()); +// // +// // let err: LevelError = LevelError::default(); +// // assert!(&err.is_ok()); +// // } +// // +// // #[test] +// // fn test_is_default() { +// // let err: Status = LevelError::ok(); +// // assert!(err.is_ok()); +// // +// // let err: Status = LevelError::io_error(String::from("a").into(), +// // String::from("b").into()); +// // assert!(!err.is_ok()); +// // +// // let status: Status = LevelError::not_found(String::from("a").into(), +// // String::from("b").into()); +// // assert!(status.is_not_found()); +// // assert!(status.get_error().is_not_found()); +// // } +// // +// // #[test] +// // fn test_status_to_string() { +// // // ok +// // let status: Status = LevelError::ok(); +// // assert_eq!("OK", status.to_string()); +// // +// // let msg1 = "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc\ +// // abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc\ +// // abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc\ +// // abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc\ +// // abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc"; +// // let msg2 = "456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456\ +// // 456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456\ +// // 456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456\ +// // 456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456\ +// // 456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456"; +// // +// // let error: Status = LevelError::invalid_argument(String::from(msg1).into(), +// // String::from(msg2).into()); +// // +// // let binding = error.to_string(); +// // let error_msg = binding.as_str(); +// // println!("{}", error_msg); +// // +// // let expect_string: String = format!("Invalid argument: {}{}{}", String::from(msg1), COLON_WHITE_SPACE, +// // String::from(msg2)); +// // assert_eq!(expect_string, error_msg); +// // } +// // +// // #[test] +// // fn test_level_error_to_string() { +// // // ok +// // let status: Status = LevelError::ok(); +// // assert_eq!("OK", status.to_string()); +// // +// // // err invalid_argument +// // let msg1 = "bcabcabcabcabcabcbc"; +// // let msg2 = "56"; +// // let error: Status = LevelError::invalid_argument(String::from(msg1).into(), +// // String::from(msg2).into()); +// // +// // let le_err: LevelError = error.get_error(); +// // println!("{}", &le_err); +// // +// // // Display +// // assert_eq!(String::from("Invalid argument: "), le_err.to_string()); +// // } +// // +// // #[test] +// // fn test_level_error_try_from() -> Result<(), String> { +// // let rs = LevelError::try_from(1)?; +// // assert!(&rs.is_not_found()); +// // assert_eq!(rs.get_value(), 1); +// // let rs: Result = 1.try_into(); +// // assert!(rs.ok().unwrap().is_not_found()); +// // +// // let rs = LevelError::try_from(0)?; +// // assert!(&rs.is_ok()); +// // assert_eq!(rs.get_value(), 0); +// // let rs: Result = 0.try_into(); +// // assert!(rs.ok().unwrap().is_ok()); +// // +// // let rs = LevelError::try_from(2)?; +// // assert!(&rs.is_corruption()); +// // assert_eq!(rs.get_value(), 2); +// // let rs: LevelError = 2.try_into()?; +// // assert!(rs.is_corruption()); +// // +// // let rs: LevelError = LevelError::try_from(3)?; +// // assert!(&rs.is_not_supported_error()); +// // assert_eq!(rs.get_value(), 3); +// // let rs: LevelError = 3.try_into()?; +// // assert!(rs.is_not_supported_error()); +// // +// // let rs = LevelError::try_from(4)?; +// // assert!(&rs.is_invalid_argument()); +// // assert_eq!(rs.get_value(), 4); +// // +// // let rs = LevelError::try_from(5)?; +// // assert!(&rs.is_io_error()); +// // assert_eq!(rs.get_value(), 5); +// // +// // let rs = LevelError::try_from(66); +// // assert_eq!("Unknown code: 66", rs.err().unwrap()); +// // +// // Ok(()) +// // } +// +// } diff --git a/src/util/mod.rs b/src/util/mod.rs index 53143b2..289ca42 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -47,6 +47,9 @@ pub mod unsafe_slice; pub mod env; mod env_test; pub mod mem_debug; +// pub mod error; +// mod error_test; +// pub mod error_code; /// 定义别名 pub type Result = result::Result; -- Gitee