Skip to content

Commit d2e0e35

Browse files
committed
error refactoring
1 parent 647df49 commit d2e0e35

File tree

3 files changed

+40
-56
lines changed

3 files changed

+40
-56
lines changed

rust/cryptonote/src/crypto.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ pub fn encrypt_symmetric(
7474
chacha20poly1305::Nonce::from_slice(&nonce);
7575
cipher
7676
.encrypt(nonce_array, plaintext)
77-
.map_err(AppError::ChaChaPoly1305)?
77+
.map_err(|_| AppError::Encrypt)?
7878
}
7979
CipherType::Aes256Gcm => {
8080
let cipher = Aes256Gcm::new_from_slice(&key)?;
8181
let nonce_array =
8282
aes_gcm::Nonce::from_slice(&nonce);
8383
cipher
8484
.encrypt(nonce_array, plaintext)
85-
.map_err(AppError::AesGcm)?
85+
.map_err(|_| AppError::Encrypt)?
8686
}
8787
};
8888

@@ -115,7 +115,7 @@ pub fn decrypt_symmetric(
115115
nonce_array,
116116
data.ciphertext.as_ref(),
117117
)
118-
.map_err(AppError::ChaChaPoly1305)
118+
.map_err(|_| AppError::Decrypt)
119119
}
120120
CipherType::Aes256Gcm => {
121121
let cipher = Aes256Gcm::new_from_slice(&key)?;
@@ -126,7 +126,7 @@ pub fn decrypt_symmetric(
126126
nonce_array,
127127
data.ciphertext.as_ref(),
128128
)
129-
.map_err(AppError::AesGcm)
129+
.map_err(|_| AppError::Decrypt)
130130
}
131131
}
132132
}

rust/cryptonote/src/encoding.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn build_url(
3939
pub fn parse_url(url: &str) -> Result<NoteData, AppError> {
4040
url.split("#note=")
4141
.nth(1)
42-
.ok_or_else(|| AppError::InvalidUrl)
42+
.ok_or_else(|| AppError::Url)
4343
.and_then(decode_note)
4444
}
4545

rust/cryptonote/src/error.rs

Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,70 @@
11
pub use derive_more::Display;
22
use qrcode::types::QrError;
33
use sha2::digest;
4+
use std::string::FromUtf8Error;
45

56
#[derive(Debug, Display)]
67
pub enum AppError {
7-
SymKey(digest::InvalidLength),
8-
AesGcm(aes_gcm::Error),
9-
ChaChaPoly1305(chacha20poly1305::Error),
10-
Getrandom(String),
8+
Cipher(digest::InvalidLength),
9+
Getrandom(getrandom::Error),
10+
Base64(base64::DecodeError),
1111
Json(serde_json::Error),
12-
Base64(String),
13-
Utf8(String),
12+
Utf8(FromUtf8Error),
1413
Qr(QrError),
15-
InvalidUrl,
14+
Encrypt,
15+
Decrypt,
16+
Url,
1617
}
1718

1819
impl AppError {
1920
pub fn localized(
2021
&self,
2122
t: &crate::i18n::Translations,
2223
) -> String {
23-
match self {
24-
AppError::SymKey(msg) => {
25-
format!("{}: {}", t.crypto_error, msg)
26-
}
27-
AppError::AesGcm(msg) => {
28-
format!("{}: {}", t.crypto_error, msg)
29-
}
30-
AppError::ChaChaPoly1305(msg) => {
31-
format!("{}: {}", t.crypto_error, msg)
32-
}
33-
AppError::Getrandom(msg) => {
34-
format!("{}: {}", t.crypto_error, msg)
35-
}
36-
AppError::Base64(msg) => {
37-
format!("{}: {}", t.encoding_error, msg)
38-
}
39-
AppError::Json(msg) => {
40-
format!("{}: {}", t.encoding_error, msg)
41-
}
42-
AppError::Utf8(msg) => {
43-
format!("{}: {}", t.utf8_error, msg)
44-
}
45-
AppError::Qr(msg) => {
46-
format!("{}: {}", t.utf8_error, msg)
47-
}
48-
AppError::InvalidUrl => {
49-
t.invalid_url_error.to_string()
50-
}
51-
}
24+
let msg = match self {
25+
AppError::Cipher(_) => t.crypto_error,
26+
AppError::Getrandom(_) => t.crypto_error,
27+
AppError::Base64(_) => t.encoding_error,
28+
AppError::Json(_) => t.encoding_error,
29+
AppError::Utf8(_) => t.utf8_error,
30+
AppError::Qr(_) => t.utf8_error,
31+
AppError::Encrypt => t.crypto_error,
32+
AppError::Decrypt => t.crypto_error,
33+
AppError::Url => t.invalid_url_error,
34+
};
35+
format!("{}: {}", msg, self)
5236
}
5337
}
5438

5539
impl std::error::Error for AppError {}
5640

57-
impl From<serde_json::Error> for AppError {
58-
fn from(e: serde_json::Error) -> Self {
59-
AppError::Json(e)
41+
impl From<digest::InvalidLength> for AppError {
42+
fn from(e: digest::InvalidLength) -> Self {
43+
AppError::Cipher(e)
6044
}
6145
}
6246

63-
impl From<base64::DecodeError> for AppError {
64-
fn from(err: base64::DecodeError) -> Self {
65-
AppError::Base64(err.to_string())
47+
impl From<getrandom::Error> for AppError {
48+
fn from(e: getrandom::Error) -> Self {
49+
AppError::Getrandom(e)
6650
}
6751
}
6852

69-
impl From<std::string::FromUtf8Error> for AppError {
70-
fn from(err: std::string::FromUtf8Error) -> Self {
71-
AppError::Utf8(err.to_string())
53+
impl From<base64::DecodeError> for AppError {
54+
fn from(e: base64::DecodeError) -> Self {
55+
AppError::Base64(e)
7256
}
7357
}
7458

75-
impl From<getrandom::Error> for AppError {
76-
fn from(err: getrandom::Error) -> Self {
77-
AppError::Getrandom(err.to_string())
59+
impl From<serde_json::Error> for AppError {
60+
fn from(e: serde_json::Error) -> Self {
61+
AppError::Json(e)
7862
}
7963
}
8064

81-
impl From<digest::InvalidLength> for AppError {
82-
fn from(e: digest::InvalidLength) -> Self {
83-
AppError::SymKey(e)
65+
impl From<FromUtf8Error> for AppError {
66+
fn from(e: FromUtf8Error) -> Self {
67+
AppError::Utf8(e)
8468
}
8569
}
8670

0 commit comments

Comments
 (0)