Skip to content
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

Add InvalidLengthError #40

Closed
wants to merge 1 commit into from

Conversation

tcharding
Copy link
Member

@tcharding tcharding commented Oct 27, 2023

Draft for discussion. This wraps the internals of the HexToArrayError as suggested in #13.

Note: Please don't take this as argumentative, the purpose is to get right to the bottom of things so once we start executing whatever we decide on its done right. Remember, we are setting precedent for all the other crates in the rust-bitcoin org.

I personally think this is going too far for a two reasons, primarily (1)

  1. This does not save us from having to do a major release when we add new data to the error types because most likely that will lead to a change in the Display output which is a major breaking change.
  2. We already have quite a lot of error boilerplate, admittedly this is write-once code but still it is there, its still a liability. This new wrapping style introduces loads more boilerplate, which means loads of work, which likely no one else is going to do except me - and its boring as bat shit. If we are going to go for this I'd like everyone to be really confident its worth it. Also everytime we have to make a tiny change to some stylistic error thing it has to be done in many places, and I never get it right first time.

In order to make adding more information to our error types easier on
downstream hide the internals of the invalid length error.
@Kixunil
Copy link
Collaborator

Kixunil commented Oct 27, 2023

1

Adressed in #13 TL;DR: not an issue

2

I believe it's worth it and we don't really have any alternative other than wrapping whole error and disclosing all the details later or just getting everything right the first time.

I'm perfectly willing to write that boilerplate, I'm just currently doing some more urgent things unrelated to this crate.

@Kixunil
Copy link
Collaborator

Kixunil commented Oct 27, 2023

Oh, actually we may have to wrap the top-level error as well to support multiple errors (unless we want to split the API, which I'd prefer not to).

Roughly:

// impl Error such that only first is considered and it's transparent
pub struct Error {
    first: HexError,
    // We could also do clever things with Vec to make the error type smaller but that's for another day
    // Note that we don't do internal enforcing of sanity (preventing duplicates) because it'd make the API awful.
    #[cfg(feature = "multi-error")]
    second: Option<HexError>,
}

impl Error {
    pub fn first(&self) -> &HexError {
        &self.first
    }

    pub fn into_first(self) -> HexError {
        self.first
    }

    pub fn iter(&self) -> impl Iterator<Item = &HexError> {
        core::iter::once(&self.first)
            #[cfg(feature = "multi-error")]
            .chain(self.second.as_ref())
    }
    // into_iter as well

    pub fn into_vec(self) -> Vec<HexError> {
        #[cfg(feature = "multi-error")]
        match self.second {
            Some(second) => vec![self.first, second],
            None => vec![self.first],
        }
        #[cfg(not(feature = "multi-error"))]
        vec![self.first]
    }
}

// impl Error
pub enum HexError {
    OddLength(OddLengthError),
    InvalidChars(InvalidChars),
}

@tcharding
Copy link
Member Author

I'm perfectly willing to write that boilerplate, I'm just currently doing some more urgent things unrelated to this crate.

Ok, I'm going to leave the error handling in this crate to you. I'm guessing the snippet above goes beyond what will need doing in other crates since you have an idea of what is going to be done here in future, where as I was just trying to come up with a precedent to apply to all the other crates (initially rust-bech32 and hashes since they, along with this one, are likely the first to stabalise).

@tcharding
Copy link
Member Author

Closing in favour of #42

@tcharding tcharding closed this Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants