Skip to content

Insurance: convert panic!-based validation in create_policy / pay_premium to typed InsuranceError returns #712

@Baskarayelu

Description

@Baskarayelu

📋 Description

Every other contract in the workspace returns a typed #[contracterror] (e.g. RemittanceSplitError, SavingsGoalError, BillPaymentsError), but insurance defines InsuranceError in insurance/src/lib.rs and then does not use itcreate_policy, pay_premium, and deactivate_policy all panic! with string messages such as "monthly_premium out of range for coverage type" and "policy not found".

Why this matters: Panics abort the transaction with an opaque host error, so callers (CLI, indexer, and especially the orchestrator cross-contract flow) cannot distinguish a recoverable validation failure from a real fault. Typed errors are the contract's public ABI for failure — the insurance contract is the only one missing it.

🎯 Requirements & Context

Functional requirements

  • Change create_policy, pay_premium, batch_pay_premiums, set_external_ref, deactivate_policy, get_active_policies, and get_total_monthly_premium signatures to return Result<_, InsuranceError> where they currently panic!.
  • Map each existing panic to the correct existing variant: empty/too-long name → InvalidName; bad premium → InvalidPremium; bad coverage → InvalidCoverageAmount; ratio guard → UnsupportedCombination; bad external ref → InvalidExternalRef; cap hit → MaxPoliciesReached; missing policy → PolicyNotFound; inactive → PolicyInactive; non-owner → Unauthorized; uninitialized → NotInitialized.
  • Replace the init panic!("already initialized") with AlreadyInitialized.
  • Update insurance/src/test.rs to assert Result::Err(InsuranceError::...) instead of #[should_panic].

Context & constraints

  • Soroban SDK 21.7.7, #![no_std]. InsuranceClient::try_* methods become the test surface for error assertions.
  • Preserve numeric error discriminants already assigned (1..11) to keep ABI stability.
  • Do not change validation thresholds — this is a representation change only.

🛠️ Suggested Execution

1. Fork & branch

git checkout -b refactor/insurance-typed-errors

2. Implement changes

  • Convert internal helpers require_initialized, load_policy, validate_ext_ref to return Result.
  • Add Rust doc comments documenting which error each public fn can return.
  • Update next_payment_scheduling_tests.rs references if affected.

3. Test & commit

cargo test -p insurance -- --nocapture
cargo clippy -p insurance --all-targets -- -D warnings
  • Edge cases: ensure no remaining panic!/unwrap()/expect() in non-test code paths.

Example commit message

refactor(insurance): return typed InsuranceError instead of panicking

Wires the existing InsuranceError variants into create_policy,
pay_premium and deactivate_policy, aligning insurance with the
workspace error-handling convention.

✅ Acceptance Criteria & Guidelines

Requirement Target
No panic!/unwrap/expect in non-test insurance code Required
Every variant of InsuranceError reachable & tested Required
Error coverage of converted paths ≥ 95%
cargo test -p insurance + clippy clean Required
ABI discriminants unchanged Required
Timeframe 96 hours from assignment

💬 Community & Support

Questions and design discussion — join the Remitwise contributor community on Discord: https://discord.gg/CtQuPZFMA

Please comment on the issue when you pick it up so we avoid duplicate work and can unblock you quickly. 🚀

Metadata

Metadata

No fields configured for Feature.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions