Skip to content

Commit eb2676d

Browse files
authored
Improve Errors (#722)
1 parent 1f0a757 commit eb2676d

File tree

28 files changed

+282
-326
lines changed

28 files changed

+282
-326
lines changed

crates/bcr-ebill-api/src/service/bill_service/error.rs

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,10 @@ use thiserror::Error;
55
/// Generic error type
66
#[derive(Debug, Error)]
77
pub enum Error {
8-
/// errors that currently return early http status code Status::NotFound
8+
/// errors stemming from resources that were not found
99
#[error("not found")]
1010
NotFound,
1111

12-
/// errors stemming from trying to do invalid operations
13-
#[error("invalid operation")]
14-
InvalidOperation,
15-
16-
/// error returned if the given file upload id is not a temp file we have
17-
#[error("No file found for file upload id")]
18-
NoFileForFileUploadId,
19-
2012
/// errors that stem from interacting with a blockchain
2113
#[error("Blockchain error: {0}")]
2214
Blockchain(#[from] blockchain::Error),
@@ -39,49 +31,6 @@ pub enum Error {
3931
#[error("Protocol error: {0}")]
4032
Protocol(#[from] bcr_ebill_core::protocol::ProtocolError),
4133

42-
#[error("io error {0}")]
43-
Io(#[from] std::io::Error),
44-
45-
/// errors that stem from drawee identity not being in the contacts
46-
#[error("Can not get drawee identity from contacts.")]
47-
DraweeNotInContacts,
48-
49-
/// errors that stem from payee identity not being in the contacts
50-
#[error("Can not get payee identity from contacts.")]
51-
PayeeNotInContacts,
52-
53-
/// errors that stem from buyer identity not being in the contacts
54-
#[error("Can not get buyer identity from contacts.")]
55-
BuyerNotInContacts,
56-
57-
/// errors that stem from endorsee identity not being in the contacts
58-
#[error("Can not get endorsee identity from contacts.")]
59-
EndorseeNotInContacts,
60-
61-
/// errors that stem from mint identity not being in the contacts
62-
#[error("Can not get mint identity from contacts.")]
63-
MintNotInContacts,
64-
65-
/// errors that stem from recoursee identity not being in the contacts
66-
#[error("Can not get recoursee identity from contacts.")]
67-
RecourseeNotInContacts,
68-
69-
/// errors that stem from trying to cancel a mint request that's not pending
70-
#[error("Mint request can only be cancelled if it's pending.")]
71-
CancelMintRequestNotPending,
72-
73-
/// errors that stem from trying to reject a mint request that's not offered
74-
#[error("Mint request can only be rejected if it's offered.")]
75-
RejectMintRequestNotOffered,
76-
77-
/// errors that stem from trying to accept a mint request that's not offered
78-
#[error("Mint request can only be accepted if it's offered.")]
79-
AcceptMintRequestNotOffered,
80-
81-
/// errors that stem from trying to accept a mint request that's expired
82-
#[error("Mint request can only be accepted if it's not expired.")]
83-
AcceptMintOfferExpired,
84-
8534
/// errors that stem from bill validation errors
8635
#[error("bill validation error {0}")]
8736
Validation(#[from] bcr_ebill_core::ValidationError),

crates/bcr-ebill-api/src/service/bill_service/issue.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl BillService {
7070
let public_data_drawee = match self.contact_store.get(&data.drawee).await {
7171
Ok(Some(drawee)) => drawee.try_into()?,
7272
Ok(None) | Err(_) => {
73-
return Err(Error::DraweeNotInContacts);
73+
return Err(Error::Validation(ValidationError::DraweeNotInContacts));
7474
}
7575
};
7676

@@ -91,7 +91,7 @@ impl BillService {
9191
let mut public_data_payee = match self.contact_store.get(&data.payee).await {
9292
Ok(Some(payee)) => payee.try_into()?,
9393
Ok(None) | Err(_) => {
94-
return Err(Error::PayeeNotInContacts);
94+
return Err(Error::Validation(ValidationError::PayeeNotInContacts));
9595
}
9696
};
9797

@@ -113,14 +113,14 @@ impl BillService {
113113
let public_data_drawee = match self.contact_store.get(&data.drawee).await {
114114
Ok(Some(drawee)) => drawee.try_into()?,
115115
Ok(None) | Err(_) => {
116-
return Err(Error::DraweeNotInContacts);
116+
return Err(Error::Validation(ValidationError::DraweeNotInContacts));
117117
}
118118
};
119119

120120
let mut public_data_payee = match self.contact_store.get(&data.payee).await {
121121
Ok(Some(payee)) => payee.try_into()?,
122122
Ok(None) | Err(_) => {
123-
return Err(Error::PayeeNotInContacts);
123+
return Err(Error::Validation(ValidationError::PayeeNotInContacts));
124124
}
125125
};
126126

@@ -162,7 +162,7 @@ impl BillService {
162162
.file_upload_store
163163
.read_temp_upload_file(file_upload_id)
164164
.await
165-
.map_err(|_| Error::NoFileForFileUploadId)?;
165+
.map_err(|_| Error::Validation(ValidationError::NoFileForFileUploadId))?;
166166
bill_files.push(
167167
self.encrypt_and_save_uploaded_file(
168168
file_name,

crates/bcr-ebill-api/src/service/bill_service/service.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,7 +1678,9 @@ impl BillServiceApi for BillService {
16781678
.await?;
16791679
Ok(())
16801680
} else {
1681-
Err(Error::CancelMintRequestNotPending)
1681+
Err(Error::Validation(
1682+
ValidationError::CancelMintRequestNotPending,
1683+
))
16821684
}
16831685
}
16841686

@@ -1742,7 +1744,7 @@ impl BillServiceApi for BillService {
17421744
// and not expired
17431745
if let Ok(Some(offer)) = self.mint_store.get_offer(&req.mint_request_id).await {
17441746
if offer.expiration_timestamp < timestamp {
1745-
return Err(Error::AcceptMintOfferExpired);
1747+
return Err(Error::Validation(ValidationError::AcceptMintOfferExpired));
17461748
}
17471749
// accept the offer
17481750
self.mint_client
@@ -1790,7 +1792,9 @@ impl BillServiceApi for BillService {
17901792
Err(Error::NotFound)
17911793
}
17921794
} else {
1793-
Err(Error::AcceptMintRequestNotOffered)
1795+
Err(Error::Validation(
1796+
ValidationError::AcceptMintRequestNotOffered,
1797+
))
17941798
}
17951799
}
17961800

@@ -1823,7 +1827,9 @@ impl BillServiceApi for BillService {
18231827
.await?;
18241828
Ok(())
18251829
} else {
1826-
Err(Error::RejectMintRequestNotOffered)
1830+
Err(Error::Validation(
1831+
ValidationError::RejectMintRequestNotOffered,
1832+
))
18271833
}
18281834
}
18291835

@@ -1835,7 +1841,7 @@ impl BillServiceApi for BillService {
18351841
// if dev mode is off - we return an error
18361842
if !get_config().dev_mode_config.on {
18371843
error!("Called dev mode operation with dev mode disabled - please enable!");
1838-
return Err(Error::InvalidOperation);
1844+
return Err(Error::Validation(ValidationError::InvalidOperation));
18391845
}
18401846

18411847
validate_bill_id_network(bill_id)?;

crates/bcr-ebill-api/src/service/company_service.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ impl CompanyService {
185185
.file_upload_store
186186
.read_temp_upload_file(upload_id)
187187
.await
188-
.map_err(|_| crate::service::Error::NoFileForFileUploadId)?;
188+
.map_err(|_| {
189+
crate::service::Error::Validation(ValidationError::NoFileForFileUploadId)
190+
})?;
189191
// validate file size for upload file type
190192
if !upload_file_type.check_file_size(file_bytes.len()) {
191193
return Err(crate::service::Error::Validation(
@@ -976,7 +978,7 @@ impl CompanyServiceApi for CompanyService {
976978
// if dev mode is off - we return an error
977979
if !get_config().dev_mode_config.on {
978980
error!("Called dev mode operation with dev mode disabled - please enable!");
979-
return Err(Error::InvalidOperation);
981+
return Err(Error::Validation(ValidationError::InvalidOperation));
980982
}
981983
validate_node_id_network(id)?;
982984

crates/bcr-ebill-api/src/service/contact_service.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ impl ContactService {
179179
.file_upload_store
180180
.read_temp_upload_file(upload_id)
181181
.await
182-
.map_err(|_| crate::service::Error::NoFileForFileUploadId)?;
182+
.map_err(|_| {
183+
crate::service::Error::Validation(ValidationError::NoFileForFileUploadId)
184+
})?;
183185
// validate file size for upload file type
184186
if !upload_file_type.check_file_size(file_bytes.len()) {
185187
return Err(crate::service::Error::Validation(

crates/bcr-ebill-api/src/service/file_upload_service.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ impl FileUploadServiceApi for FileUploadService {
113113
.file_upload_store
114114
.read_temp_upload_file(file_upload_id)
115115
.await
116-
.map_err(|_| crate::service::Error::NoFileForFileUploadId)?;
116+
.map_err(|_| {
117+
crate::service::Error::Validation(ValidationError::NoFileForFileUploadId)
118+
})?;
117119
let (file_name, file_bytes) = file;
118120
return Ok(Some((file_name, file_bytes)));
119121
}
@@ -250,11 +252,7 @@ mod tests {
250252
let mut storage = MockFileUploadStoreApiMock::new();
251253
storage
252254
.expect_write_temp_upload_file()
253-
.returning(|_, _, _| {
254-
Err(bcr_ebill_persistence::Error::Io(std::io::Error::other(
255-
"test error",
256-
)))
257-
});
255+
.returning(|_, _, _| Err(bcr_ebill_persistence::Error::EncodingError));
258256
let mut file = MockUploadFileHandler::new();
259257
file.expect_name()
260258
.returning(|| Some(String::from("invoice")));

crates/bcr-ebill-api/src/service/identity_service.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ impl IdentityService {
166166
.file_upload_store
167167
.read_temp_upload_file(upload_id)
168168
.await
169-
.map_err(|_| crate::service::Error::NoFileForFileUploadId)?;
169+
.map_err(|_| {
170+
crate::service::Error::Validation(ValidationError::NoFileForFileUploadId)
171+
})?;
170172
// validate file size for upload file type
171173
if !upload_file_type.check_file_size(file_bytes.len()) {
172174
return Err(crate::service::Error::Validation(
@@ -720,7 +722,7 @@ impl IdentityServiceApi for IdentityService {
720722
// if dev mode is off - we return an error
721723
if !get_config().dev_mode_config.on {
722724
error!("Called dev mode operation with dev mode disabled - please enable!");
723-
return Err(Error::InvalidOperation);
725+
return Err(Error::Validation(ValidationError::InvalidOperation));
724726
}
725727

726728
// if there is identity yet, we return an error
@@ -1129,11 +1131,10 @@ mod tests {
11291131
let identity = empty_identity();
11301132
Ok(identity)
11311133
});
1132-
storage.expect_save().returning(|_| {
1133-
Err(bcr_ebill_persistence::Error::Io(std::io::Error::other(
1134-
"test error",
1135-
)))
1136-
});
1134+
storage
1135+
.expect_save()
1136+
.returning(|_| Err(bcr_ebill_persistence::Error::EncodingError))
1137+
.times(1);
11371138
let mut chain_storage = MockIdentityChainStoreApiMock::new();
11381139
chain_storage.expect_get_latest_block().returning(|| {
11391140
let identity = empty_identity();
@@ -1201,11 +1202,9 @@ mod tests {
12011202
#[tokio::test]
12021203
async fn get_identity_propagates_errors() {
12031204
let mut storage = MockIdentityStoreApiMock::new();
1204-
storage.expect_get().returning(|| {
1205-
Err(bcr_ebill_persistence::Error::Io(std::io::Error::other(
1206-
"test error",
1207-
)))
1208-
});
1205+
storage
1206+
.expect_get()
1207+
.returning(|| Err(bcr_ebill_persistence::Error::EncodingError));
12091208

12101209
let service = get_service(storage);
12111210
let res = service.get_identity().await;
@@ -1235,11 +1234,9 @@ mod tests {
12351234
#[tokio::test]
12361235
async fn get_full_identity_propagates_errors() {
12371236
let mut storage = MockIdentityStoreApiMock::new();
1238-
storage.expect_get_full().returning(|| {
1239-
Err(bcr_ebill_persistence::Error::Io(std::io::Error::other(
1240-
"test error",
1241-
)))
1242-
});
1237+
storage
1238+
.expect_get_full()
1239+
.returning(|| Err(bcr_ebill_persistence::Error::EncodingError));
12431240

12441241
let service = get_service(storage);
12451242
let res = service.get_full_identity().await;

crates/bcr-ebill-api/src/service/mod.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub enum Error {
2020
#[error("Persistence error: {0}")]
2121
Persistence(#[from] bcr_ebill_persistence::Error),
2222

23-
/// errors that currently return early http status code Status::NotFound
23+
/// errors stemming from resources that were not found
2424
#[error("not found")]
2525
NotFound,
2626

@@ -47,18 +47,6 @@ pub enum Error {
4747
#[error("Blockchain error: {0}")]
4848
Blockchain(#[from] bcr_ebill_core::blockchain::Error),
4949

50-
/// std io
51-
#[error("Io error: {0}")]
52-
Io(#[from] std::io::Error),
53-
5450
#[error("Json error: {0}")]
5551
Json(#[from] serde_json::Error),
56-
57-
/// error returned if the given file upload id is not a temp file we have
58-
#[error("No file found for file upload id")]
59-
NoFileForFileUploadId,
60-
61-
/// errors stemming from trying to do invalid operations
62-
#[error("invalid operation")]
63-
InvalidOperation,
6452
}

crates/bcr-ebill-api/src/service/notification_service/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ impl From<crypto::Error> for Error {
7878
}
7979
}
8080

81-
impl From<bcr_ebill_core::util::Error> for Error {
82-
fn from(e: bcr_ebill_core::util::Error) -> Self {
81+
impl From<bitcoin::base58::InvalidCharacterError> for Error {
82+
fn from(e: bitcoin::base58::InvalidCharacterError) -> Self {
8383
Error::Crypto(format!("Failed base58 operation: {e}"))
8484
}
8585
}

crates/bcr-ebill-core/src/blockchain/mod.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ use secp256k1::PublicKey;
22
use serde::{Deserialize, Serialize};
33
use thiserror::Error;
44

5+
use crate::ValidationError;
56
use crate::block_id::BlockId;
67
use crate::hash::Sha256Hash;
78
use crate::signature::SchnorrSignature;
89
use crate::timestamp::Timestamp;
910
use crate::util::crypto;
10-
use crate::{ValidationError, util};
1111
use borsh::{BorshDeserialize, BorshSerialize, to_vec};
1212
use log::{error, warn};
1313
use std::fmt::Display;
14-
use std::string::FromUtf8Error;
1514

1615
pub mod bill;
1716
pub mod company;
@@ -31,10 +30,6 @@ pub enum Error {
3130
#[error("Blockchain is invalid")]
3231
BlockchainInvalid,
3332

34-
/// If there is an error deserializing and decrypting blocks
35-
#[error("Could not parse and decrypt blockchain")]
36-
BlockchainParse,
37-
3833
/// If certain block is not valid and can't be added
3934
#[error("Block is invalid")]
4035
BlockInvalid,
@@ -47,26 +42,13 @@ pub enum Error {
4742
#[error("Block's signature does not match the signer")]
4843
BlockSignatureDoesNotMatchSigner,
4944

50-
/// If an invalid operation is passed to a function (e.g. a non-reject op)
51-
#[error("Invalid operation")]
52-
InvalidOperation,
53-
5445
/// Errors stemming from cryptography, such as converting keys, encryption and decryption
5546
#[error("Secp256k1Cryptography error: {0}")]
5647
Secp256k1Cryptography(#[from] crypto::Error),
5748

5849
/// Errors stemming from base58 decoding
5950
#[error("Base 58 Decode error: {0}")]
60-
Base58Decode(#[from] util::Error),
61-
62-
/// Errors stemming from converting from utf-8 strings
63-
#[error("UTF-8 error: {0}")]
64-
Utf8(#[from] FromUtf8Error),
65-
66-
/// Errors stemming from dealing with invalid block data, e.g. if within an Endorse block,
67-
/// there is no endorsee
68-
#[error("Invalid block data error: {0}")]
69-
InvalidBlockdata(String),
51+
Base58Decode(#[from] bitcoin::base58::InvalidCharacterError),
7052

7153
/// The given blockchain type string could not be converted to a valid type
7254
#[error("Invalid blockchain type: {0}")]

0 commit comments

Comments
 (0)