Skip to content

Commit

Permalink
Better error
Browse files Browse the repository at this point in the history
Signed-off-by: Xavier Lau <[email protected]>
  • Loading branch information
aurexav committed Dec 6, 2023
1 parent 011a071 commit ba46529
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
16 changes: 8 additions & 8 deletions subcryptor/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,32 @@ use thiserror::Error as ThisError;
#[allow(missing_docs)]
#[derive(Debug, ThisError)]
pub enum Error {
#[error("{0:?}")]
#[error("[subcryptor] {0:?}")]
ArrayBytes(array_bytes::Error),
#[error(transparent)]
Base64Decode(#[from] base64::DecodeError),
#[error(transparent)]
CryptoSecretBox(#[from] CryptoSecretBox),
#[error("{0:?}")]
#[error("[subcryptor] from base58 error, {0:?}")]
FromBase58(base58::FromBase58Error),
#[error("invalid prefix, {0:?}")]
#[error("[subcryptor] invalid prefix, {0:?}")]
InvalidPrefix(u8),
#[error("invalid ss58 address, {0:?}")]
#[error("[subcryptor] invalid ss58 address, {0:?}")]
InvalidSs58Address(String),
#[error(transparent)]
Scrypt(#[from] scrypt::errors::InvalidOutputLen),
#[error("unsupported encryption type")]
#[error("[subcryptor] unsupported encryption type")]
UnsupportedEncryptionType,
#[error("unsupported network, {0:?}")]
#[error("[subcryptor] unsupported network, {0:?}")]
UnsupportedNetwork(String),
}

/// Crypto secretbox error.
#[allow(missing_docs)]
#[derive(Debug, ThisError)]
pub enum CryptoSecretBox {
#[error("{0:?}")]
#[error("[subcryptor] crypto secretbox general error, {0:?}")]
General(crypto_secretbox::Error),
#[error("{0:?}")]
#[error("[subcryptor] crypto secretbox cipher error, {0:?}")]
Cipher(crypto_secretbox::cipher::InvalidLength),
}
13 changes: 8 additions & 5 deletions subcryptor/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn ss58_address_of_should_work() {
fn ss58_address_of_should_fail() {
assert_eq!(
ss58_address_of(&[], "invalid network").unwrap_err().to_string(),
"unsupported network, \"invalid network\""
"[subcryptor] unsupported network, \"invalid network\""
);
}

Expand All @@ -31,21 +31,24 @@ fn public_key_of_should_work() {

#[test]
fn public_key_of_should_fail() {
assert_eq!(public_key_of::<Sr25519>("").unwrap_err().to_string(), "invalid ss58 address, \"\"");
assert_eq!(
public_key_of::<Sr25519>("").unwrap_err().to_string(),
"[subcryptor] invalid ss58 address, \"\""
);
assert_eq!(
public_key_of::<Sr25519>("?56HGo9setPcU2qhFMVWLkcmtCEGySLwNqa3DaEiYSWtte4Y")
.unwrap_err()
.to_string(),
"InvalidBase58Character('?', 0)"
"[subcryptor] from base58 error, InvalidBase58Character('?', 0)"
);
assert_eq!(
public_key_of::<Sr25519>("56HGo9setPcU2qhFMVWLkcmtCEGySLwNqa3DaEiYSWtte4Y")
.unwrap_err()
.to_string(),
"invalid prefix, 180"
"[subcryptor] invalid prefix, 180"
);
assert_eq!(
public_key_of::<Sr25519>("15").unwrap_err().to_string(),
"invalid ss58 address, \"15\""
"[subcryptor] invalid ss58 address, \"15\""
);
}

0 comments on commit ba46529

Please sign in to comment.