From d46dfa28129ec4180c460b5114495ece1a35ce1f Mon Sep 17 00:00:00 2001 From: hubertshelley Date: Thu, 20 Jun 2024 09:48:18 +0800 Subject: [PATCH] =?UTF-8?q?feat(Error):=20=E6=8B=93=E5=B1=95=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/candle_whisper/Cargo.toml | 4 ++-- examples/custom_tokio_listener/Cargo.toml | 2 +- examples/custom_tokio_runtime/Cargo.toml | 2 +- examples/exception_handler/Cargo.toml | 2 +- examples/form/Cargo.toml | 2 +- examples/grpc/Cargo.toml | 6 ++--- examples/grpc_h2c/Cargo.toml | 6 ++--- examples/grpc_streaming/Cargo.toml | 6 ++--- examples/multipart-form/Cargo.toml | 2 +- examples/sse-chat/Cargo.toml | 2 +- examples/templates/Cargo.toml | 2 +- examples/todo/Cargo.toml | 2 +- examples/websocket-chat/Cargo.toml | 2 +- examples/websocket/Cargo.toml | 6 ++--- silent/Cargo.toml | 14 ++++++------ silent/src/error/mod.rs | 27 +++++++++++++++++++++++ 16 files changed, 57 insertions(+), 30 deletions(-) diff --git a/examples/candle_whisper/Cargo.toml b/examples/candle_whisper/Cargo.toml index d38267a..1c7ecf1 100644 --- a/examples/candle_whisper/Cargo.toml +++ b/examples/candle_whisper/Cargo.toml @@ -21,12 +21,12 @@ bindgen_cuda = { version = "0.1.5", optional = true } [dependencies] -clap = { version = "4.5.4", features = ["derive"] } +clap = { version = "4.5.7", features = ["derive"] } serde = { version = "1.0", features = ["derive"] } silent = { path = "../../silent", features = ["multipart"] } symphonia = { version = "0.5.4", features = ["all"] } anyhow = "1.0.86" -tokio = { version = "1.37.0", features = ["full"] } +tokio = { version = "1.38.0", features = ["full"] } #candle-core = { version = "0.3.2" } #candle-nn = { version = "0.3.2" } diff --git a/examples/custom_tokio_listener/Cargo.toml b/examples/custom_tokio_listener/Cargo.toml index b2df581..58527bc 100644 --- a/examples/custom_tokio_listener/Cargo.toml +++ b/examples/custom_tokio_listener/Cargo.toml @@ -8,4 +8,4 @@ edition = "2021" [dependencies] silent = { path = "../../silent" } async-trait = "0.1.80" -tokio = { version = "1.37.0", features = ["full"] } +tokio = { version = "1.38.0", features = ["full"] } diff --git a/examples/custom_tokio_runtime/Cargo.toml b/examples/custom_tokio_runtime/Cargo.toml index 87f32a2..58c97d0 100644 --- a/examples/custom_tokio_runtime/Cargo.toml +++ b/examples/custom_tokio_runtime/Cargo.toml @@ -8,4 +8,4 @@ edition = "2021" [dependencies] silent = { path = "../../silent" } async-trait = "0.1.80" -tokio = { version = "1.37.0", features = ["full"] } +tokio = { version = "1.38.0", features = ["full"] } diff --git a/examples/exception_handler/Cargo.toml b/examples/exception_handler/Cargo.toml index 6d0fa02..41ee57b 100644 --- a/examples/exception_handler/Cargo.toml +++ b/examples/exception_handler/Cargo.toml @@ -7,4 +7,4 @@ edition = "2021" [dependencies] silent = { path = "../../silent" } -serde = { version = "1.0.202", features = ["derive"] } +serde = { version = "1.0.203", features = ["derive"] } diff --git a/examples/form/Cargo.toml b/examples/form/Cargo.toml index 8d78a04..7938132 100644 --- a/examples/form/Cargo.toml +++ b/examples/form/Cargo.toml @@ -7,4 +7,4 @@ edition = "2021" [dependencies] silent = { path = "../../silent" } -serde = { version = "1.0.202", features = ["derive"] } +serde = { version = "1.0.203", features = ["derive"] } diff --git a/examples/grpc/Cargo.toml b/examples/grpc/Cargo.toml index c283f6d..1b575e8 100644 --- a/examples/grpc/Cargo.toml +++ b/examples/grpc/Cargo.toml @@ -16,17 +16,17 @@ path = "src/client.rs" tonic = { git = "https://github.com/alexrudy/tonic", branch = "hyper-1.0" } tonic-reflection = { git = "https://github.com/alexrudy/tonic", branch = "hyper-1.0" } prost = "0.12" -tokio = { version = "1.37", features = ["macros", "rt-multi-thread"] } +tokio = { version = "1.38", features = ["macros", "rt-multi-thread"] } silent = { path = "../../silent", features = ["grpc"] } axum = "0.7" async-trait = "0.1.80" hyper = "1.3.1" -hyper-util = "0.1.3" +hyper-util = "0.1.5" bytes = "1.6.0" pin-project-lite = "0.2.14" http-body = "1.0.0" http = "1.1.0" -http-body-util = "0.1.1" +http-body-util = "0.1.2" [build-dependencies] tonic-build = { git = "https://github.com/alexrudy/tonic", branch = "hyper-1.0" } diff --git a/examples/grpc_h2c/Cargo.toml b/examples/grpc_h2c/Cargo.toml index ee0afb3..00e7e92 100644 --- a/examples/grpc_h2c/Cargo.toml +++ b/examples/grpc_h2c/Cargo.toml @@ -16,17 +16,17 @@ path = "src/client.rs" tonic = { git = "https://github.com/alexrudy/tonic", branch = "hyper-1.0" } tonic-reflection = { git = "https://github.com/alexrudy/tonic", branch = "hyper-1.0" } prost = "0.12" -tokio = { version = "1.37", features = ["macros", "rt-multi-thread"] } +tokio = { version = "1.38", features = ["macros", "rt-multi-thread"] } silent = { path = "../../silent", features = ["grpc"] } axum = "0.7" async-trait = "0.1.80" hyper = "1.3.1" -hyper-util = "0.1.3" +hyper-util = "0.1.5" bytes = "1.6.0" pin-project-lite = "0.2.14" http-body = "1.0.0" http = "1.1.0" -http-body-util = "0.1.1" +http-body-util = "0.1.2" tower = "0.4.13" [build-dependencies] diff --git a/examples/grpc_streaming/Cargo.toml b/examples/grpc_streaming/Cargo.toml index ba9d33c..314da1b 100644 --- a/examples/grpc_streaming/Cargo.toml +++ b/examples/grpc_streaming/Cargo.toml @@ -16,17 +16,17 @@ path = "src/client.rs" tonic = { git = "https://github.com/alexrudy/tonic", branch = "hyper-1.0" } tonic-reflection = { git = "https://github.com/alexrudy/tonic", branch = "hyper-1.0" } prost = "0.12" -tokio = { version = "1.37", features = ["macros", "rt-multi-thread"] } +tokio = { version = "1.38", features = ["macros", "rt-multi-thread"] } silent = { path = "../../silent", features = ["grpc"] } axum = "0.7" async-trait = "0.1.80" hyper = "1.3.1" -hyper-util = "0.1.3" +hyper-util = "0.1.5" bytes = "1.6.0" pin-project-lite = "0.2.14" http-body = "1.0.0" http = "1.1.0" -http-body-util = "0.1.1" +http-body-util = "0.1.2" tokio-stream = "0.1.15" h2 = "0.4.5" diff --git a/examples/multipart-form/Cargo.toml b/examples/multipart-form/Cargo.toml index 6bd82c1..9d36b9e 100644 --- a/examples/multipart-form/Cargo.toml +++ b/examples/multipart-form/Cargo.toml @@ -7,4 +7,4 @@ edition = "2021" [dependencies] silent = { path = "../../silent", features = ["multipart"] } -serde = { version = "1.0.202", features = ["derive"] } +serde = { version = "1.0.203", features = ["derive"] } diff --git a/examples/sse-chat/Cargo.toml b/examples/sse-chat/Cargo.toml index 3ae28d9..ac0b336 100644 --- a/examples/sse-chat/Cargo.toml +++ b/examples/sse-chat/Cargo.toml @@ -11,4 +11,4 @@ once_cell = "1" parking_lot = "0.12" tokio = { version = "1", features = ["macros"] } tokio-stream = { version = "0.1", features = ["net"] } -serde = { version = "1.0.202", features = ["derive"] } +serde = { version = "1.0.203", features = ["derive"] } diff --git a/examples/templates/Cargo.toml b/examples/templates/Cargo.toml index 8889daa..75350c9 100644 --- a/examples/templates/Cargo.toml +++ b/examples/templates/Cargo.toml @@ -6,5 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -serde = { version = "1.0.202", features = ["derive"] } +serde = { version = "1.0.203", features = ["derive"] } silent = { path = "../../silent", features = ["template"] } diff --git a/examples/todo/Cargo.toml b/examples/todo/Cargo.toml index 864ebed..cdff445 100644 --- a/examples/todo/Cargo.toml +++ b/examples/todo/Cargo.toml @@ -7,6 +7,6 @@ edition = "2021" [dependencies] silent = { path = "../../silent" } -serde = { version = "1.0.202", features = ["derive"] } +serde = { version = "1.0.203", features = ["derive"] } uuid = { version = "1.8.0", features = ["serde", "v4"] } async-trait = "0.1.80" diff --git a/examples/websocket-chat/Cargo.toml b/examples/websocket-chat/Cargo.toml index 140ea86..2eb2460 100644 --- a/examples/websocket-chat/Cargo.toml +++ b/examples/websocket-chat/Cargo.toml @@ -7,7 +7,7 @@ publish = false [dependencies] silent = { path = "../../silent", features = ["upgrade"] } -tokio = { version = "1.37.0", features = ["full"] } +tokio = { version = "1.38.0", features = ["full"] } tokio-stream = { version = "0.1.15", features = ["net"] } futures-util = { version = "0.3.30", default-features = false } once_cell = "1.19.0" diff --git a/examples/websocket/Cargo.toml b/examples/websocket/Cargo.toml index 1d5598a..0a3db86 100644 --- a/examples/websocket/Cargo.toml +++ b/examples/websocket/Cargo.toml @@ -8,8 +8,8 @@ edition = "2021" [dependencies] async-trait = "0.1.80" silent = { path = "../../silent", features = ["upgrade"] } -tokio = { version = "1.37.0", features = ["full"] } -tokio-tungstenite = "0.23.0" +tokio = { version = "1.38.0", features = ["full"] } +tokio-tungstenite = "0.23.1" futures-util = "0.3.30" -backtrace = "0.3.71" +backtrace = "0.3.73" headers = "0.4.0" diff --git a/silent/Cargo.toml b/silent/Cargo.toml index cab15a0..a29092e 100644 --- a/silent/Cargo.toml +++ b/silent/Cargo.toml @@ -36,22 +36,22 @@ grpc = ["upgrade", "dep:axum", "dep:tower-service", "dep:pin-project-lite", "dep # Basic dependencies thiserror = "1.0.61" hyper = { version = "1.3.1", features = ["full"] } -hyper-util = { version = "0.1.3", features = ["server-auto", "tokio"] } -tokio = { version = "1.37.0", optional = true } +hyper-util = { version = "0.1.5", features = ["server-auto", "tokio"] } +tokio = { version = "1.38.0", optional = true } bytes = "1.6.0" -http-body-util = "0.1.1" +http-body-util = "0.1.2" tracing = "0.1.40" tracing-subscriber = { version = "0.3.18", features = ["local-time"] } async-trait = "0.1.80" -serde = { version = "1.0.202", features = ["derive"] } +serde = { version = "1.0.203", features = ["derive"] } serde_json = "1.0.117" uuid = "1.8.0" -url = "2.5.0" +url = "2.5.2" serde_urlencoded = "0.7.1" mime = "0.3.17" futures-util = "0.3.30" chrono = { version = "0.4.38", default-features = false, features = ["clock"] } -tokio-tungstenite = { version = "0.23.0", optional = true } +tokio-tungstenite = { version = "0.23.1", optional = true } headers = "0.4.0" tokio-stream = { version = "0.1.15", features = ["net"], optional = true } pin-project = { version = "1.1", optional = true } @@ -73,7 +73,7 @@ textnonce = { version = "1.0.0", optional = true } # Template -tera = { version = "1.19.1", optional = true } +tera = { version = "1.20.0", optional = true } # Session async-session = { version = "3.0.0", optional = true } diff --git a/silent/src/error/mod.rs b/silent/src/error/mod.rs index 3e7c2ba..ca5fc60 100644 --- a/silent/src/error/mod.rs +++ b/silent/src/error/mod.rs @@ -73,6 +73,33 @@ pub enum SilentError { pub type SilentResult = Result; +impl From<(StatusCode, String)> for SilentError { + fn from(value: (StatusCode, String)) -> Self { + Self::business_error(value.0, value.1) + } +} + +impl From<(u16, String)> for SilentError { + fn from(value: (u16, String)) -> Self { + Self::business_error( + StatusCode::from_u16(value.0).expect("invalid status code"), + value.1, + ) + } +} + +impl From for SilentError { + fn from(value: String) -> Self { + Self::business_error(StatusCode::INTERNAL_SERVER_ERROR, value) + } +} + +impl From for SilentError { + fn from(value: BoxedError) -> Self { + Self::business_error(StatusCode::INTERNAL_SERVER_ERROR, value.to_string()) + } +} + impl SilentError { pub fn business_error_obj(code: StatusCode, msg: S) -> Self where