Description
Every failure path in the contract is a bare panic! with a string literal — "rate and balance must be positive" (src/lib.rs:114/:247), "stream already active" (src/lib.rs:279), "stream not active" (src/lib.rs:304), "batch too large" (src/lib.rs:352), "rate increase exceeds 10% limit" (src/lib.rs:586), "stream not found" (src/stream.rs:62), and more. docs/error-codes.md catalogues these strings and explicitly notes that a v1.0 plan in docs/audit-readiness.md proposes migrating to #[contracterror] enums. This issue performs that migration so off-chain clients can match on stable numeric codes instead of brittle strings.
Requirements and context
Functional
- Define
#[contracterror] #[derive(Copy, Clone, ...)] #[repr(u32)] pub enum StreamError { ... } with one variant per documented failure in docs/error-codes.md (e.g. NotFound = 1, IdOverflow = 2, NonPositiveRateOrBalance = 3, AlreadyActive = 4, NotActive = 5, BatchTooLarge = 6, RateIncreaseTooLarge = 7, Unsettled = 8, Unclaimed = 9, MemoTooLong = 10).
- Convert entry points to return
Result<T, StreamError> (or panic_with_error!) instead of panic!, preserving the existing trigger conditions exactly so semantics are unchanged.
- Keep the numeric codes stable and documented; the enum discriminants become the contract’s public error ABI.
Non-functional / repo conventions
- Update the inline
#[should_panic(expected = "...")] tests (e.g. test_stop_stream_inactive_panics at src/lib.rs:793, test_update_rate_large_increase_panics at src/lib.rs:1391) to assert on the error variant via the generated try_* client methods rather than panic strings.
- Stay within the WASM size budget (
scripts/check-wasm-size.sh); a small enum is cheaper than many distinct string literals.
Acceptance criteria
Suggested execution
1. Fork the repo and create a branch — git checkout -b improvement/contracterror-enum.
2. Implement changes — add StreamError (new src/error.rs re-exported from lib.rs, or inline), convert entry points and src/stream.rs:62.
3. Write/extend tests — convert #[should_panic] tests to try_*-based assertions on Err(StreamError::...).
4. Test and commit
cargo fmt --all -- --check
cargo clippy --all-targets --all-features -- -D warnings
cargo test --features testutils
./scripts/check-wasm-size.sh
Example commit message
refactor(errors): migrate panic strings to #[contracterror] StreamError enum
Guidelines
- Target 95% line coverage (
docs/coverage.sh).
- Add
/// doc-comments per variant and rewrite docs/error-codes.md (string → code table); cross-link docs/audit-readiness.md.
- Timeframe: 96 hours.
Description
Every failure path in the contract is a bare
panic!with a string literal —"rate and balance must be positive"(src/lib.rs:114/:247),"stream already active"(src/lib.rs:279),"stream not active"(src/lib.rs:304),"batch too large"(src/lib.rs:352),"rate increase exceeds 10% limit"(src/lib.rs:586),"stream not found"(src/stream.rs:62), and more.docs/error-codes.mdcatalogues these strings and explicitly notes that a v1.0 plan indocs/audit-readiness.mdproposes migrating to#[contracterror]enums. This issue performs that migration so off-chain clients can match on stable numeric codes instead of brittle strings.Requirements and context
Functional
#[contracterror] #[derive(Copy, Clone, ...)] #[repr(u32)] pub enum StreamError { ... }with one variant per documented failure indocs/error-codes.md(e.g.NotFound = 1,IdOverflow = 2,NonPositiveRateOrBalance = 3,AlreadyActive = 4,NotActive = 5,BatchTooLarge = 6,RateIncreaseTooLarge = 7,Unsettled = 8,Unclaimed = 9,MemoTooLong = 10).Result<T, StreamError>(orpanic_with_error!) instead ofpanic!, preserving the existing trigger conditions exactly so semantics are unchanged.Non-functional / repo conventions
#[should_panic(expected = "...")]tests (e.g.test_stop_stream_inactive_panicsatsrc/lib.rs:793,test_update_rate_large_increase_panicsatsrc/lib.rs:1391) to assert on the error variant via the generatedtry_*client methods rather than panic strings.scripts/check-wasm-size.sh); a small enum is cheaper than many distinct string literals.Acceptance criteria
StreamErrorenum defined with stable discriminants covering every row ofdocs/error-codes.md.panic!("...")call sites insrc/lib.rsandsrc/stream.rsreplaced with the enum.docs/error-codes.mdupdated to map each panic string to its new numeric code.cargo testandcargo clippy ... -D warningspass.Suggested execution
1. Fork the repo and create a branch —
git checkout -b improvement/contracterror-enum.2. Implement changes — add
StreamError(newsrc/error.rsre-exported fromlib.rs, or inline), convert entry points andsrc/stream.rs:62.3. Write/extend tests — convert
#[should_panic]tests totry_*-based assertions onErr(StreamError::...).4. Test and commit
cargo fmt --all -- --check cargo clippy --all-targets --all-features -- -D warnings cargo test --features testutils ./scripts/check-wasm-size.shExample commit message
Guidelines
docs/coverage.sh).///doc-comments per variant and rewritedocs/error-codes.md(string → code table); cross-linkdocs/audit-readiness.md.