Skip to content

feat: improve error handling with validation and user-friendly messages#22

Open
majiayu000 wants to merge 1 commit intoSthembisoMfusi:mainfrom
majiayu000:feature/better-error-handling
Open

feat: improve error handling with validation and user-friendly messages#22
majiayu000 wants to merge 1 commit intoSthembisoMfusi:mainfrom
majiayu000:feature/better-error-handling

Conversation

@majiayu000
Copy link

Summary

This PR addresses Issue #8 by significantly improving error handling in PyDex:

  • Input validation: Validates Pokémon names/IDs before making API requests

    • Checks for empty strings, whitespace-only inputs
    • Validates character format (only letters, numbers, hyphens allowed)
    • Warns about excessively long inputs or unreasonably high IDs
  • Improved HTTP error handling:

    • 404 Not Found: Provides helpful suggestions with example Pokémon names and IDs
    • 429 Rate Limit: Advises the user to wait before retrying
    • 500+ Server Errors: Explains that it's a server-side issue
    • Connection Errors: Suggests checking internet connection
    • Timeout Errors: Informs about slow server response (10 second timeout added)
  • Proper exit codes: Now exits with code 1 on errors for better scripting support

Changes

  • pokedex.py: Added validate_pokemon_input() function and improved fetch_pokemon_data() with specific error handlers
  • test_pokedex.py: Updated to match new error handling behavior
  • test_error_handling.py: Added comprehensive unit tests (24 tests) covering all new functionality

Test plan

  • All 24 new unit tests pass
  • All 4 original integration tests pass
  • Manual testing with various inputs (valid names, invalid names, network errors simulated)

Closes #8

Co-Authored-By: Claude Opus 4.5 noreply@anthropic.com

- Add input validation for Pokémon names and IDs
  - Check for empty strings, invalid characters, and excessive length
  - Validate numeric IDs are within reasonable range (1-10000)
- Improve HTTP error handling with specific messages for:
  - 404 Not Found: suggestions with example Pokémon names/IDs
  - 429 Rate Limit: advise waiting before retrying
  - 500+ Server Errors: explain server-side issues
  - Connection errors: suggest checking internet connection
  - Timeout errors: inform about slow server response
- Add request timeout (10 seconds) to prevent hanging
- Exit with code 1 on errors for proper error signaling
- Update existing tests to match new error handling behavior
- Add comprehensive unit tests for all new error handling

Closes SthembisoMfusi#8

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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.

[Good First Issue] Better Error Handling

1 participant