Skip to content

Convert create_policy and pay_premium validation panics to typed InsuranceError returns #1024

Description

@Baskarayelu

📋 Description

create_policy and pay_premium in insurance/src/lib.rs use panic! for several validation conditions (name, premium, coverage, combination) even though the crate defines a full InsuranceError enum (InvalidName, InvalidPremium, InvalidCoverageAmount, UnsupportedCombination, PolicyInactive, etc.). These panics abort with opaque host errors instead of the typed codes the rest of the contract uses.

This issue converts the reachable validation panics in create_policy and pay_premium to typed InsuranceError returns.

Why this matters: Insurance feeds Reporting's get_insurance_report and orchestrated premium flows. A panic on invalid input aborts the whole call chain instead of returning a code a caller can branch on, and panics are far harder to test and audit than typed errors.

🎯 Requirements & Context

Functional requirements

  • Replace name/premium/coverage/combination validation panics in create_policy with InvalidName / InvalidPremium / InvalidCoverageAmount / UnsupportedCombination.
  • Replace pay_premium validation panics (inactive policy, missing policy) with PolicyInactive / PolicyNotFound.
  • Honor the per-CoverageType bounds in TypeConstraints::for_type when validating.
  • Update tests that asserted panics to assert typed errors.

Context & constraints

  • Soroban SDK 21.7.7; reuse existing InsuranceError variants.
  • Keep the success path and next_payment_date advancement unchanged.
  • Tests run with cargo test -p insurance.

🛠️ Suggested Execution

1. Fork & branch

git checkout -b refactor/insurance-typed-errors

2. Implement changes

  • Convert the panic sites and change signatures to Result<_, InsuranceError> where needed in insurance/src/lib.rs.
  • Add /// doc comments listing each entrypoint's error codes.

3. Test & commit

cargo test -p insurance
  • Cover edge cases: each TypeConstraints min/max boundary, unsupported coverage/amount combination, pay on inactive policy, pay on missing policy.

Example commit message

refactor(insurance): return typed InsuranceError from create_policy and pay_premium

Replaces validation panics with InvalidName/InvalidPremium/UnsupportedCombination etc.

✅ Acceptance Criteria & Guidelines

Requirement Target
Validation panics replaced with typed errors Required
TypeConstraints bounds honored Required
Coverage of validation paths ≥ 95%, all branches
Doc comments listing error codes Required
cargo test -p insurance + cargo clippy clean Required
Timeframe 96 hours from assignment

💬 Community & Support

Join the Remitwise contributor community on Discord: https://discord.gg/CtQuPZFMA — comment when you pick this up. 🚀

Metadata

Metadata

Assignees

No one assigned

    Type

    Fields

    No fields configured for Task.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions