-
Notifications
You must be signed in to change notification settings - Fork 251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: make lockable-ft example use Result
-based error handling
#750
Conversation
&mut self, | ||
escrow_account_id: AccountId, | ||
allowance: String, | ||
) -> Result<(), &'static str> { | ||
let allowance = u128::from_str(&allowance).expect("Failed to parse allowance"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably preferable if we switch these .expect
calls over to early returning an error since our error type is a str anyways, but not a big deal if we leave it as is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops forgot to finalize this review comment
} | ||
|
||
/// Locks an additional `lock_amount` to the caller of the function (`predecessor_id`) from | ||
/// the `owner_id`. | ||
/// Requirements: | ||
/// * The (`predecessor_id`) should have enough allowance or be the owner. | ||
/// * The owner should have enough unlocked balance. | ||
pub fn lock(&mut self, owner_id: AccountId, lock_amount: String) { | ||
#[return_result] | ||
pub fn lock(&mut self, owner_id: AccountId, lock_amount: String) -> Result<(), &'static str> { | ||
let lock_amount = u128::from_str(&lock_amount).expect("Failed to parse allow lock_amount"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If switching over to this pattern, probably worth switching this expect to mapping the error and using ?
&mut self, | ||
owner_id: AccountId, | ||
unlock_amount: String, | ||
) -> Result<(), &'static str> { | ||
let unlock_amount = | ||
u128::from_str(&unlock_amount).expect("Failed to parse allow unlock_amount"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here, and a few other places
Addresses #745 (review)
Also covered this example with workspaces-powered tests since that is the only way to test the new error handling behavior.