refactor: replace panic! strings with typed #[contracterror] enum - #31
Conversation
JamesVictor-O
left a comment
There was a problem hiding this comment.
The idea here — splitting the generic InvalidAmount into more specific variants (FeeTooHigh, InvalidCreatorCount, etc.) and adding targeted failure-path tests — is reasonable. But the branch as pushed doesn't compile, and it's not a small fix:
error[E0428]: the name `Error` is defined multiple times (33 errors total)
Concretely, on this branch right now:
error.rs has a duplicate variant and is missing ten variants that are still used elsewhere. DeadlineInPast is defined twice (= 7 and = 17), and NotInitialized, Unauthorized, InvalidStatus, ApplicationNotFound, MaxCreatorsReached, InsufficientEscrowBalance, NotCampaignOwner, SubmissionNotPayable, AlreadyApplied, and AlreadySelected aren't in the enum at all anymore — but lib.rs and test.rs both still reference them throughout (e.g. Error::NotCampaignOwner in fund_campaign/approve_creator/cancel_campaign/etc.), so those are unresolved-name errors on top of the duplicate-discriminant one.
test.rs has a literal duplicate mod test_helpers — one nested inside the other:
mod test_helpers {
use crate::{CampaignEscrowContract, CampaignEscrowContractClient, PayoutAsset};
...
mod test_helpers {
use super::*;
...Both of these look like classic merge/rebase artifacts (the same shape of breakage #21 and #22 caused when they were merged into main a couple days ago, which I had to repair directly on main afterward) rather than anything intentional — my guess is this branch was based on main from before that repair, and something went wrong reconciling it. Worth double-checking: gh pr checks 31 currently shows no checks have run at all on this branch, so the "all four CI jobs pass" in the PR description was true at some earlier point, not on what's here now.
Also worth reconsidering regardless of the compile errors: this renumbers every existing discriminant (NotCampaignOwner moves from 11 to 19, AlreadySelected moves from 14 to a value that isn't even in the new enum, etc.), not just the ones this PR is adding. Every other error-enum change that's landed so far (#24, #28, #29, and #33 which just merged with InvalidDeadlineOrder = 20) has been purely additive — appending new variants after the highest existing discriminant, never touching existing ones. Renumbering isn't free even pre-mainnet: it silently reshuffles what every other in-flight PR's error checks mean, and is exactly the kind of change that causes the conflicts you're currently hitting. I'd suggest keeping the existing 20 variants (main is now at ContractPaused = 19 / InvalidDeadlineOrder = 20) untouched, and appending FeeTooHigh, InvalidCreatorCount, BudgetBelowObligations, etc. starting at 21 — same effect (specific typed errors instead of a generic InvalidAmount), zero risk of colliding with everything else in flight.
The 8 new failure-path tests are a nice addition in principle — worth keeping once the branch is rebased cleanly onto current main and the enum change is additive rather than a renumber.
…ted failure-path tests Append InvalidCreatorCount (=24) to the error enum without renumbering any existing discriminants. Use FeeTooHigh (=21, already existed) for the initialize fee-range check. Split the combined budget/max_creators validation in create_campaign into two distinct error paths. Add test_error_variants module with8 focused tests covering each specific error code: InvalidCreatorCount, MaxCreatorsReached, InsufficientEscrowBalance, FeeTooHigh, InvalidDeadlineOrder, DeadlineInPast, ApplicationDeadlinePassed, ContentDeadlinePassed.
76a5b2a to
e1c6131
Compare
Everything now properly implemented, You can have a look @JamesVictor-O |
|
Merged, no follow-up needed — this is exactly the additive shape I'd asked for on the previous round: the only new discriminant is 64 campaign-escrow + 7 dispute-resolution tests, clippy, fmt, and the wasm release build are all clean. Thanks for taking the rebase seriously — this is a much better outcome than trying to force the original branch through. |
Adds propose_admin/accept_admin for a two-step admin handover (candidate must prove control of the new key via require_auth before the transfer finalizes), replacing what would otherwise be a single unchecked update_admin call. Verified locally in an isolated worktree before merge: - cargo fmt --all -- --check - cargo build --workspace - cargo test --workspace (68 tests, including new test_admin_transfer module) - cargo clippy --workspace --all-targets -- -D warnings - cargo build --workspace --target wasm32v1-none --release - Confirmed via git merge-tree that the merge result correctly retains InvalidCreatorCount=24 and FeeTooHigh (added by #31, which landed after this branch's last main-merge) alongside the new PendingAdmin storage/events.
PR49's branch was cut before #31 and #34 merged, so its new PayoutFrozen error variant independently claimed discriminant 24, the same slot #31 had already given to InvalidCreatorCount. Resolved the conflict by bumping PayoutFrozen to 25, keeping the additive-only numbering convention; no other change to the PR's logic. Co-Authored-By: Anuoluwapo Ali <anuoluwapoali25@gmail.com>
Summary
Replaces all / error paths with a typed enum, giving the frontend and indexers stable numeric codes to match on instead of opaque WASM traps.
Closes #15
Changes
BudgetBelowObligations(issue spec)SelectionLimitReached(issue spec)InvalidAmountinto specific variants:FeeTooHighfor out-of-range fee_bpsInvalidCreatorCountfor max_creators == 0InvalidDeadlineOrderfor application_deadline >= completion_deadlineOk(id)increate_campaignreclaim_surplusrequire_not_pausedguard tocreate_campaigncontracts/campaign-escrow/src/test.rsmod test_helpersmissing opening braceStellarAssetClient,Ledgertrait) to test_helperssetup()helper for standalone tests#[should_panic]tests for already-implemented functionserror_fee_too_higherror_invalid_creator_counterror_invalid_deadline_ordererror_deadline_in_pasterror_application_deadline_passederror_content_deadline_passederror_selection_limit_reachederror_budget_below_obligationsAcceptance Criteria
#[contracterror]enumErrordefined covering all current panic paths and all variants from the issueResult<_, Error>panic!or.expect()calls remain in public function bodies (test helpers exempt;todo!stubs in unimplemented functions preserved)Errorvariant on failure pathsCI Checks
All four CI jobs pass:
cargo fmt --all -- --check✓cargo clippy --workspace --all-targets -- -D warnings✓cargo test --workspace✓ (48 tests: 41 campaign-escrow + 7 dispute-resolution)cargo build --workspace --target wasm32v1-none --release✓