Skip to content

Commit

Permalink
fountain/ur: tweak error messages
Browse files Browse the repository at this point in the history
 - Prepend with context if applicable.

 - Consistently start lower-cased.

 - Test more formattings.
  • Loading branch information
dspicher committed Feb 6, 2025
1 parent 1d01fc7 commit dda3c7f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
12 changes: 10 additions & 2 deletions src/fountain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ pub enum Error {
impl core::fmt::Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::CborDecode(e) => write!(f, "{e}"),
Self::CborEncode(e) => write!(f, "{e}"),
Self::CborDecode(e) => write!(f, "minicbor decoding error: {e}"),
Self::CborEncode(e) => write!(f, "minicbor encoding error: {e}"),
Self::EmptyMessage => write!(f, "expected non-empty message"),
Self::EmptyPart => write!(f, "expected non-empty part"),
Self::InvalidFragmentLen => write!(f, "expected positive maximum fragment length"),
Expand Down Expand Up @@ -1102,6 +1102,14 @@ mod tests {

#[test]
fn test_error_formatting() {
assert_eq!(
super::Error::from(minicbor::decode::Error::end_of_input()).to_string(),
"minicbor decoding error: end of input bytes"
);
assert_eq!(
super::Error::from(minicbor::encode::Error::message("error")).to_string(),
"minicbor encoding error: error"
);
assert_eq!(
super::Error::EmptyMessage.to_string(),
"expected non-empty message"
Expand Down
32 changes: 20 additions & 12 deletions src/ur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ pub enum Error {
impl core::fmt::Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Bytewords(e) => write!(f, "{e}"),
Self::Fountain(e) => write!(f, "{e}"),
Self::InvalidScheme => write!(f, "Invalid scheme"),
Self::TypeUnspecified => write!(f, "No type specified"),
Self::InvalidCharacters => write!(f, "Type contains invalid characters"),
Self::InvalidIndices => write!(f, "Invalid indices"),
Self::NotMultiPart => write!(f, "Can't decode single-part UR as multi-part"),
Self::Bytewords(e) => write!(f, "bytewords: {e}"),
Self::Fountain(e) => write!(f, "fountain: {e}"),
Self::InvalidScheme => write!(f, "invalid scheme"),
Self::TypeUnspecified => write!(f, "no type specified"),
Self::InvalidCharacters => write!(f, "type contains invalid characters"),
Self::InvalidIndices => write!(f, "invalid indices"),
Self::NotMultiPart => write!(f, "can't decode single-part UR as multi-part"),
}
}
}
Expand Down Expand Up @@ -468,19 +468,27 @@ mod tests {

#[test]
fn test_error_formatting() {
assert_eq!(super::Error::InvalidScheme.to_string(), "Invalid scheme");
assert_eq!(
super::Error::from(crate::bytewords::Error::InvalidChecksum).to_string(),
"bytewords: invalid checksum"
);
assert_eq!(
super::Error::from(crate::fountain::Error::EmptyPart).to_string(),
"fountain: expected non-empty part"
);
assert_eq!(super::Error::InvalidScheme.to_string(), "invalid scheme");
assert_eq!(
super::Error::TypeUnspecified.to_string(),
"No type specified"
"no type specified"
);
assert_eq!(
super::Error::InvalidCharacters.to_string(),
"Type contains invalid characters"
"type contains invalid characters"
);
assert_eq!(super::Error::InvalidIndices.to_string(), "Invalid indices");
assert_eq!(super::Error::InvalidIndices.to_string(), "invalid indices");
assert_eq!(
super::Error::NotMultiPart.to_string(),
"Can't decode single-part UR as multi-part"
"can't decode single-part UR as multi-part"
);
}
}

0 comments on commit dda3c7f

Please sign in to comment.