Compare commits

..

2 Commits

Author SHA1 Message Date
a5c975d113
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
2024-07-05 13:09:31 +02:00
ef1faf5bd9
Barely better error reporting 2024-06-26 11:28:14 +02:00
3 changed files with 32 additions and 34 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
@ -565,21 +539,21 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
} }
pub fn insert(&self, nonce: u64, query: #query_enum_name) { pub fn insert(&self, nonce: u64, query: #query_enum_name) {
self.queries.lock().unwrap().insert(nonce, query); self.queries.lock().expect("Could not insert new query; Mutex has been poisoned.").insert(nonce, query);
} }
pub fn get(&self, nonce: &u64) -> ::std::option::Option<#query_enum_name> { pub fn get(&self, nonce: &u64) -> ::std::option::Option<#query_enum_name> {
self.queries.lock().unwrap().get(nonce).cloned() self.queries.lock().expect("Could not get query; Mutex has been poisoned.").get(nonce).cloned()
} }
pub fn set_answer(&self, nonce: u64, answer: #answer_enum_name) { pub fn set_answer(&self, nonce: u64, answer: #answer_enum_name) {
if let ::std::option::Option::Some(query) = self.queries.lock().unwrap().get_mut(&nonce) { if let ::std::option::Option::Some(query) = self.queries.lock().expect("Could not set answer to query; Mutex has been poisoned.").get_mut(&nonce) {
query.set_answer(answer); query.set_answer(answer);
} }
} }
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.queries.lock().unwrap().len() self.queries.lock().expect("Could not get query count; Mutex has been poisoned.").len()
} }
} }
}; };
@ -638,7 +612,9 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
} else { } else {
match self.received.send(response).await { match self.received.send(response).await {
::std::result::Result::Ok(_) => {}, ::std::result::Result::Ok(_) => {},
::std::result::Result::Err(e) => #error("Failed to send received answer to : {:?}", e), ::std::result::Result::Err(e) => {
#error("Failed to send received answer to Client: {:?}", e);
},
}; };
} }
buf.drain(0..(4 + len as usize)); buf.drain(0..(4 + len as usize));