devchat-ai / devchat

Automate your dev tasks with AI-powered scripts, from your IDE's chat panel.
https://www.devchat.ai
Apache License 2.0
342 stars 39 forks source link

Handle Leading or Trailing Spaces in KEY to Prevent Misleading Connection Errors #296

Closed runjinz closed 5 months ago

runjinz commented 5 months ago

Issue Description

When a KEY contains leading or trailing spaces, it triggers the following error: h11._util.LocalProtocolError: Illegal header value. The openai library, upon catching this exception, rethrows an openai.APIConnectionError: Connection error., which can misleadingly suggest that there's an issue with the network connection instead of an illegal header value caused by the KEY formatting.

Expected Behavior

It would be beneficial to perform trimming (removing leading and trailing spaces) on the KEY before attempting to use it. This pre-validation could prevent the mentioned misleading openai.APIConnectionError and provide clearer debugging information in cases where an invalid KEY format is the issue.

Suggested Solution

Implement a trimming function or check that automatically removes any leading or trailing spaces from the KEY before any operations are made with it. This could be done as part of the initial setup or configuration process when the KEY is first set or applied.

Why This is Important

By addressing this issue, we can ensure that errors are reported more accurately, making it easier for developers to diagnose and fix issues. Misleading errors like a Connection error when the actual problem lies with the KEY format can lead to unnecessary troubleshooting efforts focused on the wrong area.