Skip to content

Commit

Permalink
feat(Error): 拓展异常处理
Browse files Browse the repository at this point in the history
  • Loading branch information
hubertshelley committed Jun 20, 2024
1 parent cdcaedd commit d46dfa2
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 30 deletions.
4 changes: 2 additions & 2 deletions examples/candle_whisper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_tokio_listener/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
2 changes: 1 addition & 1 deletion examples/custom_tokio_runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
2 changes: 1 addition & 1 deletion examples/exception_handler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ edition = "2021"

[dependencies]
silent = { path = "../../silent" }
serde = { version = "1.0.202", features = ["derive"] }
serde = { version = "1.0.203", features = ["derive"] }
2 changes: 1 addition & 1 deletion examples/form/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ edition = "2021"

[dependencies]
silent = { path = "../../silent" }
serde = { version = "1.0.202", features = ["derive"] }
serde = { version = "1.0.203", features = ["derive"] }
6 changes: 3 additions & 3 deletions examples/grpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
6 changes: 3 additions & 3 deletions examples/grpc_h2c/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 3 additions & 3 deletions examples/grpc_streaming/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion examples/multipart-form/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
2 changes: 1 addition & 1 deletion examples/sse-chat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
2 changes: 1 addition & 1 deletion examples/templates/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
2 changes: 1 addition & 1 deletion examples/todo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion examples/websocket-chat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 3 additions & 3 deletions examples/websocket/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
14 changes: 7 additions & 7 deletions silent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 }
Expand Down
27 changes: 27 additions & 0 deletions silent/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,33 @@ pub enum SilentError {

pub type SilentResult<T> = Result<T, SilentError>;

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<String> for SilentError {
fn from(value: String) -> Self {
Self::business_error(StatusCode::INTERNAL_SERVER_ERROR, value)
}
}

impl From<BoxedError> for SilentError {
fn from(value: BoxedError) -> Self {
Self::business_error(StatusCode::INTERNAL_SERVER_ERROR, value.to_string())
}
}

impl SilentError {
pub fn business_error_obj<S>(code: StatusCode, msg: S) -> Self
where
Expand Down

0 comments on commit d46dfa2

Please sign in to comment.