Skip to content

Conversation

sagpatil
Copy link
Contributor

@sagpatil sagpatil commented Oct 2, 2025

Fixes #2182 - Enhanced error messages with context and suggestions for better developer experience when invoking contracts, especially with bash scripts.
Providing detailed, context-rich error messages for contract invocation arguments, including expected vs. received types and actionable suggestions.

  • Pre-validating JSON arguments to catch malformed input and type mismatches early.
  • Detecting common mistakes, such as invalid addresses and quoting errors, with documentation links and best practices.
  • Refactoring argument parsing internals for better organization, maintainability, and adherence to Rust best practices:
  • Extracts long functions into focused helpers (build_clap_command, parse_command_matches, get_function_spec, parse_function_arguments, parse_single_argument, parse_file_argument, build_invoke_contract_args).
  • Improves error handling (e.g., replacing map().unwrap_or_else() with explicit matching).

Example improvement:

Before: Error: parsing argument amount: InvalidValue(Some(U64))
After: Failed to parse argument 'amount' - Expected u64 but received '"100"'
Suggestion: For numbers, ensure no quotes around the value

Fixes #2182 - Enhanced error messages with context and suggestions for better
developer experience when invoking contracts, especially with bash scripts.

- Add descriptive error messages with expected vs received values
- Pre-validate JSON arguments to catch errors early
- Detect common mistakes (malformed JSON, type mismatches, invalid addresses)
- Include actionable suggestions and best practices documentation
- Add comprehensive tests (5 new unit tests, all 33 tests passing)

Example improvement:
Before: Error: parsing argument amount: InvalidValue(Some(U64))
After: Failed to parse argument 'amount' - Expected u64 but received '"100"'
       Suggestion: For numbers, ensure no quotes around the value
@github-project-automation github-project-automation bot moved this to Backlog (Not Ready) in DevX Oct 2, 2025
sagpatil and others added 3 commits October 3, 2025 10:11
…organization

Resolves three clippy pedantic lints in contract argument parsing:

- **too_many_lines**: Refactored the 114-line `build_host_function_parameters_with_filter`
  function by extracting logical components into focused helper functions:
  - `build_clap_command()`: Handles clap command setup and subcommand registration
  - `parse_command_matches()`: Manages command line parsing and help message handling
  - `get_function_spec()`: Retrieves and validates function specifications
  - `parse_function_arguments()`: Orchestrates argument parsing for all function inputs
  - `parse_single_argument()`: Handles individual argument parsing with type validation
  - `parse_file_argument()`: Manages file-based argument loading (JSON and binary)
  - `build_invoke_contract_args()`: Constructs final InvokeContractArgs structure

- **map_unwrap_or**: Replaced `map().unwrap_or_else()` pattern with explicit `match`
  statement in `get_available_functions()` for better error handling clarity

- **uninlined_format_args**: Updated test assertion to use inline format arguments
  (`{case}` instead of `{}, case`) for improved readability

The refactoring maintains identical functionality while improving code maintainability,
readability, and adherence to Rust best practices. Each extracted function has a
single responsibility and clear ownership semantics.

All existing tests pass and clippy checks are clean.
@sagpatil sagpatil marked this pull request as ready for review October 8, 2025 19:41
@fnando fnando requested a review from Copilot October 8, 2025 22:41
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances error handling for contract invocation arguments in the Stellar CLI, providing more detailed and actionable error messages for developers invoking contracts, especially when using bash scripts. The changes improve argument parsing with better context, type validation, and specific suggestions for common mistakes.

Key changes:

  • Enhanced error messages with context including expected vs. received types and actionable suggestions
  • Pre-validation of JSON arguments to catch malformed input early
  • Refactored argument parsing into focused helper functions for better maintainability

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
cmd/soroban-cli/src/commands/contract/arg_parsing.rs Major refactoring of error types and argument parsing logic with enhanced error messages and modular helper functions
cmd/crates/soroban-test/tests/it/help.rs Updated test to match new structured error format for missing arguments
Contract_Invocation_Best_Practices.md New documentation providing best practices for contract invocation with bash scripts and JSON handling

sagpatil and others added 4 commits October 9, 2025 14:25
… feedback

Addresses all feedback from GitHub PR review #2235:

**Error Handling Improvements:**
- Replace panic-prone `unwrap()` with proper error handling in `parse_single_argument()`
- Add context-aware error suggestions based on expected type and received value
- Enhance `CannotParseArg` error with dynamic `suggestion` field

**Code Organization:**
- Extract `is_primitive_type()` helper function to improve readability
- Add `get_context_suggestions()` for intelligent error guidance
- Maintain necessary `mut` keyword where required by API

**Context-Aware Error Messages:**
- Numbers with quotes: "For numbers, ensure no quotes around the value (e.g., use 100 instead of \"100\")"
- Decimal for integers: "Integer types don't support decimal values - use a whole number"
- Invalid booleans: "For booleans, use 'true' or 'false' (without quotes)"
- Invalid addresses: "For addresses, use format: G... (account), C... (contract), or identity name"
- Unquoted strings: "For strings, ensure the value is properly quoted"
- Complex types: Type-specific formatting guidance

**Testing:**
- Tested all 13 error message types
- Verify context-aware suggestions work correctly
- Ensure all existing functionality remains intact

**Developer Experience:**
- Transform cryptic errors like "InvalidValue(Some(U64))" into actionable guidance
- Provide specific examples and alternative approaches in error messages
- Include troubleshooting steps for common issues (file access, JSON syntax, etc.)
- Update println! statements to use inline format arguments
- Maintain test functionality while adhering to clippy pedantic rules
- All tests continue to pass (7/7 argument parsing tests)
- Fix prettier formatting issues in Contract_Invocation_Best_Practices.md
- Ensures CI checks pass for markdown formatting
- All markdown files now comply with prettier rules
@sagpatil sagpatil requested a review from fnando October 14, 2025 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Backlog (Not Ready)

Development

Successfully merging this pull request may close these issues.

Improve CLI error handling and validation for contract invocation arguments

2 participants