fix(contracts): SC-22 standardize error codes across all Soroban contracts - #948
Open
NEA-DEV-coder wants to merge 1 commit into
Open
Conversation
…racts Replace all panic!() calls with structured ContractError variants and panic_with_error!() / return Err() so frontends get deterministic u32 error codes instead of opaque string panics. Changes: - contracts/game_contract/src/error.rs (new) Move ContractError out of lib.rs into its own module; add 8 new variants: AlreadyInitialized(40), NotInitialized(41), AdminAlreadySet(42), NotAdmin(43), AlreadyPaused(44), NotPaused(45), InvalidConfig(46), InsufficientTreasury(47) - contracts/game_contract/src/lib.rs Import mod error + panic_with_error; remove inline enum; replace all 25 panic!() calls with panic_with_error!() (void fns) or return Err() (Result-returning fns) - contracts/emergency_circuit_breaker/src/lib.rs Add ContractError enum (AlreadyInitialized, NotAdmin, AlreadyPaused, NotPaused, ContractPaused, InvalidAmount, InsufficientBalance, EscrowAlreadyExists); replace all 12 panic!() calls; update should_panic tests to remove expected-string assertions - contracts/ai_nft/src/lib.rs Add AlreadyInitialized(8), NotAdmin(9), AlreadyPaused(10), NotPaused(11) variants to existing ContractError; replace 6 panic!() calls - contracts/game_registry/contracts/game-registry/src/lib.rs Add NotAdmin(11), AlreadyPaused(12), NotPaused(13) variants to existing RegistryError; replace 4 panic!() calls with return Err() All tests pass: ai_nft 3/3, emergency_circuit_breaker 14/14, game_contract 65/65 Closes OpenKnight-Foundation#931
|
@NEA-DEV-coder Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #931
Replaces all
panic!()calls across every Soroban contract with structuredContractErrorvariants returned viapanic_with_error!()orreturn Err(). Every error path now maps to a distinctu32code exposed through the SDK'sSCError::Contract, making frontend error handling deterministic.Changes
contracts/game_contract/src/error.rs(new file)ContractErrorenum out oflib.rsinto its own module per acceptance criteriaAlreadyInitialized(40),NotInitialized(41),AdminAlreadySet(42),NotAdmin(43),AlreadyPaused(44),NotPaused(45),InvalidConfig(46),InsufficientTreasury(47)contracts/game_contract/src/lib.rspanic!()calls withpanic_with_error!()(void-returning fns) orreturn Err(ContractError::...)(Result-returning fns)contracts/emergency_circuit_breaker/src/lib.rsContractErrorenum with 8 variantspanic!()callsshould_panictests (removed expected-string matching; structured errors use codes not strings)contracts/ai_nft/src/lib.rsContractError; replaced all 6panic!()callscontracts/game_registry/contracts/game-registry/src/lib.rsRegistryError; replaced all 4panic!()callsTesting
All tests pass (Rust 1.98 / Linux):
ai_nftemergency_circuit_breakergame_contract