Skip to content

Add Comprehensive Error Handling & Custom Error Types #27

Description

@OthmanImam

Context

Currently, contract functions panic on invalid inputs or edge cases. This creates poor UX and makes debugging difficult for frontend integration. We need structured error types that communicate specific failure reasons to callers.

Problem

  • No custom error types defined; functions use unwrap() which panics
  • Frontend cannot distinguish between different failure modes (invalid token, insufficient balance, swap validation failure)
  • Error messages are unhelpful for debugging or user-facing UI

What "Done" Looks Like

  • Define a custom ContractError enum covering all failure modes:
    • Invalid token symbol
    • Insufficient balance
    • Invalid swap pair
    • Zero amount swap attempt
    • Unauthorized access
  • Replace all unwrap() calls with proper Result<T, ContractError>
  • Update all public contract functions to return Result types
  • Add comprehensive test cases for each error path in swaptrade-contracts/counter/src/

Implementation Guidelines

  • Define error enum in a new errors.rs module
  • Use Soroban's Result with custom error mapping
  • Ensure error types serialize properly for contract invocation responses
  • Reference: Soroban error handling docs
  • Watch for: Panic paths in trading.rs and portfolio.rs logic

Acceptance Criteria

  • ContractError enum implemented with 5+ error variants
  • All public functions return Result<T, ContractError> (0 unwrap() calls in public API)
  • Error types serialize/deserialize correctly for Soroban contract responses
  • Test suite has 100% coverage of error paths
  • All error variants tested with expected contract failure modes
  • Documentation clearly explains each error type and when it occurs

Validation

  • All tests pass with new error handling
  • No panics occur on invalid inputs (test with malformed data)
  • Error enum variants are clearly documented

Metadata

Metadata

Assignees

Labels

Stellar WaveIssues in the Stellar wave program

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions