You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#[contracterror]#[derive(Copy,Clone,Debug,Eq,PartialEq)]#[repr(u32)]pubenumMyError{ErrorName = 1,// Start from 1, sequential}
Use Errors
// Panic with errorif invalid_condition {panic_with_error!(&env,MyError::ErrorName);}// With Resultlet result = operation().ok_or(MyError::ErrorName)?;
Safe Arithmetic
// Checked operationslet sum = a.checked_add(b).ok_or(Error::Overflow)?;let diff = a.checked_sub(b).ok_or(Error::Underflow)?;// Saturating (clips at min/max)let capped = a.saturating_add(b);
Test Errors
#[test]#[should_panic(expected = "ErrorName")]fntest_error_case(){// Code that should panic with specific error}
Best Practices
Sequential codes: Start from 1, increment by 1
Descriptive names: ClearPurpose, not Err1
Check early: Validate inputs before processing
Safe math: Always use checked_* operations
Test all errors: Every error code should have a test