Skip to content

Commit c0034ec

Browse files
committed
custom error enums for state and token methods
1 parent 207f6b7 commit c0034ec

File tree

3 files changed

+94
-145
lines changed

3 files changed

+94
-145
lines changed

fil_fungible_token/src/token/errors.rs

Lines changed: 0 additions & 71 deletions
This file was deleted.

fil_fungible_token/src/token/mod.rs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
pub mod errors;
21
pub mod receiver;
32
mod state;
43
mod types;
5-
use std::ops::Neg;
64

7-
use self::state::TokenState;
5+
use self::state::{StateError, TokenState};
86
pub use self::types::*;
97

10-
use anyhow::bail;
11-
use anyhow::Ok;
12-
use anyhow::Result;
138
use cid::Cid;
149
use fvm_ipld_blockstore::Blockstore as IpldStore;
1510
use fvm_shared::econ::TokenAmount;
1611
use fvm_shared::ActorID;
1712
use num_traits::Signed;
13+
use std::ops::Neg;
14+
use thiserror::Error;
15+
16+
#[derive(Error, Debug)]
17+
pub enum TokenError {
18+
#[error("error in underlying state {0}")]
19+
State(#[from] StateError),
20+
#[error("invalid negative: {0}")]
21+
InvalidNegative(String),
22+
}
23+
24+
type Result<T> = std::result::Result<T, TokenError>;
1825

1926
/// Library functions that implement core FRC-??? standards
2027
///
@@ -44,7 +51,7 @@ where
4451
/// Constructs the token state tree and saves it at a CID
4552
pub fn init_state(&self) -> Result<Cid> {
4653
let init_state = TokenState::new(&self.bs)?;
47-
init_state.save(&self.bs)
54+
Ok(init_state.save(&self.bs)?)
4855
}
4956

5057
/// Helper function that loads the root of the state tree related to token-accounting
@@ -60,7 +67,10 @@ where
6067
/// The mint amount must be non-negative or the method returns an error
6168
pub fn mint(&self, initial_holder: ActorID, value: TokenAmount) -> Result<()> {
6269
if value.is_negative() {
63-
bail!("value of mint was negative {}", value);
70+
return Err(TokenError::InvalidNegative(format!(
71+
"mint amount {} cannot be negative",
72+
value
73+
)));
6474
}
6575

6676
// Increase the balance of the actor and increase total supply
@@ -92,7 +102,7 @@ where
92102
pub fn balance_of(&self, holder: ActorID) -> Result<TokenAmount> {
93103
// Load the HAMT holding balances
94104
let state = self.load_state();
95-
state.get_balance(&self.bs, holder)
105+
Ok(state.get_balance(&self.bs, holder)?)
96106
}
97107

98108
/// Gets the allowance between owner and spender
@@ -116,7 +126,10 @@ where
116126
delta: TokenAmount,
117127
) -> Result<TokenAmount> {
118128
if delta.is_negative() {
119-
bail!("value of delta was negative {}", delta);
129+
return Err(TokenError::InvalidNegative(format!(
130+
"increase allowance delta {} cannot be negative",
131+
delta
132+
)));
120133
}
121134

122135
let mut state = self.load_state();
@@ -138,7 +151,10 @@ where
138151
delta: TokenAmount,
139152
) -> Result<TokenAmount> {
140153
if delta.is_negative() {
141-
bail!("value of delta was negative {}", delta);
154+
return Err(TokenError::InvalidNegative(format!(
155+
"decrease allowance delta {} cannot be negative",
156+
delta
157+
)));
142158
}
143159

144160
let mut state = self.load_state();
@@ -186,7 +202,10 @@ where
186202
value: TokenAmount,
187203
) -> Result<TokenAmount> {
188204
if value.is_negative() {
189-
bail!("cannot burn a negative amount");
205+
return Err(TokenError::InvalidNegative(format!(
206+
"burn amount {} cannot be negative",
207+
value
208+
)));
190209
}
191210

192211
let mut state = self.load_state();
@@ -235,7 +254,10 @@ where
235254
value: TokenAmount,
236255
) -> Result<()> {
237256
if value.is_negative() {
238-
bail!("cannot transfer a negative amount");
257+
return Err(TokenError::InvalidNegative(format!(
258+
"transfer amount {} cannot be negative",
259+
value
260+
)));
239261
}
240262

241263
let mut state = self.load_state();

0 commit comments

Comments
 (0)