Skip to content

refactor(insurance): return typed InsuranceError instead of panicking#749

Merged
Baskarayelu merged 1 commit into
Remitwise-Org:mainfrom
fikrah-Tech:refactor/insurance-typed-errors
Jun 18, 2026
Merged

refactor(insurance): return typed InsuranceError instead of panicking#749
Baskarayelu merged 1 commit into
Remitwise-Org:mainfrom
fikrah-Tech:refactor/insurance-typed-errors

Conversation

@yunus-dev-codecrafter

@yunus-dev-codecrafter yunus-dev-codecrafter commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

closes #712

Summary

Refactored the insurance contract to return typed InsuranceError instead of panicking, aligning it with the workspace error-handling convention used by other contracts (RemittanceSplitError, SavingsGoalError, BillPaymentsError).

Changes

  • Converted all public functions to return Result<T, InsuranceError>:

    • init(), create_policy(), pay_premium(), batch_pay_premiums()
    • set_external_ref(), deactivate_policy(), get_active_policies()
    • get_policy(), get_total_monthly_premium()
  • Converted internal helpers to return Result:

    • require_initialized(), get_owner(), load_policy(), validate_ext_ref()
  • Mapped all panics to existing InsuranceError variants:

    • 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
    • Already initialized → AlreadyInitialized
  • Added Rust doc comments documenting which errors each public function can return

  • Updated tests to handle Result types with .unwrap()

Why This Matters

Panics abort transactions with opaque host errors, preventing callers (CLI, indexer, orchestrator cross-contract flows) from distinguishing recoverable validation failures from real faults. Typed errors are the contract's public ABI for failure — the insurance contract was the only one missing this.

Acceptance Criteria

  • ✅ No panic!, unwrap(), or expect() in non-test insurance code
  • ✅ Every InsuranceError variant is reachable
  • ✅ ABI discriminants unchanged (1..11)
  • ✅ All existing tests updated to handle Result types

@Baskarayelu

Copy link
Copy Markdown
Contributor

yes, swapping the panics in create_policy/pay_premium for typed InsuranceError is exactly the hardening insurance needed. clean diff. merging.

@Baskarayelu Baskarayelu merged commit 0882dd9 into Remitwise-Org:main Jun 18, 2026
2 of 6 checks passed
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.

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

2 participants