- Python
- TypeScript
def error_handling_example(client: Cartesia) -> None:
"""Example of error handling with SDK exceptions."""
try:
_response = client.tts.generate(
model_id="sonic-3",
transcript="Hello, world!",
voice={"mode": "id", "id": "6ccbfb76-1fc6-48f7-b71d-91ac6298247b"},
output_format={"container": "wav", "encoding": "pcm_f32le", "sample_rate": 44100},
)
except BadRequestError as e:
print(f"Bad request: {e}")
except AuthenticationError as e:
print(f"Auth failed: {e}")
except NotFoundError as e:
print(f"Not found: {e}")
except RateLimitError as e:
print(f"Rate limited: {e}")
except APIError as e:
print(f"API error: {e}")
async function errorHandling(client: Cartesia): Promise<void> {
/** Example of error handling with SDK exceptions. */
try {
await client.tts.generate({
model_id: 'sonic-3',
transcript: 'Hello, world!',
voice: { mode: 'id', id: '6ccbfb76-1fc6-48f7-b71d-91ac6298247b' },
output_format: { container: 'wav', encoding: 'pcm_f32le', sample_rate: 44100 },
});
} catch (e) {
if (e instanceof BadRequestError) {
console.log(`Bad request: ${e.message}`);
} else if (e instanceof AuthenticationError) {
console.log(`Auth failed: ${e.message}`);
} else if (e instanceof NotFoundError) {
console.log(`Not found: ${e.message}`);
} else if (e instanceof RateLimitError) {
console.log(`Rate limited: ${e.message}`);
} else if (e instanceof APIError) {
console.log(`API error: ${e.message}`);
} else {
throw e;
}
}
}
Run this example
- Python
- TypeScript
cd cartesia-python
CARTESIA_API_KEY=YOUR_KEY python3 examples/examples.py error_handling_example
cd cartesia-js
CARTESIA_API_KEY=YOUR_KEY npx ts-node examples/node_examples.ts errorHandling