Spaces:
Sleeping
Sleeping
Better error handling for API key test
Browse files
app.py
CHANGED
|
@@ -250,8 +250,11 @@ def test_api_key(api_key_input):
|
|
| 250 |
else:
|
| 251 |
return f"Key found but empty after strip. Raw length: {len(raw_key)}"
|
| 252 |
|
|
|
|
|
|
|
|
|
|
| 253 |
# Show key info (without revealing the key)
|
| 254 |
-
key_preview = f"{key_to_use[:
|
| 255 |
|
| 256 |
try:
|
| 257 |
client = anthropic.Anthropic(api_key=key_to_use)
|
|
@@ -262,10 +265,16 @@ def test_api_key(api_key_input):
|
|
| 262 |
messages=[{"role": "user", "content": "Say 'OK'"}]
|
| 263 |
)
|
| 264 |
return f"API key valid! ({key_preview})"
|
| 265 |
-
except anthropic.AuthenticationError:
|
| 266 |
-
return f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
except Exception as e:
|
| 268 |
-
return f"Error: {str(e)}"
|
| 269 |
|
| 270 |
|
| 271 |
# Build the interface
|
|
|
|
| 250 |
else:
|
| 251 |
return f"Key found but empty after strip. Raw length: {len(raw_key)}"
|
| 252 |
|
| 253 |
+
# Clean up common issues with key entry
|
| 254 |
+
key_to_use = key_to_use.strip().strip('"').strip("'")
|
| 255 |
+
|
| 256 |
# Show key info (without revealing the key)
|
| 257 |
+
key_preview = f"{key_to_use[:10]}...{key_to_use[-4:]}" if len(key_to_use) > 20 else f"(length: {len(key_to_use)})"
|
| 258 |
|
| 259 |
try:
|
| 260 |
client = anthropic.Anthropic(api_key=key_to_use)
|
|
|
|
| 265 |
messages=[{"role": "user", "content": "Say 'OK'"}]
|
| 266 |
)
|
| 267 |
return f"API key valid! ({key_preview})"
|
| 268 |
+
except anthropic.AuthenticationError as e:
|
| 269 |
+
return f"Auth failed ({key_preview}). Key may be invalid or expired. Check console.anthropic.com"
|
| 270 |
+
except anthropic.APIConnectionError as e:
|
| 271 |
+
return f"Connection error: Cannot reach Anthropic API. Check network."
|
| 272 |
+
except anthropic.RateLimitError as e:
|
| 273 |
+
return f"Rate limited. Key works but you've hit usage limits."
|
| 274 |
+
except anthropic.APIStatusError as e:
|
| 275 |
+
return f"API error {e.status_code}: {e.message}"
|
| 276 |
except Exception as e:
|
| 277 |
+
return f"Error ({type(e).__name__}): {str(e)}"
|
| 278 |
|
| 279 |
|
| 280 |
# Build the interface
|