Error handling: prepare for the unknown
How to handle errors and exceptions in the CGC SDK (SDKException) and solve common issues such as installation and import problems.
Always wrap resource operations in try-except blocks:
import cgc.sdk.resource as resource
import cgc.sdk.exceptions as exceptions
try:
response = resource.resource_create(
name="my-app",
image_name="nginx:latest",
cpu=2,
memory=4
)
if response['code'] == 200:
print("Success!")
else:
print(f"Failed: {response.get('reason')}")
except exceptions.SDKException as e:
print(f"SDK Error (code {e.code}): {e}")
except Exception as e:
print(f"Unexpected error: {e}")
Common issues and solutions
Installation issues
Problem: Cannot import CGC SDK
Symptoms:
ImportError: No module named 'cgc.sdk'
Solutions:
- Verify installation:
pip list | grep cgc
- Install/reinstall CGC client:
pip install cgcsdk --upgrade
- Check Python path:
import sys
print(sys.path)
# Ensure CGC installation directory is in path
- Virtual environment issues:
# Activate your virtual environment
source venv/bin/activate # On Linux/Mac
# or
venv\Scripts\activate # On Windows
# Then reinstall
pip install cgcsdk