Skip to content

contracts: replace bare panic with typed ContractError::AlreadyInitia… - #1136

Open
Silver36-ship-it wants to merge 1 commit into
ritik4ever:mainfrom
Silver36-ship-it:ReplaceBarePanicWithaTypedError
Open

contracts: replace bare panic with typed ContractError::AlreadyInitia…#1136
Silver36-ship-it wants to merge 1 commit into
ritik4ever:mainfrom
Silver36-ship-it:ReplaceBarePanicWithaTypedError

Conversation

@Silver36-ship-it

Copy link
Copy Markdown

Closes #753

…lized; add test

Description

Type of change

  • Bug fix
  • New feature
  • Breaking change

Security Checklist

Please review the SECURITY_CHECKLIST.md and check off any items that apply. Reviewers must sign off on these items before merge.

  • Input validation changed
  • Auth modified
  • New external fetch
  • Dependency added
  • Secret handling

Title: Replace bare panic in initialize() with typed ContractError::AlreadyInitialized

PR Summary

  • Description: Replace an untyped panic!("already initialized") in the contract initialize() guard with a typed contract error and add a unit test asserting the typed error is returned on a second initialization attempt.
  • Goal: Make error behavior structured so callers/tests receive AlreadyInitialized instead of an unstructured panic.

Why

  • Bare panics produce unstructured failure messages making error handling and off-chain integrations brittle. Using a typed ContractError allows deterministic assertions in tests and clearer runtime semantics.

Files changed

  • lib.rs — add ContractError enum, panic_error() helper, and replace the bare panic in initialize().
  • test.rs — add test_initialize_twice_returns_already_initialized() asserting a second initialize() call yields the AlreadyInitialized error.

Implementation details

  • Added a #[contracterror] enum ContractError including AlreadyInitialized (and other named variants used elsewhere).
  • Implemented fn panic_error(err: ContractError) -> ! { panic!("{:?}", err); } to unify existing error pattern usage.
  • Replaced:
    • panic!("already initialized");
      with:
    • panic_error(ContractError::AlreadyInitialized);
  • Added a test:
    • test_initialize_twice_returns_already_initialized() — registers the contract, calls initialize() twice, and uses #[should_panic(expected = "AlreadyInitialized")].

Tests

  • Added: unit test in test.rs
  • Run tests:
cd contracts
cargo test
  • Expected behavior:
    • All existing tests continue to pass.
    • New test asserts the second initialize() call panics with message AlreadyInitialized.

Compatibility & Migration

  • Backwards-compatible for existing successful initializations; behavior only differs when initialize() is called a second time (returns typed error instead of untyped panic).
  • No on-chain data migration required.

Risk / Impact

  • Low — localized change to initialization guard and test-only additions.
  • Ensure off-chain tooling which previously parsed the raw panic string still functions (typed error string will be the debug name, e.g., AlreadyInitialized).

Reviewer checklist

  • Confirm ContractError variant naming matches project conventions.
  • Confirm panic_error() usage is consistent with other error paths (preserves existing panic message format).
  • Run cargo test in contracts to validate no regressions.
  • Verify that other modules referencing raw panic strings are updated if they relied on the exact previous string (none expected except this case).

@vercel

vercel Bot commented Aug 29, 2026

Copy link
Copy Markdown

@Silver36-ship-it is attempting to deploy a commit to the ritik4ever's projects Team on Vercel.

A member of the Team first needs to authorize it.

@drips-wave

drips-wave Bot commented Aug 29, 2026

Copy link
Copy Markdown

@Silver36-ship-it 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Replace bare panic! with a typed Error for double initialization

1 participant