-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: function! macro for error context #317
Conversation
Deletes any now-superfluous `const NAME: &'static str = "name";` definitions. This gives a path to the function that is being called, which is useful for debugging errors. It replaces the necessity of implementing `const NAME: &'static str = "name";` in every function that wants to use these macros (or coming up with a custom context).
I explored this approach in the past and decided to not use it at the time. More specifically, I used a similar trick to find the name of the type instead of the name of the function. A minor inconvenience is that the actual representation is more verbose, not guaranteed to be stable, and some tests may break from time to time because of that. IMO, this is not a big deal because it’s easy to regenerate these (we are using An alternative I was thinking about is: /// A type with a static name.
pub trait StaticName {
/// Name associated to this type.
const NAME: &'static str;
}
/// A type from which a name can be retrieved.
pub trait GetName {
/// Returns the name associated to this value.
fn get_name(&self) -> &'static str;
}
impl<T: StaticName> GetName for T {
fn get_name(&self) -> &'static str {
T::NAME
}
}
assert_obj_safe!(GetName);
/// Gets the name of this value.
pub fn get_name<T: GetName>(value: &T) -> &'static str {
value.get_name()
}
macro_rules! impl_static_name {
($ty:ident) => {{
impl StaticName for $ty {
const NAME: &'static str = stringify!($ty);
}
}};
}
struct Example;
impl_static_name!(Example); And this could be part of another macro that we should typically apply on most decodable / encodable types, so we would get it for "free" as well. This also avoid copy-paste errors when using The benefit is that we get a shorter name, and we have the On the other hand, having the name of the function is not bad either. I’m not yet settled on either one as of today. Thoughts? |
@CBenoit it may be worth clarifying precisely what we want to get from this
In the Personally when I hit any error, I'm looking for
From those axioms I would imagine a "base class" that contains that information, and any "subclasses" (like With the use of this A key advantage I see to That's a long rambling take, I could say more but I want to hear your take on the proper meaning of |
Sure, I think clarification is needed too!
I would argue that the "invalid payload" in
This is in line with my vision.
Elaborating on what you said, here is how I imagined the error handling story in IronRDP: 1. is addressed by In addition to all that,
This is the original intent. For PDUs, the context field is supposed to hold the name of the PDU being decoded or encoded, so it’s easy to find which part of the code is raising the error.
However, I’ve been wanting to revisit all that, but didn’t had the chance yet. I have a WIP branch with some experiments though. (That’s the branch where I originally experimented with All that being said, I think I would like |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approving and enabling auto-merge
function!
macrod79d9fc
to
e058169
Compare
Coverage Report 🤖 ⚙️Past: New: Diff: -0.00% [this comment will be updated automatically] |
@zmb3 suggested we add this for Teleport to avoid copy-paste errors wherever we're using these macros, however it occurred to me that this might best fit as a more general solution throughout IronRDP.
I tested this by applying the following patch
and running a session (via teleport), which results in a log message like
the key piece being
context: "<ironrdp_cliprdr::pdu::capabilities::CapabilitySet as ironrdp_pdu::PduDecode>::decode"