Skip to content

Commit

Permalink
refactor(tracing): improve error reporting (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
pbillaut authored Sep 15, 2024
1 parent 3c1d524 commit 8360145
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl Account {
if !is_valid_amount(amount) {
Err(InvalidTransaction("withdrawal amount must be a positive number".into()))
} else if amount > self.available {
Err(FailedTransaction("withdrawal failed: insufficient funds".into()))
Err(FailedTransaction("withdrawal failed because of insufficient funds".into()))
} else {
self.available -= amount;
self.total -= amount;
Expand Down
14 changes: 14 additions & 0 deletions src/account_activity.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::dispute::DisputeCase;
use crate::transaction::{Transaction, TransactionID};
use crate::ClientID;
use std::fmt::{Display, Formatter};
use thiserror::Error;

#[derive(Error, Debug)]
Expand Down Expand Up @@ -117,3 +118,16 @@ impl AccountActivity {
}
}
}

impl Display for AccountActivity {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let kind = match self {
AccountActivity::Deposit(_) => "deposit",
AccountActivity::Withdrawal(_) => "withdrawal",
AccountActivity::Dispute(_) => "dispute",
AccountActivity::Resolve(_) => "resolve",
AccountActivity::Chargeback(_) => "chargeback",
};
write!(f, "{}", kind)
}
}
16 changes: 9 additions & 7 deletions src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ pub trait Processor {
Err(err) => {
error!(error = ?err, "error parsing account activity record")
}
Ok(transaction) => {
Ok(activity) => {
let account = accounts
.entry(transaction.client_id())
.or_insert_with(|| Account::new(transaction.client_id()));
if let Err(err) = account.transaction(transaction) {
.entry(activity.client_id())
.or_insert_with(|| Account::new(activity.client_id()));
if let Err(err) = account.transaction(activity) {
warn!(
transaction.id = %transaction.transaction_id(),
client.id = %transaction.client_id(),
"error processing account activity: {}",err
activity = %activity,
transaction.id = %activity.transaction_id(),
client.id = %activity.client_id(),
error = ?err,
"error processing account activity",
);
}
}
Expand Down

0 comments on commit 8360145

Please sign in to comment.