Skip to content

Improve configuration handling and error messages #399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
bhouston opened this issue May 4, 2025 · 1 comment
Open

Improve configuration handling and error messages #399

bhouston opened this issue May 4, 2025 · 1 comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@bhouston
Copy link
Member

bhouston commented May 4, 2025

Description

Based on the analysis of issue #397, there are several improvements that could be made to MyCoder's configuration handling to make it more user-friendly and robust:

Current Issues

  1. When a configuration file is not loaded correctly, MyCoder silently falls back to defaults without warning the user
  2. The error message about Anthropic API is confusing when the user has configured a different provider
  3. The ESM vs CommonJS format requirements are not clearly documented
  4. Environment variables for the default provider are checked even when a different provider is specified

Proposed Improvements

  1. Better Configuration Loading Feedback:

    • Log which configuration file was loaded and from where
    • Warn when no configuration file was found
    • Provide clear error messages for syntax or format errors in configuration files
  2. Provider-Specific Validation:

    • Only check for environment variables related to the configured provider
    • Add a validation step that verifies the required environment variables are present before attempting to use them
  3. Improved Error Messages:

    • When an API call fails, include information about which provider was being used
    • Provide clearer guidance on how to set up the required environment variables
    • Add a troubleshooting section to error messages
  4. Documentation Updates:

    • Clarify the naming and format requirements for configuration files
    • Add examples for different locations (project-level vs global)
    • Document Node.js version requirements for ESM support

Implementation Ideas

  1. Add a debug log in loadConfig() that shows which files were checked and which one was loaded
  2. Modify the provider factory to validate environment variables before attempting to create the provider
  3. Update error handling in API calls to include more context
  4. Add a configuration validation step that runs before executing commands

Related Issues

Priority

Medium - This would significantly improve the user experience, especially for new users setting up MyCoder for the first time.

@bhouston bhouston added enhancement New feature or request documentation Improvements or additions to documentation labels May 4, 2025
@bhouston
Copy link
Member Author

bhouston commented May 4, 2025

Issue Triage Analysis

Thank you for this comprehensive issue outlining improvements to MyCoder's configuration handling based on the analysis of issue #397.

Issue Classification

  • Type: Enhancement (with bug fixes)
  • Labels: enhancement, documentation, configuration

Assessment

I've analyzed the current configuration handling in MyCoder and confirmed all the issues mentioned:

  1. Silent configuration loading failures: When a configuration file exists but cannot be loaded correctly, MyCoder silently falls back to defaults without any warning to the user.

  2. Confusing API key errors: Even when a different provider is specified in the config, the code still checks for the Anthropic API key environment variable first, leading to confusing error messages.

  3. Unclear documentation: The ESM vs CommonJS format requirements and configuration file locations are not clearly documented.

  4. Improper environment variable validation: Environment variables for the default provider are checked even when a different provider is specified.

Technical Details

The root causes of these issues are:

  1. The c12 library is used to load configuration files, but there's no feedback to the user about which files were checked or loaded.

  2. In $default.ts, the provider validation happens too late in the process:

    // Early API key check based on model provider
    const providerSettings = providerConfig[config.provider as keyof typeof providerConfig];
    
    if (!providerSettings) {
      // Unknown provider
      logger.info(`Unknown provider: ${config.provider}`);
      throw new Error(`Unknown provider: ${config.provider}`);
    }
    
    // only validate key if baseUrl is not set, otherwise we assume the user is using a local provider
    let apiKey: string | undefined = undefined;
    const { keyName } = providerSettings;
    if (keyName) {
      // Then fall back to environment variable
      logger.info(`Looking API key in env: ${keyName}`);
      apiKey = process.env[keyName];
      if (!config.baseUrl) {
        if (!apiKey) {
          logger.error(getProviderApiKeyError(config.provider));
          throw new Error(`${config.provider} API key not found`);
        }
      }
    }
  3. There's no validation step that verifies the required environment variables before attempting to use them.

Implementation Recommendations

I agree with all the proposed improvements and suggest the following implementation approach:

  1. Better Configuration Loading Feedback:

    • Add debug logging in loadConfig() to show which files were checked and which one was loaded
    • Add warnings when a config file exists but couldn't be loaded due to syntax errors or format issues
  2. Provider-Specific Validation:

    • Create a validation function that checks the configuration for consistency before proceeding
    • Only check for environment variables related to the configured provider
    • Add clear messaging about which provider is being used
  3. Improved Error Messages:

    • Update the error handling in API calls to include information about which provider was being used
    • Provide clear guidance on how to set up the required environment variables
  4. Documentation Updates:

    • Create a dedicated configuration documentation page
    • Include examples for different locations and formats
    • Add a troubleshooting section for common configuration issues

Priority Assessment

This should be considered a medium-high priority issue as it significantly impacts user experience, especially for new users setting up MyCoder for the first time.

Would you like me to start working on any specific part of this enhancement?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant