Skip to content

Improve error handling in GhCliApiClient #495

Description

@hossain-khan

Problem

Currently, GhCliApiClient.executeCommand() throws a generic RuntimeException for all errors:

if (exitCode != 0) {
    throw RuntimeException("gh command failed with exit code $exitCode: $output")
}

This doesn't distinguish between:

  • GitHub CLI not installed
  • Not authenticated (gh auth login needed)
  • GitHub API errors (rate limit, not found, etc.)
  • Network connectivity issues

Proposed Solution

  1. Parse GH CLI error output to identify specific error types
  2. Create custom exceptions for different error scenarios:
    • GhCliNotAuthenticatedException
    • GhCliRateLimitException
    • GhCliNetworkException
  3. Map to existing error handling via ErrorProcessor
  4. Provide actionable error messages to users

Example:

private fun executeCommand(command: List<String>): String {
    val process = ProcessBuilder(command).redirectErrorStream(true).start()
    val output = BufferedReader(InputStreamReader(process.inputStream)).use { it.readText() }
    val exitCode = process.waitFor()
    
    if (exitCode != 0) {
        when {
            output.contains("authentication") || output.contains("not logged in") ->
                throw IllegalStateException("GitHub CLI not authenticated. Run: gh auth login")
            output.contains("rate limit") ->
                throw RuntimeException("GitHub API rate limit exceeded: $output")
            output.contains("not found") ->
                throw RuntimeException("Resource not found: $output")
            else ->
                throw RuntimeException("gh command failed with exit code $exitCode: $output")
        }
    }
    return output
}

Benefits

  • Better user experience with actionable error messages
  • Easier troubleshooting
  • Consistent error handling with Retrofit implementation
  • Graceful handling of authentication issues

Priority

High - Improves user experience significantly

Related Files

  • src/main/kotlin/dev/hossain/githubstats/client/GhCliApiClient.kt
  • src/main/kotlin/dev/hossain/githubstats/util/ErrorProcessor.kt

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions