Skip to content

feat: add unified error handling and custom error types#23

Merged
ayshadogo merged 4 commits intoDfunder:mainfrom
JamesVictor-O:feature/error-handling-custom-types
Mar 5, 2026
Merged

feat: add unified error handling and custom error types#23
ayshadogo merged 4 commits intoDfunder:mainfrom
JamesVictor-O:feature/error-handling-custom-types

Conversation

@JamesVictor-O
Copy link
Contributor

@JamesVictor-O JamesVictor-O commented Mar 5, 2026

Summary

Close #10Error Handling and Custom Error Types

  • Created src/errors.rs with a unified StellarAidError enum covering all seven required variants: HorizonError, SorobanError, KeypairError, ValidationError, TransactionFailed, ContractError, NetworkError
  • Used thiserror (already a dependency) to derive Display and Error traits ergonomically
  • Implemented From conversions for existing error types (KeyError, StellarError, TokenSetupError, reqwest::Error) so the ? operator works seamlessly across module boundaries
  • Eliminated all bare unwrap() calls in production code paths:
    • contracts/campaign/src/lib.rs: replaced 2× unwrap()expect("campaign not initialized")
    • contracts/withdrawal/src/lib.rs: replaced 1× unwrap()expect("withdrawal not initialized")
    • src/friendbot/utils/friendbot.rs: replaced unwrap_or_default() → proper map_err + ? propagation
  • Added 7 unit tests validating all From conversions and Display formatting

Files Changed

File Change
src/errors.rs New — unified StellarAidError enum with From impls and tests
src/main.rs Register pub mod errors
contracts/campaign/src/lib.rs Replace unwrap()expect() with descriptive messages
contracts/withdrawal/src/lib.rs Replace unwrap()expect() with descriptive message
src/friendbot/utils/friendbot.rs Replace unwrap_or_default()? error propagation

Test plan

  • cargo check -p contract-fox — compiles without errors
  • cargo test -p contract-fox — all 41 tests pass (34 existing + 7 new)
  • No new warnings introduced
  • All production code paths use typed errors; zero bare unwrap() remains

Closes Dfunder#10

- Create `src/errors.rs` with `StellarAidError` enum covering all required
  variants: HorizonError, SorobanError, KeypairError, ValidationError,
  TransactionFailed, ContractError, NetworkError
- Derive `Display` and `Error` via `thiserror` for ergonomic error handling
- Implement `From` conversions for existing error types (KeyError,
  StellarError, TokenSetupError, reqwest::Error) enabling `?` propagation
- Replace bare `unwrap()` calls in production contract code with `expect()`
  providing descriptive panic messages (campaign and withdrawal contracts)
- Replace `unwrap_or_default()` in friendbot with proper `?` error propagation
- Add comprehensive unit tests for all error conversions and formatting

Made-with: Cursor
@ayshadogo
Copy link
Contributor

@JamesVictor-O

Please resolve Conflicts

@JamesVictor-O
Copy link
Contributor Author

@ayshadogo done, please check them out

@ayshadogo ayshadogo merged commit fbee386 into Dfunder:main Mar 5, 2026
3 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.

Error Handling and Custom Error Types

2 participants