Skip to content

fix(contracts): SC-22 standardize error codes across all Soroban contracts - #948

Open
NEA-DEV-coder wants to merge 1 commit into
OpenKnight-Foundation:mainfrom
NEA-DEV-coder:fix/sc-22-standardize-error-codes
Open

fix(contracts): SC-22 standardize error codes across all Soroban contracts#948
NEA-DEV-coder wants to merge 1 commit into
OpenKnight-Foundation:mainfrom
NEA-DEV-coder:fix/sc-22-standardize-error-codes

Conversation

@NEA-DEV-coder

Copy link
Copy Markdown

Summary

Closes #931

Replaces all panic!() calls across every Soroban contract with structured ContractError variants returned via panic_with_error!() or return Err(). Every error path now maps to a distinct u32 code exposed through the SDK's SCError::Contract, making frontend error handling deterministic.

Changes

contracts/game_contract/src/error.rs (new file)

  • Extracted ContractError enum out of lib.rs into its own module per acceptance criteria
  • Added 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

  • Replaced all 25 panic!() calls with panic_with_error!() (void-returning fns) or return Err(ContractError::...) (Result-returning fns)

contracts/emergency_circuit_breaker/src/lib.rs

  • Added new ContractError enum with 8 variants
  • Replaced all 12 panic!() calls
  • Updated should_panic tests (removed expected-string matching; structured errors use codes not strings)

contracts/ai_nft/src/lib.rs

  • Added 4 variants to existing ContractError; replaced all 6 panic!() calls

contracts/game_registry/contracts/game-registry/src/lib.rs

  • Added 3 variants to existing RegistryError; replaced all 4 panic!() calls

Testing

All tests pass (Rust 1.98 / Linux):

Contract Tests
ai_nft 3/3 ✅
emergency_circuit_breaker 14/14 ✅
game_contract 65/65 ✅

…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
@drips-wave

drips-wave Bot commented Aug 24, 2026

Copy link
Copy Markdown

@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! 🚀

Learn more about application limits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SC-22: Standardize error codes across all Soroban contracts

1 participant