Attempt to change the way errors are created
Some checks failed
Build library & run tests / docs (push) Failing after 2m31s
Build library & run tests / build (tcp) (push) Failing after 2m41s
Build library & run tests / build (unix) (push) Failing after 2m42s

This commit is contained in:
Kodi Craft 2024-07-05 13:09:31 +02:00
parent ef1faf5bd9
commit a5c975d113
Signed by: kodi
GPG Key ID: 69D9EED60B242822
3 changed files with 25 additions and 29 deletions

21
Cargo.lock generated
View File

@ -306,6 +306,7 @@ dependencies = [
"ron", "ron",
"serde", "serde",
"syn", "syn",
"thiserror",
"tokio", "tokio",
"tokio-test", "tokio-test",
] ]
@ -973,6 +974,26 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160"
[[package]]
name = "thiserror"
version = "1.0.61"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.61"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "thread_local" name = "thread_local"
version = "1.1.8" version = "1.1.8"

View File

@ -29,6 +29,7 @@ tokio-test = "0.4.4"
env_logger = { version = "0.11.3" } env_logger = { version = "0.11.3" }
log = { version = "0.4.21" } log = { version = "0.4.21" }
console-subscriber = "0.3.0" console-subscriber = "0.3.0"
thiserror = "1.0.61"
[lib] [lib]
proc-macro = true proc-macro = true

View File

@ -352,39 +352,13 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
// Create an error and result type for sending messages // Create an error and result type for sending messages
let error_enum = quote! { let error_enum = quote! {
#[derive(::std::fmt::Debug)] #[derive(::std::fmt::Debug, ::std::clone::Clone, ::std::fmt::Display, ::thiserror::Error)]
#vis enum #error_enum_name { #vis enum #error_enum_name {
#[error("Failed to send query: {0}")]
SendError(::tokio::sync::mpsc::error::SendError<(u64, #question_enum_name)>), SendError(::tokio::sync::mpsc::error::SendError<(u64, #question_enum_name)>),
#[error("Connection to sender thread closed")]
Closed, Closed,
} }
impl ::std::fmt::Display for #error_enum_name {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
match self {
#error_enum_name::SendError(e) => write!(f, "Failed to send query: {}", e),
#error_enum_name::Closed => write!(f, "Connection closed"),
}
}
}
impl ::std::error::Error for #error_enum_name {
fn source(&self) -> ::std::option::Option<&(dyn ::std::error::Error + 'static)> {
match self {
#error_enum_name::SendError(e) => ::std::option::Option::Some(e),
#error_enum_name::Closed => ::std::option::Option::None,
}
}
fn description(&self) -> &str {
match self {
#error_enum_name::SendError(_) => "Failed to send query",
#error_enum_name::Closed => "Connection closed",
}
}
fn cause(&self) -> ::std::option::Option<&dyn ::std::error::Error> {
match self {
#error_enum_name::SendError(e) => ::std::option::Option::Some(e),
#error_enum_name::Closed => ::std::option::Option::None,
}
}
}
}; };
// Create enums for the types of messages the server and client will use // Create enums for the types of messages the server and client will use